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


相关推荐

  • phpstorm-激活码(破解版激活)[通俗易懂]

    phpstorm-激活码(破解版激活),https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月16日
    63
  • 最详细的APAP论文代码分析

    最详细的APAP论文代码分析最详细的APAP论文代码分析代码见:https://cs.adelaide.edu.au/~tjchin/apap/此次实验选用的代码是2013年的版本:由于文档中的代码块截图不一定清楚,需要的可以去上面的网址下载代码对照着看。一、代码1.1、加载文件在程序开始前调用close、clear等函数清除原先工作空间的操作,然后将此次实验所需的文件文件添加到环境中。1.2、编译Mex文件经过对代码块添加测试代码,证明了此处的代码块并未执行,在命令行仅仅输出了对文件的判断,而未输出if-end

    2025年12月8日
    3
  • python数据可视化从入门到实战_大数据可视化概念

    python数据可视化从入门到实战_大数据可视化概念真依然很拉风,简书《数据可视化》专栏维护者,里面有很多优秀的文章,本文便是其中一篇。文章总结了多种数据可视化图形,并简要介绍了各种图形的作用,能为科研工作者在数据可视化阶段提供新的思路,在…

    2022年10月2日
    5
  • C#中关于Struct(结构体)介绍以及附带案例解释,一看就会,一学就懂系列[通俗易懂]

    C#中关于Struct(结构体)介绍以及附带案例解释,一看就会,一学就懂系列[通俗易懂]Struct结构体的含义:结构体的作用?结构体的特点?结构体的声明?结构体的注意事项?…

    2022年10月6日
    4
  • linux中vi和退出 简单操作

    linux中vi和退出 简单操作进入编辑模式 按 o 进行编辑编辑结束 按 ESC 键跳到命令模式 然后输入退出命令 w 保存文件但不退出 vi 编辑 w 强制保存 不退出 vi 编辑 wfile 将修改另存到 file 中 不退出 vi 编辑 wq 保存文件并退出 vi 编辑 wq 强制保存文件并退出 vi 编辑 q 不保存文件并退出 vi 编辑 q 不保存文件并强制退出 vi 编辑 e 放弃所有修改 从上次保

    2026年3月17日
    2
  • python——pkl文件

    python——pkl文件pkl文件是python里面保存文件的一种格式,如果直接打开会显示一堆序列化的东西。cPickle在python3中更名为pickle使用方式如下:importpickleaspshoplistfile=’shoplist.data’#保存文件数据所在文件的文件名shoplist=[‘apple’,’mango’,’carrot’]f=open(shoplistfile,’wb’)#二进制打开,如果找不到该文件,则创建一个p.dump(shoplist,f)

    2025年10月9日
    8

发表回复

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

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