Пример #1
0
        //*** 20200524 - This one needs to be checked for consistency with Dart code!
        public static byte[] encryptStringWithAesGCM(String data, KeyParameter key)
        {
            // Generate a random 96-bit nonce N
            byte[] nonce = KeysHelper.generateRandomNonceUtf8(12);

            GcmBlockCipher cipher = new GcmBlockCipher(new AesEngine());
            //*** 20200524 - Is 96 the right number?
            AeadParameters parameters = new AeadParameters(key, 96, nonce);

            cipher.Init(true, parameters);
            byte[] utf8Data = Encoding.UTF8.GetBytes(data);
            int    bufSize  = utf8Data.Length;

            byte[] cipherText = new byte[cipher.GetOutputSize(bufSize)];
            int    len        = cipher.ProcessBytes(utf8Data, 0, bufSize, cipherText, 0);

            cipher.DoFinal(cipherText, len);
            using (MemoryStream combinedStream = new MemoryStream())
            {
                using (BinaryWriter binaryWriter = new BinaryWriter(combinedStream))
                {
                    binaryWriter.Write(nonce);
                    binaryWriter.Write(cipherText);
                }
                return(combinedStream.ToArray());
            }
        }