Tried several times through,,,
Package com. The demo;
The import javax.mail. Crypto. Cipher;
The import javax.mail. Crypto. KeyGenerator;
Import the Java. Security. The Key;
Import the Java. Security. SecureRandom;
Public class Arithmetic {
The static Key Key;
//key
Public static final String DEFAULT_SECRET_KEY="% 2 $RFV qaz3wsx3edc TGB ^ YHN& UJMasdgf3asdwe3qws1233gawe ";
The static {
Key=getKey (DEFAULT_SECRET_KEY);
}
Public static Key getKey (String strKey) {
Try {
KeyGenerator _generator=KeyGenerator. GetInstance (" DES ");
_generator. Init (new SecureRandom (strKey getBytes ()));
Key=_generator. GenerateKey ();
_generator=null;
} the catch (Exception e) {
e.printStackTrace();
}
return key;
}
Public static String getEncString (String strMing) {
Byte [] byteMi=null;
Byte [] byteMing=null;
String strMi="";
Try {
System. The out. Println (" 1 "+ strMing. GetBytes ());
System. The out. Println (" 2 "+ getEncCode (strMing. GetBytes ()));
System. The out. Println (" 3. "+ byte2hex (getEncCode (strMing. GetBytes ())));
Return byte2hex (getEncCode (strMing getBytes ()));
//byteMing=strMing. GetBytes (" UTF8 ");
//byteMi=this. GetEncCode (byteMing);
//strMi=new String (byteMi, "UTF8");
} the catch (Exception e) {
e.printStackTrace();
} the finally {
ByteMing=null;
ByteMi=null;
}
Return strMi;
}
Public static String getDesString (String strMi) {
Byte [] byteMing=null;
Byte [] byteMi=null;
String strMing="";
Try {
Return a new String (getDesCode (hex2byte (strMi getBytes ())));
//byteMing=this. GetDesCode (byteMi);
//strMing=new String (byteMing, "UTF8");
} the catch (Exception e) {
e.printStackTrace();
} the finally {
ByteMing=null;
ByteMi=null;
}
Return strMing;
}
Private static byte [] getEncCode (byte [] byteS) {
Byte [] byteFina=null;
Cipher Cipher;
Try {
Cipher=cipher. GetInstance (" DES ");
Cipher. The init (cipher ENCRYPT_MODE, key);
ByteFina=cipher. DoFinal (byteS);
} the catch (Exception e) {
e.printStackTrace();
} the finally {
Cipher=null;
}
Return byteFina;
}
Private static byte [] getDesCode (byte [] byteD) {
Cipher Cipher;
Byte [] byteFina=null;
Try {
Cipher=cipher. GetInstance (" DES ");
Cipher. The init (cipher DECRYPT_MODE, key);
ByteFina=cipher. DoFinal (byteD);
} the catch (Exception e) {
e.printStackTrace();
} the finally {
Cipher=null;
}
Return byteFina;
}
Public static String byte2hex (byte [] b) {
String hs="";
String STMP="";
For (int n=0; N & lt; B.l ength; N++) {
STMP=(Java lang. Integer. ToHexString (b [n] & amp; 0 XFF));
If (STMP) length ()==1)
The hs=hs + "0" + STMP;
The else
The hs=hs + STMP;
}
Return the hs. ToUpperCase ();
}
Public static byte [] hex2byte (byte [] b) {
If ((b.l ength % 2)!=0)
Throw new IllegalArgumentException (" length is not an even number ");
Byte [] b2=new byte [b.l ength/2);
For (int n=0; N & lt; B.l ength; N +=2) {
String item=new String (b, n, 2);
B2=(byte) [n/2] Integer. The parseInt (item, 16);
}
Return b2.
}
Public static void main (String [] args) {
String strEnc=Arithmetic. GetEncString (" wangmn ");
System. The out. Println (strEnc);
String strDes=Arithmetic. GetDesString (strEnc);
System. The out. Println (strDes);
}
}
CodePudding user response:
https://www.cnblogs.com/zywf/p/7565103.htmlDon't need to follow the code translation has a ready-made