ISV Server Encrypting the Username and Password After Resource Enabling
The following figure shows the overall process of code invocation.
/** * Encrypt the username and password returned after the resources are released. * @param key --Key * @param str --Original content * @param encryptLength --Length of the encrypted content * @return --Encryption result */ 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: Troubleshooting } System.out .println(afterEncryptStr); return iv + afterEncryptStr; } |
/** * Randomly generate a string. * @param length --Length of the randomly generated string * @return --Random string */ public static String getRandomChars(int length) { String randomChars = ""; SecureRandom random = new SecureRandom(); for (int i = 0; i < length; i++) { // Randomly choose letters and digits. if (random.nextInt(2) % 2 == 0) { // Specify whether uppercase or lowercase letters are output. 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 encryption * @param content --Content to be encrypted * @param key --Encryption key * @param iv --IV * @param encryptLength --Only lengths of 128 bits and 256 bits are supported. * @return --Encryption result * @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-bit encryption * @param content --Byte array of the encrypted content * @param keyBytes --Encrypted byte array * @param iv --Byte array of the encrypted IV * @param encryptLength --Only lengths of 128 bits and 256 bits are supported. * @return --Decrypted byte content * @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; } |
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot