/**
* DES加解密demo
*/
public class DESUtils {
private static Key key;
private static String KEY_STR="rchm";
static{
try{
KeyGenerator generator = KeyGenerator.getInstance("DES");
generator.init(new SecureRandom(KEY_STR.getBytes()));
key = generator.generateKey();
generator = null;
}catch(Exception e){
throw new RuntimeException(e);
}
}
/**
* 获得des加密后的字符串
* @param str
* @return String
*/
public static String encrypt(String str){
try{
BASE64Encoder encode = new BASE64Encoder();
byte[] strBytes = str.getBytes("UTF8");
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] encryptStrBytes = cipher.doFinal(strBytes);
return encode.encode(encryptStrBytes);
}catch(Exception e){
throw new RuntimeException(e);
}
}
/**
* 获得解密后的字符串
* @param str
* @return String
*/
public static String decrypt(String str){
BASE64Decoder decode = new BASE64Decoder();
try{
byte[] strBytes = decode.decodeBuffer(str);
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] decryptStrBytes = cipher.doFinal(strBytes);
return new String(decryptStrBytes,"UTF8");
}catch(Exception e){
throw new RuntimeException(e);
}
}
public static void main(String[] args) {
String srcStr = "my+N-a;me";
String encryptStr = encrypt(srcStr);
String desStr = decrypt(encryptStr);
System.out.println("strSrc:"+srcStr);
System.out.println("encryptStr:"+encryptStr);
System.out.println("desStr:"+desStr);
System.out.println(srcStr==desStr);
System.out.println(srcStr.equals(desStr));
}
}
* DES加解密demo
*/
public class DESUtils {
private static Key key;
private static String KEY_STR="rchm";
static{
try{
KeyGenerator generator = KeyGenerator.getInstance("DES");
generator.init(new SecureRandom(KEY_STR.getBytes()));
key = generator.generateKey();
generator = null;
}catch(Exception e){
throw new RuntimeException(e);
}
}
/**
* 获得des加密后的字符串
* @param str
* @return String
*/
public static String encrypt(String str){
try{
BASE64Encoder encode = new BASE64Encoder();
byte[] strBytes = str.getBytes("UTF8");
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] encryptStrBytes = cipher.doFinal(strBytes);
return encode.encode(encryptStrBytes);
}catch(Exception e){
throw new RuntimeException(e);
}
}
/**
* 获得解密后的字符串
* @param str
* @return String
*/
public static String decrypt(String str){
BASE64Decoder decode = new BASE64Decoder();
try{
byte[] strBytes = decode.decodeBuffer(str);
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] decryptStrBytes = cipher.doFinal(strBytes);
return new String(decryptStrBytes,"UTF8");
}catch(Exception e){
throw new RuntimeException(e);
}
}
public static void main(String[] args) {
String srcStr = "my+N-a;me";
String encryptStr = encrypt(srcStr);
String desStr = decrypt(encryptStr);
System.out.println("strSrc:"+srcStr);
System.out.println("encryptStr:"+encryptStr);
System.out.println("desStr:"+desStr);
System.out.println(srcStr==desStr);
System.out.println(srcStr.equals(desStr));
}
}