Chilkat HOME ASP Visual Basic VB.NET C# Visual C++ C MFC Delphi FoxPro Java Perl PHP Python Ruby SQL Server VBScript
AES Encrypt Byte ArrayPython code to AES encrypt a byte array.
import sys import chilkat crypt = chilkat.CkCrypt2() # Any string argument automatically begins the 30-day trial. success = crypt.UnlockComponent("30-day trial") if (success != True): print "Crypt component unlock failed" sys.exit() # Use 128-bit AES encryption, in CBC mode. crypt.put_CryptAlgorithm("aes") crypt.put_CipherMode("cbc") crypt.put_KeyLength(128) # In Python, strings are sequences of bytes with no contraints # on length and content. Any byte value can be included, # including null bytes. # Create a string of 256 bytes with each byte having # the value of its index. inBytes = '' for i in range(0,256): inBytes = inBytes + chr(i) # Create byte strings for our 128-bit secret key and # initialization vector: keyBytes = '' for i in range(0,16): keyBytes = keyBytes + chr(i) ivBytes = '' for i in range(0,16): ivBytes = ivBytes + chr(i) # The secret key is equal in length to the KeyLength. # In this case, the KeyLength = 128 bits, so the SecretKey # is 16 bytes (16 * 8 = 128) crypt.SetSecretKey(keyBytes,16) # For AES encryption, the IV is always 16 bytes. # If omitted, it defaults to 16 null bytes. crypt.SetIV(ivBytes,16) # The Chilkat Crypt module will work with CkByteData # objects. Copy the bytes into a CkByteData object: inData = chilkat.CkByteData() inData.append(inBytes,256) # Encrypt... encryptedData = chilkat.CkByteData() crypt.EncryptBytes(inData,encryptedData) # Extract the encrypted bytes back into a Python string. # (Remember, this isn't a printable string. It's a string # with binary byte data.) encryptedBytes = encryptedData.getBytes() # How many bytes in the output? print "encrypted size = " + str(len(encryptedBytes)) + " bytes\n" # 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. # PS> 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. # Save the encrypted data to a file. encryptedData.saveFile("encryptedData.dat") # Display the encrypted data as a hexidecimal string print crypt.encodeBytes(encryptedBytes,len(encryptedBytes),"hex") + "\n" # Decrypt... decryptedData = chilkat.CkByteData() crypt.DecryptBytes(encryptedData,decryptedData ) # Get the bytes. decryptedBytes = decryptedData.getBytes() # The size in bytes is 256. Chilkat Crypt automatically unpadded. print "Decrypted size = " + str(len(decryptedBytes)) + " bytes\n" # Display the decrypted data in hex format: print crypt.encodeBytes(decryptedBytes,len(decryptedBytes),"hex") + "\n" |
Need a specific example? Send a request to support@chilkatsoft.com
© 2000-2008 Chilkat Software, Inc. All Rights Reserved.