Java RC4 Encryption and decryption

Source: Internet
Author: User

Package com.*;p Ublic 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 < i++) {Index2 = ((B_key[index1] & 0xff) + (State[i] & 0xff) + index2) & 0xff;byte t MP = 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 means hexadecimal}private static byte[] hexstring2bytes (String src) {int size = Src.length (); byte[] ret = new Byte[si ze/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;by Te[] 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"));}}

Java RC4 Encryption and decryption

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.