Java RC4 encryption and decryption, javarc4 encryption and decryption
Package com. *; public class RC4 {public static String decry_RC4 (byte [] data, String key) {if (data = null | key = null) {return null ;} return asString (RC4Base (data, key);} public static String decry_RC4 (String data, String key) {if (data = null | key = null) {return null;} return new String (RC4Base (HexString2Bytes (data), key);} 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);} 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 = 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 ;} 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 indicates hexadecimal} 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 inputStr = "be a good man"; String str = encry_RC4_string (inputStr, "123456"); System. out. println (str); System. out. println (decry_RC4 (str, "123456 "));}}
Use java to implement RC4 encryption, which can be decrypted and provides a high reward.
Import javax. crypto. cipher; import javax. crypto. spec. secretKeySpec; import javax. xml. bind. datatypeConverter; public class Test {public static void main (String [] args) throws Exception {Cipher cipher = Cipher. getInstance ("RC4"); String pwd = "123456"; String ptext = "Hello World"; SecretKeySpec key = new SecretKeySpec (pwd. getBytes ("UTF-8"), "RC4"); cipher. init (Cipher. ENCRYPT_MODE, key); byte [] cdata = cipher. updat E (ptext. getBytes ("UTF-8"); // decrypt the cipher. init (Cipher. DECRYPT_MODE, key); byte [] ddata = cipher. update (cdata); System. out. println ("Password:" + pwd); System. out. println ("plaintext:" + ptext); System. out. println ("ciphertext:" + DatatypeConverter. printHexBinary (cdata); System. out. println ("decryption:" + new String (ddata, "UTF-8");} password: 123456 plaintext: Hello World Hello ciphertext: 489D120B4B1342F30D5B46961D83E12B4875 decryption: hello World, RC4 is not secure. It can only be used for general encryption and cannot be used. Financial and other important occasions.
A third-party package that implements the RC4 encryption algorithm, JAVA
Public class Test {public static String rc4 (String aInput, String aKey) {int [] iS = new int [256]; byte [] iK = new byte [256]; for (int I = 0; I <256; I ++) iS [I] = I; int j = 1; for (short I = 0; I <256; I ++) {iK [I] = (byte) aKey. charAt (I % aKey. length ();} j = 0; for (int I = 0; I <255; I ++) {j = (j + iS [I] + iK [I]) % 256; int temp = iS [I]; iS [I] = iS [j]; iS [j] = temp;} int I = 0; j = 0; char [] iInputChar = aInput. toCharArray (); char [] iOutputChar = new char [iInputChar. length]; for (short x = 0; x <iInputChar. length; x ++) {I = (I + 1) % 256; j = (j + iS [I]) % 256; int temp = iS [I]; iS [I] = iS [j]; iS [j] = temp; int t = (iS [I] + (iS [j] % 256) % 256; int iY = iS [t]; char iCY = (char) iY; iOutputChar [x] = (char) (iInputChar [x] ^ iCY );} return new String (iOutputChar);}/*** @ param args */public static void main (String [] args) {// TODO Auto-generated method stub String inputStr = "be a good man with a similar speed"; String key = "abcdefg"; String str = rc4 (inputStr, key ); // print the encrypted string System. out. println (str); // print the decrypted string System. out. println (rc4 (str, key ));}}