Last active
August 9, 2023 12:37
-
-
Save VulBusters/436e48d2531580c4724a8c559b745c8a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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