前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >java 异或加密_使用异或实现一个简单的加密或解密

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

作者头像
全栈程序员站长
发布2022-11-09 10:55:11
发布2022-11-09 10:55:11
1.4K00
代码可运行
举报
运行总次数:0
代码可运行

大家好,又见面了,我是你们的朋友全栈君。

代码语言:javascript
代码运行次数:0
复制
/*
* 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.cn/185906.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年10月4日 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档