Skip to content

Instantly share code, notes, and snippets.

@VulBusters
Last active August 9, 2023 12:37
Show Gist options
  • Save VulBusters/436e48d2531580c4724a8c559b745c8a to your computer and use it in GitHub Desktop.
Save VulBusters/436e48d2531580c4724a8c559b745c8a to your computer and use it in GitHub Desktop.
private static String Encryption(String plain)
{
byte[] plainTextByte = Encoding.ASCII.GetBytes(plain);
byte[] sharedSecret = GetSharedSecret();
byte[] sessionKey = GetSessionKey(sharedSecret);
SecureRandom secureRandom = new SecureRandom();
byte[] nonce = new byte[CryptoConstant.NonceSize];
byte[] aad = new byte[CryptoConstant.AadSize];
secureRandom.NextBytes(nonce);
aad = Encoding.ASCII.GetBytes(GetKeyId());
GcmBlockCipher cipherSpec = new GcmBlockCipher(new AesEngine());
AeadParameters cipherParams = new AeadParameters(new KeyParameter(sessionKey), CryptoConstant.AadSize * 8, nonce, aad);
cipherSpec.Init(true, cipherParams);
byte[] cipherText = new byte[cipherSpec.GetOutputSize(plainTextByte.Length)];
int offset = cipherSpec.ProcessBytes(plainTextByte, 0, plainTextByte.Length, cipherText, 0);
cipherSpec.DoFinal(cipherText, offset);
return Conversion.HexToString(nonce.Concat(cipherText).ToArray());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment