Des.java directly on source code
Package Com.demo;
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 DES {
Key key;
Public DES (String str) {
Setkey (str);//Generate Key
}
Public DES () {
Setkey ("Siyue_qi");
}
/**
* Generate key based on parameters
*/
public void Setkey (String strkey) {
try {
Keygenerator _generator = keygenerator.getinstance ("DES");
_generator.init (New SecureRandom (Strkey.getbytes ()));
This.key = _generator.generatekey ();
_generator = null;
catch (Exception e) {
throw new RuntimeException ("Error Initializing Sqlmap class." Cause: "+ e);
}
}
/**
* Encrypted string plaintext input, string ciphertext output
*/
public string getencstring (string strming) {
byte[] Bytemi = null;
byte[] byteming = null;
String strmi = "";
Base64encoder base64en = new Base64encoder ();
try {
byteming = Strming.getbytes ("UTF8");
Bytemi = This.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 entered in string cipher text, string plaintext output
* @param strmi
* @return
*/
public string getdesstring (string strmi) {
Base64decoder base64de = new Base64decoder ();
byte[] byteming = null;
byte[] Bytemi = null;
String strming = "";
try {
Bytemi = Base64de.decodebuffer (Strmi);
byteming = This.getdescode (Bytemi);
strming = new String (byteming, "UTF8");
catch (Exception e) {
throw New RuntimeException (
"Error initializing Sqlmap class." Cause: "+ e);
finally {
BASE64DE = null;
byteming = null;
Bytemi = null;
}
return strming;
}
/**
* Encrypt with byte[] plaintext input, byte[] ciphertext output
* @param ByteS
* @return
*/
Private 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;
}
/**
* Decrypt with byte[] ciphertext input, in byte[] Clear text output
* @param byted
* @return
*/
Private 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;
}
public static void Main (String args[]) {
Des des = new des ();
Setting keys
Des.setkey ("12345678 =");
String str1 = "123";
DES encryption
String str2 = des.getencstring (STR1);
String destr = des.getdesstring (STR2);
System.out.println ("Ciphertext:" +STR2);
Des decryption
System.out.println ("plaintext:" +destr);
}
}