Zip Component, Email Component, Encryption Component ActiveX Control for Zip Compression .NET Components for ASP.NET
ActiveX and .NET Components for Zip Compression, Encryption, Email, XML, S/MIME, HTML Email, Character Encoding, Digital Certificates, FTP, and more ASP Email ActiveX Component


Index of Chilkat Blog Posts

January 17, 2008

Matching .NET Framework 256-bit AES CBC Encryption with ActiveX

This example demonstrates how the Chilkat ActiveX Encryption Component can be used to match the results produced by the Microsoft .NET Framework for 256-bit AES CBC-mode encryption:

The following C# and VBScript sample code both produces the same encrypted result (base64 encoded):

OHaaCi3GqtM3jlm0rkgBsg==

C# 256-bit AES Encryption (CBC-Mode):

        private void CryptTest()
        {
            byte[] initVectorBytes = Encoding.ASCII.GetBytes("1234567890123456");
            byte[] plainTextBytes = Encoding.UTF8.GetBytes("my secret text");
            byte[] keyBytes = Encoding.ASCII.GetBytes("12345678901234561234567890123456");
	
            RijndaelManaged symmetricKey = new RijndaelManaged();
            symmetricKey.Mode = CipherMode.CBC;
	
            ICryptoTransform encryptor = symmetricKey.CreateEncryptor(
                                                             keyBytes,
                                                             initVectorBytes);
	
            MemoryStream memoryStream = new MemoryStream();
	
            CryptoStream cryptoStream = new CryptoStream(memoryStream,
                                                         encryptor,
                                                         CryptoStreamMode.Write);
            cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
	
            cryptoStream.FlushFinalBlock();
	
            byte[] cipherTextBytes = memoryStream.ToArray();
	
            memoryStream.Close();
            cryptoStream.Close();
	
            string cipherText = Convert.ToBase64String(cipherTextBytes);
	
            textBox1.Text = cipherText;
        }

VBScript 256-bit AES Encryption (CBC-Mode):

Dim fso, outFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set outFile = fso.CreateTextFile("outputCbc256.txt", True)
	
set crypt = CreateObject("Chilkat.Crypt2")
	
success = crypt.UnlockComponent("Anything for 30-day trial")
If (success <> 1) Then
    MsgBox "Crypt component unlock failed"
    WScript.Quit
End If
	
crypt.CryptAlgorithm = "aes"
crypt.CipherMode = "cbc"
crypt.KeyLength = 256
' Comment this out: use the default padding.
'crypt.PaddingScheme = 3
	
crypt.SetEncodedKey "12345678901234561234567890123456","ascii"
' Note: The IV is equal to the block size of the algorithm, NOT the key length.
' The AES encryption algorithm has a block size of 16 bytes, even when the key size is 256-bits.
crypt.SetEncodedIV "1234567890123456","ascii"
	
crypt.EncodingMode = "base64"
	
text = "my secret text"
	
'  Encrypt a string and return the binary encrypted data
'  in a base-64 encoded string.
encText = crypt.EncryptStringENC(text)
	
outFile.WriteLine(encText)
	
'  Decrypt and show the original string:
decryptedText = crypt.DecryptStringENC(encText)
' This gets rid of the NULL padding chars...
decryptedText = crypt.TrimEndingWith(decryptedText," ")
	
outFile.WriteLine(decryptedText)
outFile.Close


Privacy Statement. Copyright 2000-2011 Chilkat Software, Inc. All rights reserved.
Send feedback to support@chilkatsoft.com

Components for Microsoft Windows XP, 2000, 2003 Server, Vista, Windows 7, and Windows 95/98/NT4.