Base64 encryption and decryption
Package com. City. util;
Import java. Security. messagedigest;
Import java. Security. nosuchalgorithmexception;
Import java. util. date;
Import net. SourceForge. jtds. util. md5digest;
Import sun. Misc. base64decoder;
Import sun. Misc. base64encoder;
Public class baseutil {
/**
* Base64 Encryption
* @ Author wangxinying
* @ Param SRC
* @ Return
* @ Throws exception
*/
Public String base64encoder (string SRC) throws exception {
Base64encoder encoder = new base64encoder ();
Return encoder. encode (SRC. getbytes ("utf8 "));
}
/**
* Base64 decryption
* @ Author wangxinying
* @ Param dest
* @ Return
* @ Throws exception
*/
Public String base64decoder (string DEST) throws exception {
Base64decoder decoder = new base64decoder ();
Return new string (decoder. decodebuffer (DEST), "utf8 ");
}
Public String md5encode (){
String STR = "";
Try {
Messagedigest MD5 = messagedigest. getinstance ("MD5 ");
STR = new string (md5.digest ());
} Catch (nosuchalgorithmexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
Return STR;
}
Public static void main (string [] ARGs) throws exception {
Baseutil u = new baseutil ();
String SD = "1234567890000000" + "," + "1234 ";
String UD = U. base64encoder (SD );
String ue = U. base64decoder ("mtm1mte1mdywotkwnix1c2vyatey ");
System. Out. println (UE );
Date da = new date ();
String S = da. gettime () + "";
String P = S + "," + "useri12 ";
System. Out. println (P );
String ud1 = U. base64encoder (P );
System. Out. println (s );
System. Out. println (UE );
System. Out. println (ud1 );
}
}
Des encryption and decryption
Package com. City. util;
Import java. Security. invalidkeyexception;
Import java. Security. nosuchalgorithmexception;
Import java. Security. spec. invalidkeyspecexception;
Import javax. crypto. badpaddingexception;
Import javax. crypto. cipher;
Import javax. crypto. illegalblocksizeexception;
Import javax. crypto. nosuchpaddingexception;
Import javax. crypto. secretkey;
Import javax. crypto. secretkeyfactory;
Import javax. crypto. spec. deskeyspec;
Import org. Apache. log4j. Logger;
Import com.sun.org. Apache. xml. Internal. Security. Exceptions. base64decodingexception;
Import com.sun.org. Apache. xml. Internal. Security. utils. base64;
Public class desedeutil {
Private Static final logger log = logger. getlogger (desedeutil. Class );
Private Static final string B = "mcityportaldd ";
Private Static final string encryalgorithm = "des/ECB/pkcs5padding ";
// Private Static byte [] B = new byte [] {-22, 87,-45, 67,-82,-62,-5, 25 };
Private Static secretkey key;
Public static secretkey getkey (){
Secretkeyfactory keyfactory;
Try {
Deskeyspec = new deskeyspec (B. getbytes ());
Keyfactory = secretkeyfactory. getinstance ("des ");
Key = keyfactory. generatesecret (deskeyspec );
Return key;
} Catch (nosuchalgorithmexception E1 ){
Log. Error (e1.getmessage (), E1 );
} Catch (invalidkeyexception e ){
Log. Error (E. getmessage (), e );
} Catch (invalidkeyspecexception e ){
Log. Error (E. getmessage (), e );
}
Return NULL;
}
Public static byte [] encry (byte [] Message ){
Try {
If (Key = NULL)
Key = getkey ();
Cipher c = cipher. getinstance (encryalgorithm );
C. INIT (Cipher. encrypt_mode, key );
Byte [] encry = C. dofinal (Message );
Return encry;
} Catch (nosuchalgorithmexception e ){
Log. Error (E. getmessage (), e );
} Catch (nosuchpaddingexception e ){
Log. Error (E. getmessage (), e );
} Catch (invalidkeyexception e ){
Log. Error (E. getmessage (), e );
} Catch (illegalblocksizeexception e ){
Log. Error (E. getmessage (), e );
} Catch (badpaddingexception e ){
Log. Error (E. getmessage (), e );
}
Return NULL;
}
Public static byte [] decry (byte [] Message ){
Try {
If (Key = NULL)
Key = getkey ();
Cipher c = cipher. getinstance (encryalgorithm );
C. INIT (Cipher. decrypt_mode, key );
Byte [] decry = C. dofinal (Message );
Return decry;
} Catch (nosuchalgorithmexception e ){
Log. Error (E. getmessage (), e );
} Catch (nosuchpaddingexception e ){
Log. Error (E. getmessage (), e );
} Catch (invalidkeyexception e ){
Log. Error (E. getmessage (), e );
} Catch (illegalblocksizeexception e ){
Log. Error (E. getmessage (), e );
} Catch (badpaddingexception e ){
Log. Error (E. getmessage (), e );
}
Return NULL;
}
Public static string encrymessage (string message ){
Byte [] MSG = message. getbytes ();
Byte [] encrybyte = encry (MSG );
Return base64.encode (encrybyte );
}
Public static string decrymessage (string message) throws exception {
Byte [] MSG = base64.decode (Message );
Byte [] decrybyte = decry (MSG );
String decrymessage = new string (decrybyte );
Return decrymessage;
}
Public static void main (string [] ARGs) throws exception {
String encrymessage = encrymessage ("123456 ");
System. Out. println ("ciphertext =" + encrymessage );
String decrymessage = decrymessage (encrymessage );
System. Out. println ("original =" + decrymessage );
}
}