更新时间:2023-12-04 GMT+08:00

ISV Server解密手机号和邮箱

代码调用如下图所示。

/**

*

* 解密手机号码或邮箱

* @param key 秘钥

* @param str 密文

* @param encryptLength 加密长度

* @return 解密结果

*/

public static String decryptMobilePhoneOrEMail(String key, String str, int encryptLength)

{

if(null != str && str.length() > 16)

{

String iv = str.substring(0, 16);

String encryptStr = str.substring(16);

String result = null;

try

{

result = decryptAESCBCEncode(encryptStr,

key,

iv,

encryptLength);

}

catch (InvalidKeyException | NoSuchAlgorithmException

| NoSuchPaddingException | InvalidAlgorithmParameterException

| IllegalBlockSizeException | BadPaddingException e)

{

//TODO:异常处理

}

return result;

}

return null;

}

/**

* 解密AES CBC

* @param content 原文

* @param key 秘钥

* @param iv 盐值

* @return 解密结果

* @throws BadPaddingException

* @throws IllegalBlockSizeException

* @throws InvalidAlgorithmParameterException

* @throws NoSuchPaddingException

* @throws NoSuchAlgorithmException

* @throws InvalidKeyException

*/

public static String decryptAESCBCEncode(String content, String key,

String iv, int encryptType) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException

{

if (StringUtils.isEmpty(content) || StringUtils.isEmpty(key)

|| StringUtils.isEmpty(iv))

{

return null;

}

return new String(decryptAESCBC(Base64.decodeBase64(content.getBytes()),

key.getBytes(),

iv.getBytes(),encryptType));

}

public static byte[] decryptAESCBC(byte[] content, byte[] keyBytes,

byte[] iv, int encryptType) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException

{

KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");

SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");

secureRandom.setSeed(keyBytes);

keyGenerator.init(encryptType, secureRandom);

SecretKey key = keyGenerator.generateKey();

Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv));

byte[] result = cipher.doFinal(content);

return result;

}