![]() |
Chilkat HOME Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi DLL Go Java Node.js Objective-C PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
AES Encrypt Byte Array
ASP script to AES encrypt a byte array. <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> </head> <body> <% set crypt = Server.CreateObject("Chilkat_9_5_0.Crypt2") ' Any string argument automatically begins the 30-day trial. success = crypt.UnlockComponent("30-day trial") If (success <> 1) Then Response.Write "Crypt component unlock failed" & "<br>" End If ' Use 128-bit AES encryption, in CBC mode. crypt.CryptAlgorithm = "aes" crypt.CipherMode = "cbc" crypt.KeyLength = 128 ' Create a byte array for our secret key and ' initialization vector: ' this byte array has 16 bytes (i.e. 128 bits) Dim key For i = 0 To 15 key = key & ChrB(i) Next Dim iv For i = 0 To 15 iv = iv & ChrB(i) Next ' Create a byte array to be encrypted. Dim data For i = 0 To 255 data = data & ChrB(i) Next crypt.SecretKey = key crypt.IV = iv Dim encryptedBytes encryptedBytes = crypt.EncryptBytes(data) ' How many bytes in the encrypted output? Response.Write "<p>Encrypted output size = " & (UBound(encryptedBytes)+1) & "</p>" ' We encrypted 256 bytes and the output is 272 bytes. Why? ' That's because padding is required to maintain a 16-octet (128-bit) blocksize. ' The IV is always equal to the blocksize in any symmetric ' encryption algorithm. ' PKCS#5 padding works as follows: the bytes remaining to fill a ' block are assigned a number, which is the number of bytes that ' were added to fill the block. For instance, if we have an 16-byte ' block, and only 11 bytes are filled, then we have 5 bytes to pad. ' Those 5 bytes are all assigned the value "5", for the 5 bytes of ' padding. In the case where the data is already a multiple ' of 16, an additional 16 bytes are added, each byte containing 0x10. ' Thus, when 256 bytes are encrypted, the result is 272 bytes. ' Display the encrypted bytes in hex: Response.Write "<p>" & crypt.Encode(encryptedBytes,"hex") & "</p>" ' Display the encrypted bytes as decimal integers: Dim s s = "" For i = 1 To LenB(encryptedBytes) s = s & CStr(AscB(MidB(encryptedBytes, i, 1))) & "," Next Response.Write "<p>" & s & "</p>" ' Decrypt the data and show the decrypted data in hex: Dim decryptedBytes decryptedBytes = crypt.DecryptBytes(encryptedBytes) ' How many bytes in the decrypted output? Response.Write "<p>Decrypted output size = " & (UBound(decryptedBytes)+1) & "</p>" ' Display the decrypted bytes in hex: Response.Write "<p>" & crypt.Encode(decryptedBytes,"hex") & "</p>" ' Display the decrypted bytes as decimal integers: s = "" For i = 1 To LenB(decryptedBytes) s = s & CStr(AscB(MidB(decryptedBytes, i, 1))) & "," Next Response.Write "<p>" & s & "</p>" %> </body> </html> |
||||
© 2000-2026 Chilkat Software, Inc. All Rights Reserved.