Import java. security. Security;
Import javax. crypto. Cipher;
Import javax. crypto. KeyGenerator;
Import javax. crypto. SecretKey;
Public class DesEncode {
Private static String Algorithm = "DES"; // defines the encryption Algorithm. DES, DESede, and Blowfish can be used.
Private static boolean debug = false;
Static {
Security. addProvider (new com. sun. crypto. provider. SunJCE ());
}
// Generate the key
Public static byte [] getKey () throws Exception {
Byte [] key = {-50,-46,-78,-69,-42,-86,-75,-64 };
If (debug ){
System. out. println ("generate key:" + byte2hex (key ));
}
Return key;
}
// Encryption
Public static String encode (String input) throws Exception {
SecretKey secret ey = new javax. crypto. spec. SecretKeySpec (getKey (), Algorithm );
If (debug ){
System. out. println ("pre-encryption string:" + input );
System. out. println ("binary string before encryption:" + byte2hex (input. getBytes ()));
}
Cipher c1 = Cipher. getInstance (Algorithm );
C1.init (Cipher. ENCRYPT_MODE, Cipher ey );
Byte [] cipherByte = c1.doFinal (input. getBytes ());
If (debug)
System. out. println ("encrypted binary string:" + byte2hex (cipherByte ));
Return byte2hex (cipherByte );
}
// Decrypt
Public static String decode (String input) throws Exception {
SecretKey secret ey = new javax. crypto. spec. SecretKeySpec (getKey (), Algorithm );
If (debug ){
System. out. println ("pre-decryption information:" + input );
}
Cipher C1 = cipher. getinstance (algorithm );
C1.init (Cipher. decrypt_mode, cipher ey );
Byte [] clearbyte = c1.dofinal (hex2byte (input ));
If (Debug ){
System. Out. println ("decrypted binary string:" + byte2hex (clearbyte ));
System. Out. println ("decrypted string:" + (new string (clearbyte )));
}
Return new string (clearbyte );
}
// Digest of MD5 () information, irreversible
Public static string MD5 (string input) throws exception {
Java. Security. messagedigest ALG = java. Security. messagedigest. getinstance ("MD5"); // or "SHA-1"
If (debug ){
System. out. println ("binary string before Digest:" + byte2hex (input. getBytes ()));
System. out. println ("string before Digest:" + input );
}
Alg. update (input. getBytes ());
Byte [] digest = alg. digest ();
If (debug)
System. out. println ("binary string after digest:" + byte2hex (digest ));
Return byte2hex (digest );
}
// Convert the bytecode into a hexadecimal string
Public static String byte2hex (byte [] B ){
String hs = "";
String stmp = "";
For (int n = 0; n <B. length; n ++ ){
Stmp = (java. lang. Integer. toHexString (B [n] & 0XFF ));
If (stmp. length () = 1)
Hs = hs + "0" + stmp;
Else hs = hs + stmp;
// If (n <B. length-1) hs = hs + ":";
}
Return hs. toUpperCase ();
}
// Convert a hexadecimal string to a bytecode
Public static byte [] hex2byte (String h ){
Byte [] ret = new byte [h. length ()/2];
For (int I = 0; I <ret. length; I ++ ){
Ret [I] = Integer. decode ("#" + h. substring (2 * I, 2 * I + 2). byteValue ();
}
Return ret;
}
Public static void main (String [] args) throws Exception {
Debug = true;
Decode (encode ("test encryption "));
Md5 ("haohao learning ");
}
}