RC4特点:对称加密,安全度高,按字节加密。
密钥长度:128可保证安全性
1、加密
public byte[] encryptWithRC4(String content, String key) throws Exception {
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
secureRandom.setSeed(key.getBytes());
KeyGenerator keyGenerator = KeyGenerator.getInstance("RC4");
keyGenerator.init(secureRandom);
SecretKey secretKey = keyGenerator.generateKey();
Cipher cipher = Cipher.getInstance("RC4");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
return cipher.doFinal(content.getBytes());
}
2、解密
public byte[] decryptWithRC4(String cipherText, String key) throws Exception {
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
secureRandom.setSeed(key.getBytes());
KeyGenerator keyGenerator = KeyGenerator.getInstance("RC4");
keyGenerator.init(secureRandom);
SecretKey secretKey = keyGenerator.generateKey();
Cipher cipher = Cipher.getInstance("RC4");
cipher.init(Cipher.DECRYPT_MODE, secretKey);
return cipher.doFinal(cipherText.getBytes());
}