代码语言:javascript复制
private static final String Algorithm = "DESede"; //3DES算法
private static byte[] ivs = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 };
private static IvParameterSpec iv = new IvParameterSpec(ivs);
/************************************************************
Function: // encryptMode(byte[] src,byte[] key)
Description: // 3DES_CBC_EN
Input: // src-源数据(byte[]) key-加密秘钥(byte[])
Output: // 加密后的数据
Return: // byte[]
*************************************************************/
public static byte[] encryptMode(byte[] src, byte[] key) {
try {
System.out.println("没到8bytes:" Util.byteArrayToHexString(src));
SecretKey deskey = new SecretKeySpec(key, Algorithm); // 生成密钥21
Cipher c1 = Cipher.getInstance("DESede/CBC/NoPadding"); // 实例化负责加密/解密的Cipher工具类22
c1.init(Cipher.ENCRYPT_MODE, deskey, iv); // 初始化为加密模式23
return c1.doFinal(src);
} catch (java.security.NoSuchAlgorithmException e1) {
e1.printStackTrace();
} catch (javax.crypto.NoSuchPaddingException e2) {
e2.printStackTrace();
} catch (java.lang.Exception e3) {
e3.printStackTrace();
}
return null;
}
/***********************************************************************
* Function: // decryptMode(byte[] src,byte[] key) Description: //
* 3DES_CBC_DE Input: // src-源数据(byte[]) key-解密秘钥(byte[]) Output: //
* 解密后的数据 Return: // byte[]
**********************************************************************/
public static byte[] decryptMode(byte[] src, byte[] key) {
try {
SecretKey deskey = new SecretKeySpec(key, Algorithm);
Cipher c1 = Cipher.getInstance("DESede/CBC/NoPadding");
c1.init(Cipher.DECRYPT_MODE, deskey, iv); // 初始化为解密模式44
return c1.doFinal(src);
} catch (java.security.NoSuchAlgorithmException e1) {
e1.printStackTrace();
} catch (javax.crypto.NoSuchPaddingException e2) {
e2.printStackTrace();
} catch (java.lang.Exception e3) {
e3.printStackTrace();
}
return null;
}