java 异或加密_使用异或实现一个简单的加密或解密

java 异或加密_使用异或实现一个简单的加密或解密/**Copyright(C)2017,MegatronKing**LicensedundertheApacheLicense,Version2.0(the”License”);youmaynotusethisfileexcept*incompliancewiththeLicense.YoumayobtainacopyoftheLicenseat**http://www.apache.org/licenses/.

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

/*
 * Copyright (C) 2017, Megatron King
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */

package com.yunshouhu.bouncycastle.xor;

import android.util.Base64;


/**
 * StringFog base64+xor encrypt and decrypt implementation.
 *
 * @author Megatron King
 * @since 2018/9/2 14:34
 */
public  class xorEncrypt{

    private static final String CHARSET_NAME_UTF_8 = "UTF-8";

   
    public String encrypt(String data, String key) {
        String newData;
        try {
            newData = new String(Base64.encode(xor(data.getBytes(CHARSET_NAME_UTF_8), key), Base64.NO_WRAP));
        } catch (Exception e) {
            newData = new String(Base64.encode(xor(data.getBytes(), key), Base64.NO_WRAP));
        }
        return newData;
    }

    
    public String decrypt(String data, String key) {
        String newData;
        try {
            newData = new String(xor(Base64.decode(data, Base64.NO_WRAP), key), CHARSET_NAME_UTF_8);
        } catch (Exception e) {
            newData = new String(xor(Base64.decode(data, Base64.NO_WRAP), key));
        }
        return newData;
    }

    /**
    public boolean overflow(String data, String key) {

        return data != null && data.length() * 4 / 3 >= 1024;
    }*/

    private static byte[] xor(byte[] data, String key) {
        int len = data.length;
        int lenKey = key.length();
        int i = 0;
        int j = 0;
        while (i < len) {
            if (j >= lenKey) {
                j = 0;
            }
            data[i] = (byte) (data[i] ^ key.charAt(j));
            i++;
            j++;
        }
        return data;
    }

    public static void main(String[] args) {
        
        for(int i=0;i<100;i++)
        {
            String key="android"+i;
            String dataString="java锄禾日当午,汗滴禾下土,谁知盘中餐粒粒皆辛苦";
            xorEncrypt xor=new xorEncrypt();
            String cipher=xor.encrypt(dataString, key);
            System.out.println(cipher);
            
            String textString=xor.decrypt(cipher, key);
            if(!textString.equals(dataString))
            {
                System.err.println("error textString="+textString+",dataString="+dataString);
            }else{
                System.out.println("textString="+textString);
            }
        }
        System.out.println("==============");
        
        for(int i=0;i<100;i++)
        {
            String key="android";
            String dataString="java锄禾日当午,汗滴禾下土,谁知盘中餐粒粒皆辛苦"+i;
            xorEncrypt xor=new xorEncrypt();
            String cipher=xor.encrypt(dataString, key);
            System.out.println(cipher);
            
            String textString=xor.decrypt(cipher, key);
            if(!textString.equals(dataString))
            {
                System.err.println("error textString="+textString+",dataString="+dataString);
            }else{
                System.out.println("textString="+textString);
            }
        }
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/185906.html原文链接:https://javaforall.net

(0)
上一篇 2022年10月4日 下午8:46
下一篇 2022年10月4日 下午8:46


相关推荐

  • COM组件原理_Com组件

    COM组件原理_Com组件在COM中,接口就是一个象类,每个接口有一个接口ID(uuid)。一个COM组件通常是连续继承下来的类,比如IUNknow->IDispath->IXX->CXX。这就形成了一个COM组件,当然组件一般是一个钻石继承的样子,这里为了简化原理把他们当成一个串形继承下来。每个COM组件都有一个CLSID(uuid),这个CLSID是注册的时候写进注册表的。这样就可以通过查询注册表中的CLSID

    2025年5月31日
    4
  • 百度分享代码–一键分享Baidu Share BEGIN

    百度分享代码–一键分享Baidu Share BEGINhttp://share.baidu.com/code/advance一、概述百度分享代码已升级到2.0,本页将介绍新版百度分享的安装配置方法,请点击左侧列表查看相关章节。二、代码结构分享代码可以分为三个部分:HTML、设置和js加载,示例如下:代码结构如下: 展示按钮–> window._bd_share_config={

    2022年10月8日
    3
  • 最近公共祖先_洛谷好不好

    最近公共祖先_洛谷好不好原题链接题目描述如题,给定一棵有根多叉树,请求出指定两个点直接最近的公共祖先。输入格式第一行包含三个正整数 N,M,SN,M,S,分别表示树的结点个数、询问的个数和树根结点的序号。接下来 N-1N−1 行每行包含两个正整数 x, yx,y,表示 xx 结点和 yy 结点之间有一条直接连接的边(数据保证可以构成树)。接下来 MM 行每行包含两个正整数 a, ba,b,表示询问 aa 结点和 bb 结点的最近公共祖先。输出格式输出包含 MM 行,每行包含一个正整数,依次为每一个询问的结果。输入

    2022年8月8日
    9
  • 深入了解按位异或(转载)

    深入了解按位异或(转载)深入理解按位异或运算符参与运算的两个值 如果两个相应的 bit 位相同 则结果为 0 否则为 1 0 0 0 1 0 1 0 1 1 1 1 0 按位异或的三个特点 下面将以具体的东西加深理解 1 0 异或任何数 任何数 2 1 异或任何数 任何数取反 3 任何数异或自己 把自己变成了 0 可以利用异或运算法则进行特定的位翻转 利用 1 2 1 比如把第 2 位

    2026年3月18日
    2
  • Nano Banana 2重磅发布!通过一步API平台,解锁闪电级AI图像生成能力

    Nano Banana 2重磅发布!通过一步API平台,解锁闪电级AI图像生成能力

    2026年3月15日
    1
  • hi3516dv300芯片手册_hi3518ev300

    hi3516dv300芯片手册_hi3518ev300基于Hi3516DV300的嵌入式入门演练(上)基于Hi3516DV300的嵌入式入门演练(下)文章目录信息前言勉励1开始之前1.1操作系统与开发准备1.2推荐的书籍1.3书本之外2最小系统环境的搭建流程2.1VMWareWorkstationPlayer和Kubuntu2.1.1创建虚拟机2.1.2Kubuntu系统安装2.2搭建Hi3516DV300的开发环境2.2.1工具链安装与开发环境配置2.2.2展开SDK2.3U-Boot的编译2.4Kernel的编译2.5根

    2026年2月26日
    4

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号