/**
* @param args在java中调用sun公司提供的3DES加密解密算法时,需要使
* 用到$JAVA_HOME/jre/lib/目录下如下的4个jar包:
*jce.jar
*security/US_export_policy.jar
*security/local_policy.jar
*ext/sunjce_provider.jar
*/
private static final String Algorithm = "DESede"; //定义加密算法,可用 DES,DESede,Blowfish
public static void main(String[] args) throws UnsupportedEncodingException, GeneralSecurityException {
//添加新安全算法,如果用JCE就要把它添加进去
String srcString ="142fgdsgdsgfd";
// Security.addProvider(new com.sun.crypto.provider.SunJCE());
String password64 ="密码";
String password=Base64Util.decode(password64);
byte[] binaryData = password64.getBytes(Charset.forName("UTF-8"));
byte[] data = Base64.decodeBase64(binaryData);
System.out.println( new String(data, Charset.forName("UTF-8")));
encrypt2(srcString, password64);
encrypt(srcString, password64);
byte[] encryptdata=Base64.decodeBase64("ExDIYVI6z03oaeBOx9bVxQ==");
Cipher dcipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
SecretKey dsecretKey = new SecretKeySpec(data,"DESede");
dcipher.init(Cipher.DECRYPT_MODE, dsecretKey);
byte[] dcrptBytes = dcipher.doFinal(encryptdata);
System.out.println( new String(dcrptBytes, Charset.forName("UTF-8")));
}
private static byte[] encrypt(String srcString, String password64)
throws NoSuchAlgorithmException, NoSuchPaddingException,
InvalidKeyException, IllegalBlockSizeException, BadPaddingException,
UnsupportedEncodingException {
byte[] binaryData = password64.getBytes(Charset.forName("UTF-8"));
byte[] data = Base64.decodeBase64(binaryData);
Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
SecretKey secretKey = new SecretKeySpec(data,"DESede");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] encrptBytes = cipher.doFinal(srcString.getBytes(Charset.forName("UTF-8")));
byte[] base64Bytes = Base64Util.encode(encrptBytes);
System.out.println( new String(base64Bytes, Charset.forName("UTF-8")));
return data;
}
private static void encrypt2(String srcString, String password64)
throws NoSuchAlgorithmException, NoSuchPaddingException,
InvalidKeyException, IllegalBlockSizeException, BadPaddingException,
UnsupportedEncodingException, GeneralSecurityException {
Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
byte[] base64data= Base64Util.decode(password64.getBytes());
DESedeKeySpec dks = new DESedeKeySpec(base64data);
SecretKeyFactory keyFactory= SecretKeyFactory.getInstance("DESede");
SecretKey secretKey = keyFactory.generateSecret(dks);//new SecretKeySpec(data,"DESede");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] encrptBytes = cipher.doFinal(srcString.getBytes(Charset.forName("UTF-8")));
byte[] base64Bytes = Base64Util.encode(encrptBytes);
System.out.println( new String(base64Bytes, Charset.forName("UTF-8")));
}