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

DoFinal() public method

public DoFinal ( byte output, int outOff ) : int
output byte
outOff int
return int
		public int DoFinal(byte[] output, int outOff)
		{
			int extra = bufOff;
			if (!forEncryption)
			{
				if (extra < macSize)
					throw new InvalidCipherTextException("data too short");

				extra -= macSize;
			}

			if (extra > 0)
			{
				byte[] tmp = new byte[BlockSize];
				Array.Copy(bufBlock, 0, tmp, 0, extra);
				gCTRBlock(tmp, extra, output, outOff);
			}

			// Final gHASH
			BigInteger X = BigInteger.ValueOf(A.Length * 8).ShiftLeft(64).Add(
				BigInteger.ValueOf(totalLength * 8));
			//trace("len(A)||len(C): " + dumpBigInt(X));

			S = multiply(S.Xor(X), H);
			//trace("GHASH(H,A,C): " + dumpBigInt(S));

			// T = MSBt(GCTRk(J0,S))
			byte[] tBytes = new byte[BlockSize];
			cipher.ProcessBlock(J0, 0, tBytes, 0);
			//trace("E(K,Y0): " + new string(Hex.encode(tmp)));
			BigInteger T = S.Xor(new BigInteger(1, tBytes));

			// TODO Fix this if tagLength becomes configurable
			byte[] tag = asBlock(T);
			//trace("T: " + new string(Hex.encode(tag)));

			int resultLen = extra;

			if (forEncryption)
			{
				this.macBlock = tag;
				Array.Copy(tag, 0, output, outOff + bufOff, tag.Length);
				resultLen += tag.Length;
			}
			else
			{
				this.macBlock = new byte[macSize];
				Array.Copy(bufBlock, extra, macBlock, 0, macSize);
				if (!Arrays.AreEqual(tag, this.macBlock))
					throw new InvalidCipherTextException("mac check input GCM failed");
			}

			Reset(false);

			return resultLen;
		}

Usage Example

Example #1
0
        public static IDStorage decryptKeyStoreStorage(byte[] c_baKey, byte[] c_baIV, string c_sBase64JsonStorage)
        {
            const int MacBitSize = 128;
            byte [] baPayload = new byte[0];

            var decryptCipher = new GcmBlockCipher(new AesFastEngine());
            var parameters = new AeadParameters(new KeyParameter(c_baKey), MacBitSize, c_baIV, baPayload);
            decryptCipher.Init (false, parameters);

            byte[] baEncryptedStorage = Convert.FromBase64String (c_sBase64JsonStorage);

            var decryptedText = new byte[decryptCipher.GetOutputSize(baEncryptedStorage.Length)];
            try
            {
                var len = decryptCipher.ProcessBytes(baEncryptedStorage, 0, baEncryptedStorage.Length, decryptedText, 0);
                decryptCipher.DoFinal(decryptedText, len);
            }
            catch (InvalidCipherTextException)
            {
                //Return null if it doesn't authenticate
                return null;
            }

            string sJsonStorage = Encoding.GetEncoding (1252).GetString (decryptedText);
            IDStorage _KeyStoreStorage = JsonConvert.DeserializeObject <IDStorage> (sJsonStorage);

            return _KeyStoreStorage;
        }
All Usage Examples Of Org.BouncyCastle.Crypto.Modes.GcmBlockCipher::DoFinal