public String encrypt(String str) { try { // Encode the string into bytes using utf-8 byte[] utf8 = str.getBytes("UTF8");
// Encrypt byte[] enc = ecipher.doFinal(utf8);
// Encode bytes to base64 to get a string return new sun.misc.BASE64Encoder().encode(enc); //return new String(enc); } catch (javax.crypto.BadPaddingException e) { e.printStackTrace(); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (java.io.IOException e) { e.printStackTrace(); } return null; }
public String decrypt(String str) { try { // Decode base64 to get bytes byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(str); //byte[] dec = str.getBytes();
import java.io.*;
import javax.crypto.spec.IvParameterSpec;
import java.security.spec.*;public class DesEncrypter {
Cipher ecipher;
Cipher dcipher;
DesEncrypter(SecretKey key) {
byte iv[] = ("123@thIsiSakEy").getBytes();
AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv);
try {
ecipher = Cipher.getInstance("DES");
dcipher = Cipher.getInstance("DES");
//ecipher.init(Cipher.ENCRYPT_MODE, key);
//dcipher.init(Cipher.DECRYPT_MODE, key);
ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
} catch (javax.crypto.NoSuchPaddingException e) {
e.printStackTrace();
} catch (java.security.NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (java.security.InvalidKeyException e) {
e.printStackTrace();
} catch (java.security.InvalidAlgorithmParameterException e) {
e.printStackTrace();
}
}
public String encrypt(String str) {
try {
// Encode the string into bytes using utf-8
byte[] utf8 = str.getBytes("UTF8");
// Encrypt
byte[] enc = ecipher.doFinal(utf8);
// Encode bytes to base64 to get a string
return new sun.misc.BASE64Encoder().encode(enc);
//return new String(enc);
} catch (javax.crypto.BadPaddingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (java.io.IOException e) {
e.printStackTrace();
}
return null;
}
public String decrypt(String str) {
try {
// Decode base64 to get bytes
byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(str);
//byte[] dec = str.getBytes();
// Decrypt
byte[] utf8 = dcipher.doFinal(dec);
// Decode using utf-8
return new String(utf8, "UTF8");
} catch (javax.crypto.BadPaddingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (java.io.IOException e) {
e.printStackTrace();
}
return null;
}
public static void main (String args[]) {
try {
SecretKey key = KeyGenerator.getInstance("DES").generateKey(); byte[] keyBytes = key.getEncoded();
int numBytes = keyBytes.length;
File keyFile = new File("myFile.key");
FileOutputStream fos = new FileOutputStream(keyFile);
fos.write(keyBytes, 0, numBytes);
DesEncrypter encrypter = new DesEncrypter(key);
current = System.currentTimeMillis();
String orginalString = "hehexixihaha";
System.out.println("Orginal String is " + orginalString);
String encrypted = encrypter.encrypt(orginalString);
System.out.println("after encrypt " + encrypted);
System.out.println("encrypt key " + (current - last));
} catch (Exception e) {
e.printStackTrace();
}
}
}