Import javax. crypto. cipher;
Import javax. crypto. secretkey;
Import javax. crypto. secretkeyfactory;
Import javax. crypto. spec. deskeyspec;
// Import com. modernmedia. mW. Tag. messages;
Import sun. Misc. base64decoder;
Import sun. Misc. base64encoder;
Public class crypt {
// Configure //--------------------------------------------------------------------------------------------
// Obtain the key
Public secretkey getkey (string s) throws exception {
// S = "g8tlglec6oqzxdwge6pdikb8y ";
System. Out. println ("s =" + S );
Char [] Ss = S. tochararray ();
String SSS = "";
For (INT I = 0; I <ss. length; I = I + 2)
{
SSS = SSS + SS [I];
}
Secretkeyfactory kf = secretkeyfactory. getinstance ("des ");
Deskeyspec Ks = new deskeyspec (SSS. substring (). getbytes ());
Secretkey Kd = KF. generatesecret (KS );
Return KD;
}
// Configure //--------------------------------------------------------------------------------------------------
// Return the encrypted string
// Key is the string used to generate the key, and input is the string to be encrypted
Public String getencryptedstring (string key, string input ){
String base64 = "";
Try {
Cipher cipher = cipher. getinstance ("des/ECB/pkcs5padding ");
Cipher. INIT (Cipher. encrypt_mode, getkey (key ));
System. Out. Print ("getkey (key) =" + getkey (key) + "Key =" + key );
Byte [] inputbytes = input. getbytes ("utf8 ");
Byte [] outputbytes = cipher. dofinal (inputbytes );
Base64encoder encoder = new base64encoder ();
Base64 = encoder. encode (outputbytes );
} Catch (exception e ){
Base64 = E. getmessage ();
}
Return base64;
}
// Configure //--------------------------------------------------------------------------------------------------
// Return the decrypted string
// Key is the string used to generate the key, and input is the string to be decrypted
Public String getdecryptedstring (string key, string input ){
String result = NULL;
Try {
Cipher cipher = cipher. getinstance ("des/ECB/pkcs5padding ");
Cipher. INIT (Cipher. decrypt_mode, getkey (key ));
Base64decoder decoder = new base64decoder ();
Byte [] Raw = decoder. decodebuffer (input );
Byte [] stringbytes = cipher. dofinal (raw );
Result = new string (stringbytes, "utf8 ");
} Catch (exception e ){
Result = E. getmessage ();
}
Return result;
}
Public static void main (string [] ARGs ){
Crypt mycrypt = new crypt ();
Try {
// Secretkey skey = mycrypt. getkey ("g8tlglec6oqzxdwge6pdikb8y ");
String Ss = mycrypt. getencryptedstring ("6678912345678906", "fat ");
System. Out. println ("SS =" + SS );
String ss2 = mycrypt. getdecryptedstring ("6678912345678906", SS );
System. Out. println ("ss2 =" + ss2 );
} Catch (exception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
// String Ss = messages. getstring ("sendtomembertag.5 ");
// System. Out. Print (SS );