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算法的更多相关文章

  1. 对RC4算法进行改写,新的加密算法RCX。

    最近研究JWT算法, JWT由header.payload.signature三个部分组成,payload是非加密的,一些敏感信息能被别人非法获得,必要时候要加密. 加密算法中,RC4算法的速度可以达 ...

  2. WebSphere禁用SSLv3和RC4算法教程

    WebSphere经常会报“SSL 3.0 POODLE攻击信息泄露”和"SSL/TLS 受诫礼(BAR-MITZVAH)攻击"两个漏洞,前者建议禁用SSL算法后者建议禁用RC4算 ...

  3. lua rc4算法实现

    由于项目需要,用python django写restful接口遇到瓶颈,python django+uwsgi处理请求是会阻塞的, 如果阻塞请求不及时处理,会卡住越来越多的其它的请求,导致越来越多的5 ...

  4. (转)WebSphere禁用SSLv3和RC4算法教程

    原文:https://www.cnblogs.com/lsdb/p/7126399.html WebSphere经常会报“SSL 3.0 POODLE攻击信息泄露”和"SSL/TLS 受诫礼 ...

  5. RC4算法的Python实现详注

    刚对RC4算法进行了学习,网上发现https://ju.outofmemory.cn/entry/46753 中作者展示了RC4的python实现,但代码缺乏注释,较为晦涩,因此本文对部分代码进行了注 ...

  6. c# rc4算法,加密解密类

    rc4算法,原理,以密匙生成256位的密匙流,然后以车轮式滚过源数据异或加密. /* * 由SharpDevelop创建. * 用户: YISH * 日期: 04/04/2015 * 时间: 03:0 ...

  7. Weblogic禁用SSLv3和RC4算法教程

    weblogic在启用https时一样会报同WebSphere那样的一SSL类漏洞,中间件修复这些漏洞原理上来说是一样的,只是在具体操作上有着较大的区别. 1. weblogic禁用SSLv3算法 编 ...

  8. Tomcat禁用SSLv3和RC4算法

    1.禁用SSLv3(SSL 3.0 POODLE攻击信息泄露漏洞(CVE-2014-3566)[原理扫描]) 编缉$CATALINA_HOEM/conf/server.xml配置文件,找到https端 ...

  9. (转)SSL/TLS 漏洞“受戒礼”,RC4算法关闭

    原文:https://blog.csdn.net/Nedved_L/article/details/81110603 SSL/TLS 漏洞“受戒礼” 一.漏洞分析事件起因2015年3月26日,国外数据 ...

随机推荐

  1. 新mac 下第一次 安装 mongodb 步骤

    新入手mac,安装mongo步骤记录:不建议使用网上的brew安装方法,因为试了半天没有成功,应该是新版本限制比较多! 从mongodb官网下载mac版本mongo: 1.访问MongoDB官方下载地 ...

  2. JDK1.8 —— 接口定义增强

    使用default和static定义接口方法 JDK1.8(jre8)以后,接口中不在仅仅只允许定义抽象方法,开始允许定义普通方法了:而普通方法需要用default声明. interface IMes ...

  3. maven 学习---Maven项目文档

    本教程将教你如何一步到位创建应用程序的文档.因此,让我们开始,到  C:/MVN 创建java应用程序consumerBanking. OpenconsumerBanking文件夹,然后执行以下命令m ...

  4. 【原创】Airflow调用talend

    核心原理 因为talend job build出来是一个可直接运行的程序,可以通过shell命名启动job进程,因此可以使用airflow的bashoperator调用生成好的talend job包里 ...

  5. day 35

    目录 单表操作 分组 group by having order by limit 使用顺序 多表操作 外键 一对多 多对多 一对一 多表联查 单表操作 分组 group by 分组指的是:将所有记录 ...

  6. Bayesian Optimization使用Hyperopt进行参数调优

    超参数优化 Bayesian Optimization使用Hyperopt进行参数调优 1. 前言 本文将介绍一种快速有效的方法用于实现机器学习模型的调参.有两种常用的调参方法:网格搜索和随机搜索.每 ...

  7. 【Spring Data JPA篇】JPA的底层原理(二)

    一.接口继承结构 二.底层原理

  8. Django简介(MVC、MTV)

    Django简介 MVC Model(模型)- 应用程序中处理数据逻辑部分且与数据库交互,用于存取数据的部分 View(视图)- 用于处理后的数据界面展示,且视图通常是由模型数据创建的,是用户看到并与 ...

  9. CF1234A Equalize Prices

    洛谷 CF1234A Equalize Prices Again 洛谷传送门 题目描述 You are both a shop keeper and a shop assistant at a sma ...

  10. GDB 调试C++

    原来比较熟悉用gdb调试C程序,没有用过gdb调试C++程序,原理上没有什么区别.在形式上有一些区别,因为C++支持名字空间和class等机制,把函数的可见域做了隔离. 拿envoy的代码作个例子: ...