Arun MR

Arun MR

  • NA
  • 67
  • 0

AES /GCM/Nopadding Java equivalent encryption decryption sample codec#

Jun 22 2022 4:31 PM

AES /GCM/Nopadding Java equivalent encryption-decryption code in c# required.  Sample Java reference code attached..

 

public static String encrypt(byte[] pText, String key) throws Exception {
byte[] salt = CryptoUtils.getRandomNonce(SALT_LENGTH_BYTE);
byte[] iv = CryptoUtils.getRandomNonce(IV_LENGTH_BYTE);
SecretKey aesKey = CryptoUtils.getAESKey(key.toCharArray(), salt);
Cipher cipher = Cipher.getInstance(ENCRYPT_ALGO);
cipher.init(Cipher.ENCRYPT_MODE, aesKey, new GCMParameterSpec(TAG_LENGTH_BIT, iv));
byte[] cipherText = cipher.doFinal(pText);
byte[] cipherTextWithIvSalt = ByteBuffer.allocate(iv.length + salt.length + cipherText.length)
.put(iv)
.put(salt)
.put(cipherText)
.array();
return Base64.getEncoder().encodeToString(cipherTextWithIvSalt);
}


Answers (1)