摘要 RC4加密解密算法是工具類是大名鼎鼎的 RSA三人組中的頭號人物Ron Rivest設計的,可以有效抵御暴力搜索密鑰的攻擊。鑒於此,提供一個由Java語言實現的工具類。
前言
RC4加密算法是大名鼎鼎的 RSA三人組中的頭號人物Ron Rivest在1987年設計的密鑰長度可變的流加密算法簇。之所以稱其為簇,是因為其核心部分的S-box長度可為任意長度,但一般為256字節。RC4算法是一種對稱加密算法,所謂對稱加密,就是加密和解密的過程是一樣的。
RC4算法的特點是算法簡單,執行速度快。並且密鑰長度是可變的,可變范圍為1-256字節(8-2048比特),在現在技術支持的前提下,當密鑰長度為128bit時,用暴力法搜索密鑰已經不太可行,所以能夠預見RC4的密鑰范圍仍然能夠在今后相當長的時間里抵御暴力搜索密鑰的攻擊。實際上,現在也沒有找到對於128bit密鑰長度的RC4加密算法的有效攻擊方法。
算法實現
package com.eg.wiener.utils;
import java.io.UnsupportedEncodingException;
import java.util.UUID;
/**
* RC4加解密算法工具類
*
* @author Wiener
* @date 2020/11/17
*/
public class RC4Util {
private static String CHARSET = "UTF-8";
public static String decry_RC4(byte[] data, String key) {
if (data == null || key == null) {
return null;
}
return asString(RC4Base(data, key));
}
/**
* 解密
* @param data
* @param key
* @return
*/
public static String decry_RC4(String data, String key) {
if (data == null || key == null) {
return null;
}
return new String(RC4Base(HexString2Bytes(data), key));
}
/**
* 加密,返回字節流
* @param data
* @param key
* @return
*/
public static byte[] encry_RC4_byte(String data, String key) {
if (data == null || key == null) {
return null;
}
byte b_data[] = data.getBytes();
return RC4Base(b_data, key);
}
/**
* 加密,返回密文
* @param data 明文
* @param key 密鑰
* @return 密文
*/
public static String encry_RC4_string(String data, String key) {
if (data == null || key == null) {
return null;
}
return toHexString(asString(encry_RC4_byte(data, key)));
}
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();
}
private static byte[] initKey(String aKey) {
byte[] b_key = null;
try {
b_key = aKey.getBytes(CHARSET);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
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;
}
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表示十六進制
}
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;
}
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;
}
private static byte[] RC4Base(byte[] input, String mKkey) {
int x = 0;
int y = 0;
byte key[] = initKey(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;
}
public static void main(String[] args) {
String key = "myKey";
String encryStr = encry_RC4_string(String.format("%03d", 12333201), key);
System.out.println(encryStr);
System.out.println(decry_RC4(encryStr, key));
}
/**
* 生成6位密文
* @param data 明文,可以為用戶id等
* @param key 密鑰
* @return
*/
public static String m4Rc4Encrypt(int data, String key) {
if (null == key) {
key = UUID.randomUUID().toString();
}
return encry_RC4_string(String.format("%03d", data), key);
// String.format("%03d", i) 主要實現位數不夠前面補零。如果一個數字不超過3位,則會在其前面補零以到達規定的3位數,其中0被填充到缺省位,3代表規定位數 d代表是整型。
}
}
結束語
歡迎點贊閱讀,一同學習交流RC4加密解密算法;若有疑問,請在文章下方留下你的神評妙論!