Package JP. co. sumitomocorp. intra1china. Ess. batch. event;
Import java. Io. ioexception;
Import java. Security. Key;
Import java. Security. securerandom;
Import javax. crypto. cipher;
Import javax. crypto. keygenerator;
Import sun. Misc. base64decoder;
Import sun. Misc. base64encoder;
Public class desencrypter {
Static key;
Public static void main (string [] ARGs ){
// Obtain the Encrypted Key
String testdata = "mjgwmdawmdi = ";
// DES encryption
// Base64decoder decoder = new base64decoder ();
// Byte [] decodedbytes = NULL;
// Try {
// Decodedbytes = decoder. decodebuffer (testdata );
//} Catch (ioexception e ){
// E. printstacktrace ();
//}
// New string (decodedbytes );
String enstr = getdesstring (testdata );
System. Out. println ("ciphertext:" + enstr );
/// Des decryption
// String destr = getdesstring (enstr );
// System. Out. println ("plaintext:" + destr );
}
/**
* Encrypted string plaintext input and string ciphertext output
*/
Public static string getencstring (string strming ){
// Ciphertext byte array
Byte [] bytemi = NULL;
// Plaintext byte array
Byte [] byteming = NULL;
// Ciphertext output
String strmi = "";
Base64encoder base64en = new base64encoder ();
Try {
Byteming = strming. getbytes ("utf8 ");
Bytemi = getenccode (byteming );
Strmi = base64en. encode (bytemi );
} Catch (exception e ){
Throw new runtimeexception ("error initializing sqlmap class. Cause :"
+ E );
} Finally {
Base64en = NULL;
Byteming = NULL;
Bytemi = NULL;
}
Return strmi;
}
/**
* Decryption is input in string ciphertext and output in string plaintext.
*
* @ Param strmi
* @ Return
*/
Public static string getdesstring (string strmi ){
Base64decoder decoder = new base64decoder ();
Byte [] decodedbytes = NULL;
String A = "";
Try {
Decodedbytes = decoder. decodebuffer (strmi );
A = new string (decodedbytes, "UTF-8 ");
} Catch (ioexception e ){
E. printstacktrace ();
}
Return;
}
/**
* Encryption is input in byte [] plaintext, and byte [] ciphertext is output.
*
* @ Param bytes
* @ Return
*/
Private Static byte [] getenccode (byte [] bytes ){
Byte [] bytefina = NULL;
Cipher cipher;
Try {
Cipher = cipher. getinstance ("des ");
Cipher. INIT (Cipher. encrypt_mode, key );
Bytefina = cipher. dofinal (bytes );
} Catch (exception e ){
Throw new runtimeexception ("error initializing sqlmap class. Cause :"
+ E );
} Finally {
Cipher = NULL;
}
Return bytefina;
}
/**
* Decryption is input in byte [] ciphertext and output in byte [] plaintext
*
* @ Param byted
* @ Return
*/
Private Static byte [] getdescode (byte [] byted ){
Cipher cipher;
Byte [] bytefina = NULL;
Try {
Cipher = cipher. getinstance ("des ");
Cipher. INIT (Cipher. decrypt_mode, key );
Bytefina = cipher. dofinal (byted );
} Catch (exception e ){
Throw new runtimeexception ("error initializing sqlmap class. Cause :"
+ E );
} Finally {
Cipher = NULL;
}
Return bytefina;
}
/**
* Generate a key based on the parameter
*/
Public static void setkey (string strkey ){
Try {
Keygenerator _ generator = keygenerator. getinstance ("des ");
_ Generator. INIT (New securerandom (strkey. getbytes ()));
Key = _ generator. generatekey ();
_ Generator = NULL;
} Catch (exception e ){
Throw new runtimeexception ("error initializing sqlmap class. Cause :"
+ E );
}
}
}