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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 设置eclipse代码自动补全功能

    设置eclipse代码自动补全功能1.选择Eclipse菜单条中的Windows菜单下的Preferences项2.在左侧找到“Java”->“Editor”->“ContentAssist”3.在右侧“AutoActivation”项目下找到“AutoactivationtriggersforJava:”(可以看到设置框中默认的只有“.”,这就是为什么默认只有点“.”可以触发自动补全…

    2022年5月31日
    54
  • python中dropna()什么意思_python dropna函数

    python中dropna()什么意思_python dropna函数我正在尝试平均化熊猫的一组数据。csv文件中的数据。我有一个系列节目叫“轨道”。在前面的阶段中,我使用了dropna()方法来删除在读取csv文件时导入的一些空白行。在我使用的方法是平均5行以上的列。我不能使用滚动平均法,因为我希望使用当前值之前的两行、当前值和当前值之后的两行来获取平均值。在当我得到数据时,我遇到了一些问题,这些数据中的NaN数据已经被删除,因为标签也被删除了。在defget_…

    2026年1月15日
    4
  • HTTP中的Accept-Encoding、Content-Encoding、Transfer-Encoding、Content-Type[通俗易懂]

    HTTP中的Accept-Encoding、Content-Encoding、Transfer-Encoding、Content-Type[通俗易懂]AcceptEncoding和ContentEncodingAcceptEncoding和ContentEncoding是HTTP中用来对采用何种压缩格式传输正文进行协定的一对header。

    2022年7月1日
    31
  • php 获取当前用户的IP

    代码如下:推荐:http://www.cnblogs.com/roucheng/p/phpexcel.html

    2021年12月24日
    48
  • Java截取字符串方法_java通过split截取字符串

    Java截取字符串方法_java通过split截取字符串主要有以下几种方法:1、通过subString()方法来进行字符串截取(最常用)2、通过StringUtils提供的方法3、split()+正则表达式来进行截取先来介绍最常用的一种1、通过subString()方法来进行字符串截取,返回字符串中的子字符串,在java中有两种用法第一种,传递一个参数:publicStringsubstring(intbeginIndex)//该子字符串从指定索引处的字符开始,直到此字符串末尾。第二种,传递两个参数:

    2022年10月6日
    3
  • 生物识别指纹_生物指纹识别技术

    生物识别指纹_生物指纹识别技术锁屏要使用指纹解锁,首先要注册指纹服务,我看过的一些大厂项目中,实际上是在KeyguardUpdate.java类中发起注册的,一般是根据当前状态,是不是已经处于上锁状态(侧边指纹机器,是不等上锁即进行指纹服务注册,屏下指纹需要等上锁后,才发起指纹服务注册)。………………………

    2022年8月10日
    13

发表回复

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

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