更新时间:2025-11-13 gmt 08:00
isv server对资源开通后的用户名和密码加密-j9九游会登录
代码调用如下图所示。

/** * 对资源开通后,返回的用户名和密码进行加密 * @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 仅支持128、256长度 * @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 仅支持128、256长度 * @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; }
父主题:
相关文档
意见反馈
文档内容是否对您有帮助?
提交成功!非常感谢您的反馈,我们会继续努力做到更好!
您可在查看反馈及问题处理状态。
系统繁忙,请稍后重试
如您有其它疑问,您也可以通过华为云社区问答频道来与我们联系探讨