SteamKit2.CryptoHelper.SymmetricDecrypt C# (CSharp) Method

SymmetricDecrypt() public static method

Decrypts using AES/CBC/PKCS7 with an input byte array and key, using the random IV prepended using AES/ECB/None
public static SymmetricDecrypt ( byte input, byte key ) : byte[]
input byte
key byte
return byte[]
        public static byte[] SymmetricDecrypt( byte[] input, byte[] key )
        {
            Debug.Assert( key.Length == 32 );

            using ( var aes = new RijndaelManaged() )
            {
                aes.BlockSize = 128;
                aes.KeySize = 256;

                // first 16 bytes of input is the ECB encrypted IV
                byte[] cryptedIv = new byte[ 16 ];
                byte[] iv = new byte[ cryptedIv.Length ];
                Array.Copy( input, 0, cryptedIv, 0, cryptedIv.Length );

                // the rest is ciphertext
                byte[] cipherText = new byte[ input.Length - cryptedIv.Length ];
                Array.Copy( input, cryptedIv.Length, cipherText, 0, cipherText.Length );

                // decrypt the IV using ECB
                aes.Mode = CipherMode.ECB;
                aes.Padding = PaddingMode.None;

                using ( var aesTransform = aes.CreateDecryptor( key, null ) )
                {
                    iv = aesTransform.TransformFinalBlock( cryptedIv, 0, cryptedIv.Length );
                }

                // decrypt the remaining ciphertext in cbc with the decrypted IV
                aes.Mode = CipherMode.CBC;
                aes.Padding = PaddingMode.PKCS7;

                using ( var aesTransform = aes.CreateDecryptor( key, iv ) )
                using ( var ms = new MemoryStream( cipherText ) )
                using ( var cs = new CryptoStream( ms, aesTransform, CryptoStreamMode.Read ) )
                {
                    // plaintext is never longer than ciphertext
                    byte[] plaintext = new byte[ cipherText.Length ];

                    int len = cs.Read( plaintext, 0, plaintext.Length );

                    byte[] output = new byte[ len ];
                    Array.Copy( plaintext, 0, output, 0, len );

                    return output;
                }
            }
        }

Usage Example

Exemplo n.º 1
0
        /// <summary>
        /// Verifies and performs a symmetricdecrypt on the input using the given password as a key
        /// </summary>
        public static byte[] VerifyAndDecryptPassword(byte[] input, string password)
        {
            byte[] key, hash;
            using (SHA256 sha256 = SHA256Managed.Create())
            {
                byte[] password_bytes = Encoding.ASCII.GetBytes(password);
                key = sha256.ComputeHash(password_bytes);
            }
            using (HMACSHA1 hmac = new HMACSHA1(key))
            {
                hash = hmac.ComputeHash(input, 0, 32);
            }

            for (int i = 32; i < input.Length; i++)
            {
                if (input[i] != hash[i % 32])
                {
                    return(null);
                }
            }

            byte[] encrypted = new byte[32];
            Array.Copy(input, 0, encrypted, 0, 32);

            return(CryptoHelper.SymmetricDecrypt(encrypted, key));
        }
All Usage Examples Of SteamKit2.CryptoHelper::SymmetricDecrypt