文档首页> 云商店> 接入指南> SaaS类商品接入指南V1.0> 代码示例(Java)> ISV Server对资源开通后的用户名和密码加密
更新时间:2023-12-04 GMT+08:00

ISV Server对资源开通后的用户名和密码加密

代码调用如下图所示。

/**

* 对资源开通后,返回的用户名和密码进行加密

* @param key 秘钥

* @param str 原文

* @param encryptLength 加密长度

* @return 加密结果

*/

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

{

String iv = getRandomChars(16);

String afterEncryptStr = "";

try

{

afterEncryptStr = encryptAESCBCEncode(str, key, iv, encryptLength);

}

catch (InvalidKeyException | NoSuchAlgorithmException

| NoSuchPaddingException | InvalidAlgorithmParameterException

| IllegalBlockSizeException | BadPaddingException e)

{

//TODO:异常处理

}

System.out

.println(afterEncryptStr);

return iv + afterEncryptStr;

}

/**

* 随机生成字符串

* @param length 随机字符串的长度

* @return 随机字符串

*/

public static String getRandomChars(int length)

{

String randomChars = "";

SecureRandom random = new SecureRandom();

for (int i = 0; i < length; i++)

{

//字母和数字中随机

if (random.nextInt(2) % 2 == 0)

{

//输出是大写字母还是小写字母

int letterIndex = random.nextInt(2) % 2 == 0 ? 65 : 97;

randomChars += (char) (random.nextInt(26) + letterIndex);

}

else

{

randomChars += String.valueOf(random.nextInt(10));

}

}

return randomChars;

}

/**

* AES CBC 加密

* @param content 加密内容

* @param key 加密秘钥

* @param iv 向量iv

* @param encryptLength 仅支持128256长度

* @return 加密结果

* @throws BadPaddingException

* @throws IllegalBlockSizeException

* @throws InvalidAlgorithmParameterException

* @throws NoSuchPaddingException

* @throws NoSuchAlgorithmException

* @throws InvalidKeyException

*/

public static String encryptAESCBCEncode(String content, String key,

String iv, int encryptLength)

throws InvalidKeyException, NoSuchAlgorithmException,

NoSuchPaddingException, InvalidAlgorithmParameterException,

IllegalBlockSizeException, BadPaddingException

{

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

|| StringUtils.isEmpty(iv))

{

return null;

}

return base_64(

encryptAESCBC(content.getBytes(), key.getBytes(), iv.getBytes(), encryptLength));

}

/**

*

* AES CBC 256位加密

* @param content 加密内容字节数组

* @param keyBytes 加密字节数组

* @param iv 加密向量字节数组

* @param encryptLength 仅支持128256长度

* @return 解密后字节内容

* @throws NoSuchAlgorithmException

* @throws NoSuchPaddingException

* @throws InvalidKeyException

* @throws InvalidAlgorithmParameterException

* @throws IllegalBlockSizeException

* @throws BadPaddingException

*/

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

byte[] iv, int encryptLength)

throws NoSuchAlgorithmException, NoSuchPaddingException,

InvalidKeyException, InvalidAlgorithmParameterException,

IllegalBlockSizeException, BadPaddingException

{

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

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

secureRandom.setSeed(keyBytes);

keyGenerator.init(encryptLength, secureRandom);

SecretKey key = keyGenerator.generateKey();

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

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

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

return result;

}