RC4算法
RC4算法简介:https://baike.baidu.com/item/RC4%E7%AE%97%E6%B3%95/9686396?fr=aladdin
RC4算法java实现:
/**
* RC4加解密算法
* RC4对称性加密解密算法
*/
public class RC4 { /**
* 解密后的数据为String类型
*
* @param data
* @param key
* @return
*/
public static String decryRC4Str(byte[] data, String key) {
if (data == null || key == null) {
return null;
}
return asString(RC4Base(data, key));
} /**
* 解密后的数据为String类型
*
* @param data
* @param key
* @return
*/
public static String decryRC4Str(String data, String key) {
if (data == null || key == null) {
return null;
}
return new String(RC4Base(hexString2Bytes(data), key));
} /**
* 解密后的数据为byte[]类型
*
* @param data
* @param key
* @return
*/
public static byte[] decryRC4Byte(byte[] data, String key) {
if (data == null || key == null) {
return null;
}
return RC4Base(data, key);
} /**
* 加密后的数据为byte[]类型
*
* @param data
* @param key
* @return
*/
public static byte[] encryRC4Byte(String data, String key) {
if (data == null || key == null) {
return null;
}
byte b_data[] = data.getBytes();
return RC4Base(b_data, key);
} /**
* 加密后的数据为String类型
*
* @param data
* @param key
* @return
*/
public static String encryRC4Str(String data, String key) {
if (data == null || key == null) {
return null;
}
return toHexString(asString(encryRC4Byte(data, key)));
} /**
* 将byte[]转化成String类型
*
* @param buf
* @return
*/
private static String asString(byte[] buf) {
StringBuffer strbuf = new StringBuffer(buf.length);
for (int i = 0; i < buf.length; i++) {
strbuf.append((char) buf[i]);
}
return strbuf.toString();
} /**
* 初始计算Key
*
* @param aKey
* @return
*/
private static byte[] prepareKey(String aKey) {
byte[] b_key = aKey.getBytes();
byte state[] = new byte[256]; for (int i = 0; i < 256; i++) {
state[i] = (byte) i;
}
int index1 = 0;
int index2 = 0;
if (b_key == null || b_key.length == 0) {
return null;
}
for (int i = 0; i < 256; i++) {
index2 = ((b_key[index1] & 0xff) + (state[i] & 0xff) + index2) & 0xff;
byte tmp = state[i];
state[i] = state[index2];
state[index2] = tmp;
index1 = (index1 + 1) % b_key.length;
}
return state;
} /**
* 将String转化成16进制的String
*
* @param s
* @return
*/
private static String toHexString(String s) {
String str = "";
for (int i = 0; i < s.length(); i++) {
int ch = (int) s.charAt(i);
String s4 = Integer.toHexString(ch & 0xFF);
if (s4.length() == 1) {
s4 = '0' + s4;
}
str = str + s4;
}
return str;// 0x表示十六进制
} /**
* 将String转换成byte[]
*
* @param src
* @return
*/
private static byte[] hexString2Bytes(String src) {
int size = src.length();
byte[] ret = new byte[size / 2];
byte[] tmp = src.getBytes();
for (int i = 0; i < size / 2; i++) {
ret[i] = uniteBytes(tmp[i * 2], tmp[i * 2 + 1]);
}
return ret;
} /**
* byte的异或
*
* @param src0
* @param src1
* @return
*/
private static byte uniteBytes(byte src0, byte src1) {
char _b0 = (char) Byte.decode("0x" + new String(new byte[]{src0})).byteValue();
_b0 = (char) (_b0 << 4);
char _b1 = (char) Byte.decode("0x" + new String(new byte[]{src1})).byteValue();
byte ret = (byte) (_b0 ^ _b1);
return ret;
} /**
* RC4算法核心
*
* @param input
* @param mKkey
* @return
*/
private static byte[] RC4Base(byte[] input, String mKkey) {
int x = 0;
int y = 0;
byte key[] = prepareKey(mKkey);
int xorIndex;
byte[] result = new byte[input.length];
for (int i = 0; i < input.length; i++) {
x = (x + 1) & 0xff;
y = ((key[x] & 0xff) + y) & 0xff;
byte tmp = key[x];
key[x] = key[y];
key[y] = tmp;
xorIndex = ((key[x] & 0xff) + (key[y] & 0xff)) & 0xff;
result[i] = (byte) (input[i] ^ key[xorIndex]);
}
return result;
}
}
RC4算法的更多相关文章
- 对RC4算法进行改写,新的加密算法RCX。
最近研究JWT算法, JWT由header.payload.signature三个部分组成,payload是非加密的,一些敏感信息能被别人非法获得,必要时候要加密. 加密算法中,RC4算法的速度可以达 ...
- WebSphere禁用SSLv3和RC4算法教程
WebSphere经常会报“SSL 3.0 POODLE攻击信息泄露”和"SSL/TLS 受诫礼(BAR-MITZVAH)攻击"两个漏洞,前者建议禁用SSL算法后者建议禁用RC4算 ...
- lua rc4算法实现
由于项目需要,用python django写restful接口遇到瓶颈,python django+uwsgi处理请求是会阻塞的, 如果阻塞请求不及时处理,会卡住越来越多的其它的请求,导致越来越多的5 ...
- (转)WebSphere禁用SSLv3和RC4算法教程
原文:https://www.cnblogs.com/lsdb/p/7126399.html WebSphere经常会报“SSL 3.0 POODLE攻击信息泄露”和"SSL/TLS 受诫礼 ...
- RC4算法的Python实现详注
刚对RC4算法进行了学习,网上发现https://ju.outofmemory.cn/entry/46753 中作者展示了RC4的python实现,但代码缺乏注释,较为晦涩,因此本文对部分代码进行了注 ...
- c# rc4算法,加密解密类
rc4算法,原理,以密匙生成256位的密匙流,然后以车轮式滚过源数据异或加密. /* * 由SharpDevelop创建. * 用户: YISH * 日期: 04/04/2015 * 时间: 03:0 ...
- Weblogic禁用SSLv3和RC4算法教程
weblogic在启用https时一样会报同WebSphere那样的一SSL类漏洞,中间件修复这些漏洞原理上来说是一样的,只是在具体操作上有着较大的区别. 1. weblogic禁用SSLv3算法 编 ...
- Tomcat禁用SSLv3和RC4算法
1.禁用SSLv3(SSL 3.0 POODLE攻击信息泄露漏洞(CVE-2014-3566)[原理扫描]) 编缉$CATALINA_HOEM/conf/server.xml配置文件,找到https端 ...
- (转)SSL/TLS 漏洞“受戒礼”,RC4算法关闭
原文:https://blog.csdn.net/Nedved_L/article/details/81110603 SSL/TLS 漏洞“受戒礼” 一.漏洞分析事件起因2015年3月26日,国外数据 ...
随机推荐
- CentOS7 安装FastDFS单机版
1. 下载 FastDFS https://github.com/happyfish100/fastdfs/releases libfastcommon https://github.com/happ ...
- Asp.net管道模型之(HttpModules 和 HttpHandler)
上一节我们从大概范围介绍了管道模型的整体流程,我们从其中知道管道最重要的两大组件为:HttpModules 跟 HttpHandler.今天我们着重来介绍一下这两大组件 一:asp.net处理管道 从 ...
- pipenv管理python开发环境
简介 简单说,pipenv就是把pip和virtualenv包装起来的一个便携工具. 它不会在你的项目文件夹里生成一大堆东西,只有两个文本文件: Pipfile, 简明地显示项目环境和依赖包. Pip ...
- odoo10学习笔记二:继承(扩展)、模块数据
转载请注明原文地址:https://www.cnblogs.com/ygj0930/p/11189252.html 一:继承 在不改变底层对象的时候添加新的功能——这是通过继承机制来实现的,作为在现有 ...
- CPDF_Document
auto pDoc = std::unique_ptr<CPDF_Document>(); pDoc->CreateNewDoc(); auto pDict = CPDF_Dicti ...
- centos7-cockpit
yum install cockpit* -y systemctl start cockpit.service systemctl enable cockpit.service 修改默认端口9090 ...
- 吴丽丽-201871010123《面向对象程序设计(Java)》第七周学习总结
吴丽丽-201871010123<面向对象程序设计(Java)>第七周学习总结 项目 内容 这个作业属于哪个课程 http://www.cnblogs.com/nwnu-daizh/ 这个 ...
- 201871010128-杨丽霞《面向对象程序设计(java)》第十三周学习总结
201871010128-杨丽霞<面向对象程序设计(java)>第十三周学习总结 项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ ...
- IIS网站应用偶尔出现"服务不可用"或者显示乱码字体
IIS网站应用偶尔出现"服务不可用"或者显示乱码字体,使用以下办法可以解决. 原因:此种情况常会出现在iis是在Visual Studio或者.NET Framework之后安装发 ...
- 莫烦RL-01小例子
# Python 3.6.5 :: Anaconda, Inc. import numpy as np import pandas as pd import time np.random.seed(2 ...