Org.BouncyCastle.Crypto.Modes.CcmBlockCipher.DoFinal C# (CSharp) Method

DoFinal() public method

public DoFinal ( byte outBytes, int outOff ) : int
outBytes byte
outOff int
return int
		public virtual int DoFinal(
			byte[]	outBytes,
			int		outOff)
		{
			byte[] text = data.ToArray();
			byte[] enc = ProcessPacket(text, 0, text.Length);

			Array.Copy(enc, 0, outBytes, outOff, enc.Length);

			Reset();

			return enc.Length;
		}

Usage Example

        public string Encrypt(string data)
        {
            SecureRandom random = new SecureRandom();

            // Generate 256-bits AES key
            byte[] aesKey = new byte[32];
            random.NextBytes(aesKey);

            // Generate Initialization Vector
            byte[] IV = new byte[12];
            random.NextBytes(IV);

            // Apply RSA/None/PKCS1Padding encryption to the AES key
            byte[] encyptedAESKey = rsaCipher.DoFinal(aesKey);

            // Apply AES/CCM/NoPadding encryption to the data
            byte[] cipherText = System.Text.Encoding.UTF8.GetBytes(data);

            var ccmParameters = new CcmParameters(new KeyParameter(aesKey), 64, IV, new byte[] { });
            aesCipher = new CcmBlockCipher(new AesFastEngine());
            aesCipher.Init(true, ccmParameters);

            var encrypted = new byte[aesCipher.GetOutputSize(cipherText.Length)];
            var res = aesCipher.ProcessBytes(cipherText, 0, cipherText.Length, encrypted, 0);
            aesCipher.DoFinal(encrypted, res);

            // Merge 'IV' and 'encrypted' to 'result'
            byte[] result = new byte[IV.Length + encrypted.Length];
            System.Buffer.BlockCopy(IV, 0, result, 0, IV.Length);
            System.Buffer.BlockCopy(encrypted, 0, result, IV.Length, encrypted.Length);

            // Return encrypted data
            return Prefix + Version + Separator + System.Convert.ToBase64String(encyptedAESKey) + Separator + System.Convert.ToBase64String(result);
        }
All Usage Examples Of Org.BouncyCastle.Crypto.Modes.CcmBlockCipher::DoFinal