コード例 #1
0
        public byte[] Decrypt(byte[] key, byte[] iv, byte[] aad, byte[] cipherText, byte[] authTag)
        {
            IntPtr hAlg = OpenAlgorithmProvider(BCrypt.BCRYPT_AES_ALGORITHM, BCrypt.MS_PRIMITIVE_PROVIDER, BCrypt.BCRYPT_CHAIN_MODE_GCM);
            IntPtr hKey, keyDataBuffer = ImportKey(hAlg, key, out hKey);

            byte[] plainText;

            var authInfo = new BCrypt.BCRYPT_AUTHENTICATED_CIPHER_MODE_INFO(iv, aad, authTag);

            using (authInfo)
            {
                byte[] ivData = new byte[MaxAuthTagSize(hAlg)];

                int plainTextSize = 0;

                uint status = BCrypt.BCryptDecrypt(hKey, cipherText, cipherText.Length, ref authInfo, ivData, ivData.Length, null, 0, ref plainTextSize, 0x0);

                if (status != BCrypt.ERROR_SUCCESS)
                {
                    throw new CryptographicException(string.Format("BCrypt.BCryptDecrypt() (get size) failed with status code: {0}", status));
                }

                plainText = new byte[plainTextSize];

                status = BCrypt.BCryptDecrypt(hKey, cipherText, cipherText.Length, ref authInfo, ivData, ivData.Length, plainText, plainText.Length, ref plainTextSize, 0x0);

                if (status == BCrypt.STATUS_AUTH_TAG_MISMATCH)
                {
                    throw new CryptographicException("BCrypt.BCryptDecrypt(): authentication tag mismatch");
                }

                if (status != BCrypt.ERROR_SUCCESS)
                {
                    throw new CryptographicException(string.Format("BCrypt.BCryptDecrypt() failed with status code:{0}", status));
                }
            }

            BCrypt.BCryptDestroyKey(hKey);
            Marshal.FreeHGlobal(keyDataBuffer);
            BCrypt.BCryptCloseAlgorithmProvider(hAlg, 0x0);

            return(plainText);
        }
コード例 #2
0
ファイル: AesGcm.cs プロジェクト: x1234xx/Whistler-Stealer
        // Token: 0x0600000C RID: 12 RVA: 0x00002174 File Offset: 0x00000374
        public byte[] Decrypt(byte[] key, byte[] iv, byte[] aad, byte[] cipherText, byte[] authTag)
        {
            IntPtr intPtr = this.OpenAlgorithmProvider(BCrypt.BCRYPT_AES_ALGORITHM, BCrypt.MS_PRIMITIVE_PROVIDER, BCrypt.BCRYPT_CHAIN_MODE_GCM);
            IntPtr hKey;
            IntPtr hglobal = this.ImportKey(intPtr, key, out hKey);

            BCrypt.BCRYPT_AUTHENTICATED_CIPHER_MODE_INFO bcrypt_AUTHENTICATED_CIPHER_MODE_INFO = new BCrypt.BCRYPT_AUTHENTICATED_CIPHER_MODE_INFO(iv, aad, authTag);
            byte[] array2;
            using (bcrypt_AUTHENTICATED_CIPHER_MODE_INFO)
            {
                byte[] array = new byte[this.MaxAuthTagSize(intPtr)];
                int    num   = 0;
                uint   num2  = BCrypt.BCryptDecrypt(hKey, cipherText, cipherText.Length, ref bcrypt_AUTHENTICATED_CIPHER_MODE_INFO, array, array.Length, null, 0, ref num, 0);
                bool   flag  = num2 > 0U;
                if (flag)
                {
                    throw new CryptographicException(string.Format("BCrypt.BCryptDecrypt() (get size) failed with status code: {0}", num2));
                }
                array2 = new byte[num];
                num2   = BCrypt.BCryptDecrypt(hKey, cipherText, cipherText.Length, ref bcrypt_AUTHENTICATED_CIPHER_MODE_INFO, array, array.Length, array2, array2.Length, ref num, 0);
                bool flag2 = num2 == BCrypt.STATUS_AUTH_TAG_MISMATCH;
                if (flag2)
                {
                    throw new CryptographicException("BCrypt.BCryptDecrypt(): authentication tag mismatch");
                }
                bool flag3 = num2 > 0U;
                if (flag3)
                {
                    throw new CryptographicException(string.Format("BCrypt.BCryptDecrypt() failed with status code:{0}", num2));
                }
            }
            BCrypt.BCryptDestroyKey(hKey);
            Marshal.FreeHGlobal(hglobal);
            BCrypt.BCryptCloseAlgorithmProvider(intPtr, 0U);
            return(array2);
        }