public class RC4 {
byte[] s = new byte[256];
byte[] key;
byte keylen;// 4 ~ 16
int pi = 0;
int pj = 0; public RC4() {
this.keylen = 16;
randKey();
ksa();
} public RC4(byte keylen) {
this.keylen = keylen;
randKey();
ksa();
} public RC4(byte[] key) {
this.key = key;
keylen = (byte) key.length;
ksa();
} private void randKey() {
// SecureRandom r = new SecureRandom("abc".getBytes());
Random r = new Random(0xdeadbeef);
key = new byte[keylen];
r.nextBytes(key);
} public byte[] getKey() {
return key;
} private void ksa() {
for (int i = 0; i < s.length; i++) {
s[i] = (byte) i;
}
int j = 0;
for (int i = 0; i < s.length; i++) { //使用密钥来交换s的元素
j = ((j + s[i] & 0xff + key[i % keylen] & 0xff) % 256); //重复使用密钥 byte tmp = s[i]; //交换
s[i] = s[j];
s[j] = tmp;
}
} public byte prgaOneByte(byte in) {
pi = pi + 1 % 256;
pj = (pj + s[pi] & 0xff); byte tmp = s[pi];
s[pi] = s[pj];
s[pj] = tmp; int t = (s[pi] & 0xff + s[pj] & 0xff) % 256;
int k = s[t] & 0xff;
return (byte) ((k ^ in & 0xff) & 0xff);
} public void prga(InputStream is, OutputStream os) throws IOException {
int bufsize = 4 * 1024;
byte[] buffer = new byte[bufsize]; // 4K
byte[] outBuffer;
int len = 0;
while ((len = is.read(buffer, 0, bufsize)) != -1) { //读取一块
outBuffer = prga(buffer, 0, len);
os.write(outBuffer);
}
} public byte[] prga(byte[] buffer, int off, int len) throws IOException {
byte[] outBuffer = new byte[buffer.length]; // 4K
for (int i = 0 + off; i < len; i++) { //处理这块
outBuffer[i] = prgaOneByte(buffer[i]);
}
return outBuffer;
} public static void main(String[] args) throws IOException {
String msg = "hello wold!";
byte[] bmsg = msg.getBytes();
byte[] key;
System.out.println("bmsg :" + Arrays.toString(bmsg)); //加密前 RC4 rc4 = new RC4();
key = rc4.getKey();
byte[] ebmsg = rc4.prga(bmsg, 0, bmsg.length);
System.out.println("ebmsg :" + Arrays.toString(ebmsg)); //加密后 RC4 rc42 = new RC4(key);
byte[] debmsg = rc42.prga(ebmsg, 0, ebmsg.length);
System.out.println("debmsg:" + Arrays.toString(debmsg));//解密后
System.out.println("debmsg:" + new String(debmsg));
}
}

输出

bmsg :[, , , , , , , , , , ]
ebmsg :[-, , -, -, -, , -, -, -, , -]
debmsg:[, , , , , , , , , , ]
debmsg:hello wold!

最新文章

  1. CLR 这些年有啥变化吗?
  2. 前端性能优化的另一种方式——HTTP2.0
  3. NOIP2001 一元三次方程求解[导数+牛顿迭代法]
  4. ubuntu安装(owncloud-docker安装)
  5. set集合,是一个无序且不重复的元素集合
  6. 介绍开源的.net通信框架NetworkComms框架之七 数据加密通信
  7. 一个android应用开发的感悟
  8. CheckedListBoxControl 使用
  9. Mysql 系统参数 系统变量 状态变量
  10. Xamarin开发笔记—WebView双项事件调用
  11. [js] 小谈 export (没总结完)
  12. Cannot read property &#39;component&#39; of undefined 即vue-router 0.x转化为2.x
  13. 安卓上用Termux终端模拟器安装MC Forge 1.12.2服务器!
  14. FIVE1
  15. beego框架的最简单登入演示
  16. Yii2的save()方法容易出错的地方
  17. C和C指针小记(十七)-使用结构和指针-链表
  18. nginx+python+windows 开始
  19. .NET使用Task动态创建多任务多线程并行程序计算Redis集群keys计算
  20. STM8L外部中断 为何 死循环 寄存器操作

热门文章

  1. docker-compose 使用自定义网络并绑定 IP
  2. SQL Serve里DBA要去改变的3个配置选项
  3. Java学习笔记(7)---流(Stream),文件(File)
  4. go语言设计模式之state
  5. vue跨域
  6. misc-3-1
  7. WINDOWS代理服务器搭建 - Apache httpd
  8. AcWing 836. 合并集合
  9. luoguP3246 [HNOI2016]序列
  10. typing模块