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. GIT版本管理工具教程

    目录 GIT版本管理工具教程 一 Git初始化 二 简单指令使用 基本操作 简单总结 三 Git进阶 Git三大区域 Git回滚 Git分支 Git工作流 四 Github代码管理仓库 第一步:注册G ...

  2. Linux命令行基本数据库语句

    -- 数据库的操作 -- 链接数据库 mysql -uroot -p mysql -uroot -pmysql -- 退出数据库 exit/quit/ctrl+d -- sql语句最后需要有分号;结尾 ...

  3. Docker install in Linux

    install command sudo yum install -y yum-utils device-mapper-persistent-data lvm2 sudo yum-config-man ...

  4. 设置 WPF 的全球化语言

    https://stackoverflow.com/questions/7454024/setting-culture-en-in-globally-in-wpf-app Thread.Current ...

  5. (转)Unity与3ds Max的单位关系(使用FBX文件)

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/a1780531/article/deta ...

  6. Django框架(十八)—— drf:序列化组件(serializer)

    序列化组件 # 模型层 from django.db import models class Book(models.Model): nid = models.AutoField(primary_ke ...

  7. ArrayList对象声明& arrayList.size()

    此程序用于测试 :每次for循环内重新定义一个Integer数组,赋值后加入arrayList.由于下一次的Integer对象重新定义,原来的对象是否会被释放? 解答:不会,因为原对象仍被引用(被ar ...

  8. E05 【餐厅】What kind of coffee or tea would you like?

    核心句型 What  kind  of  coffee or tea would you like? 你想喝什么咖啡或者茶? What  would  you like? 你喜欢什么?/你想要什么? ...

  9. android SDK安装Failed to fetch URL http://dl-ssl.google.com/android/repository/addons_list-1.xml出错 解决方案

    更换代理服务器:http://mirrors.neusoft.edu.cn 端口:80 reload 解决 若以上不成功在hosts文件中添加以下内容 #Google主页203.208.46.146 ...

  10. SpringCloud介绍(一)

    Spring Cloud 是一套完整的微服务解决方案,基于 Spring Boot 框架,准确的说,它不是一个框架,而是一个大的容器,它将市面上较好的微服务框架集成进来,从而简化了开发者的代码量. 一 ...