Home > Back-end >  The RC4 encryption algorithm utility class
The RC4 encryption algorithm utility class

Time:03-02

Package com. Heyi. Util.


The import org.springframework.stereotype.Com ponent;

import java.io.UnsupportedEncodingException;
Import the Java. Util. UUID;

/* *
* the RC4 encryption algorithm utility class
*
* @ author Wiener
* @ date 2020/11/17
*/
@ Component
Public class RC4Util {
Private static String CHARSET="utf-8";

Public static String decry_RC4 (byte [] data, String key) {
If (data=null https://bbs.csdn.net/topics/=| | key==null) {
return null;
}
Return asString (RC4Base (data, key));
}

/* *
* decryption
* @ param data
* @ param key
* @ return
*/
Public static String decry_RC4 (String data, String key) {
If (data=null https://bbs.csdn.net/topics/=| | key==null) {
return null;
}
Return a new String (RC4Base (HexString2Bytes (data), key));
}

/* *
* encryption, returns byte stream
* @ param data
* @ param key
* @ return
*/
Public static byte [] encry_RC4_byte (String data, String key) {
If (data=null https://bbs.csdn.net/topics/=| | key==null) {
return null;
}
Byte b_data []=data. GetBytes ();
Return RC4Base (b_data, key);
}

/* *
* encryption, return ciphertext
* @ param data clear
* @ param key key
* @ return ciphertext
*/
Public static String encry_RC4_string (String data, String key) {
If (data=null https://bbs.csdn.net/topics/=| | 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 strbuf. Append ((char) [I]);
}
Return strbuf. ToString ();
}

Private static byte [] initKey (String aKey) {
Byte [] b_key=null;
Try {
B_key=aKey. GetBytes (CHARSET);
} the catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
Byte state []=new byte [256].

For (int I=0; i <256; I++) {
The 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] & amp; 0 XFF) + (state [I] & amp; 0 XFF) + index2) & amp; 0 XFF.
Byte TMP=state [I];
The state [I]=state [index2];
The state [index2]=TMP;
Index1=(index1 + 1) % b_key. The length;
}
return state;
}

Private static String toHexString (String s) {
String STR="";
For (int I=0; i Int ch=(int) s.c harAt (I);
String s4=Integer. ToHexString (ch & amp; 0 XFF);
If (s4. The length ()==1) {
S4='0' + s4.
}
STR=STR + s4.
}
Return the STR;//hex 0 x said
}

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 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 (" 0 "x + new String (new Byte [] {src0})). ByteValue ();
_b0=(char) (_b0 & lt; <4);
Char _b1=(char) Byte. Decode (" 0 "x + 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 X=(x + 1) & amp; 0 XFF.
Y=((key [x] & amp; 0 XFF) + y) & amp; 0 XFF.
Byte TMP=key [x];
The key [x]=key [y];
The key [y]=TMP;
XorIndex=((key [x] & amp; 0 XFF) + (key [y] & amp; 0 XFF) & amp; 0 XFF.
The result [I]=(byte) (input [I] ^ key [xorIndex]);
}
return result;
}

Public static void main (String [] args) {
The String key="myKey";
String encryStr=encry_RC4_string (String format (" % 3 d ", 12333201), a key);
System. The out. Println (encryStr);
System. The out. Println (decry_RC4 (encryStr, key));
}

/* *
* generate six ciphertext
* @ param data express, can for the user id, etc.
* @ param key key
* @ return
*/
Public static String m4Rc4Encrypt (int data, String key) {
If (null==key) {
Key=UUID. RandomUUID (). The toString ();
}
Return encry_RC4_string (String format (" % 3 d ", data), the key).
03//the String. Format (" % d ", I) in front of the main implementation digits enough padding, if a number is not more than three, will be in front of the zero padding to reach 3 digits, where 0 is populated by default, 3 represents provisions digits d integer,
}
}
  • Related