Help Center/ KooGallery/ Access Guide/ SaaS Access Guide V1.0 (Existing Products)/ Code Example (Java)/ ISV Server Decrypting the Mobile Number and Email Address
Updated on 2022-06-22 GMT+08:00

ISV Server Decrypting the Mobile Number and Email Address

The following figure shows the code invocation.

/**

*

* Decrypt a mobile number or an email address.

* @param key --Key

* @param str --Ciphertext

* @param encryptLength --Length of the encrypted content

* @return --Decryption result

*/

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: Troubleshooting

}

return result;

}

return null;

}

/**

* Decrypt AES-CBC-encrypted content.

* @param content --Original content

* @param key --Key

* @param iv --IV

* @return --Decryption result

* @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;

}