标签:使用 string exception nbsp ret dom base keygen sed
//加密
public static byte[] des3EncodeECB(byte[] key, byte[] data)
throws Exception {
Key deskey = null;
SecretKeySpec spec = new SecretKeySpec(key,"desede");
SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
deskey = keyfactory.generateSecret(spec);
Cipher cipher = Cipher.getInstance("desede" + "/ECB/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, deskey);
byte[] bOut = cipher.doFinal(data);
return bOut;
}
//解密
public static byte[] des3DecodeECB(byte[] key, byte[] data)
throws Exception {
Key deskey = null;
SecretKeySpec spec = new SecretKeySpec(key,"desede");
SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("DESede");
deskey = keyfactory.generateSecret(spec);
Cipher cipher = Cipher.getInstance("DESede" + "/ECB/NoPadding");
cipher.init(Cipher.DECRYPT_MODE, deskey);
byte[] bOut = cipher.doFinal(data);
return bOut;
}
//加密 public static byte[] encrypt(byte[] targetToByte, String algorithm,String mode, String key) throws Exception { Cipher cipher = Cipher.getInstance(algorithm); cipher.init(Cipher.ENCRYPT_MODE, getKey(key, mode)); byte[] result = cipher.doFinal(targetToByte); // System.out.println("base64:" +encryptByBase64(result)); return result; } //解密 public static byte[] decrypt(byte[] targetToByte, String algorithm,String mode, String key) throws Exception { Cipher cipher = Cipher.getInstance(algorithm); cipher.init(Cipher.DECRYPT_MODE, getKey(key, mode)); byte[] result = cipher.doFinal(targetToByte); // System.out.println("base:" + new String(result,"UTF-8")); return result; } public static Key getKey(String key, String algorithm) { try { KeyGenerator generator = KeyGenerator.getInstance(algorithm); //注意此处将key按照16进制数字解析成byte数组,按需要修改,比如key.getBytes() generator.init(new SecureRandom(HexString2Bytes(key))); return generator.generateKey(); } catch (Exception e) { e.printStackTrace(); } return null; }
标签:使用 string exception nbsp ret dom base keygen sed
原文地址:http://www.cnblogs.com/wsss/p/6925090.html