Ruby Examples

ChilkatHOMEASPVisual BasicVB.NETC#Visual C++CMFCDelphiFoxProJavaPerlPHPPythonRubySQL ServerVBScript



Ruby
Examples

Quick Start
Ruby Unicode
Ruby Byte Array
Ruby Certs
Ruby Email
Ruby Encryption
Ruby FTP
HTML-to-XML
Ruby HTTP
Ruby IMAP
Ruby MHT
Ruby MIME
Ruby S/MIME
Ruby Signatures
Ruby RSA
Ruby Socket
Ruby Spider
Ruby Tar
Ruby Upload
Ruby XML
Ruby XMP
Ruby Zip

More Examples...
String
Email Object
POP3
SMTP
RSS
Atom
Self-Extractor
Service
PPMD
Deflate
Bzip2
DH Key Exchange
DSA
SSH Key
SSH
SSH Tunnel
SFTP

Unreleased...
LZW
Bz2
Icon

 

 

 

 

 

 

 

AES Encrypt Byte Array

Ruby code to AES encrypt a byte array.

Download Chilkat Ruby Library

require 'chilkat'

crypt = Chilkat::CkCrypt2.new()

# Any string argument automatically begins the 30-day trial.
success = crypt.UnlockComponent("30-day trial")
if (success != true)
    print "Crypt component unlock failed" + "\n"
    exit
end

# Use 128-bit AES encryption, in CBC mode.
crypt.put_CryptAlgorithm("aes")
crypt.put_CipherMode("cbc")
crypt.put_KeyLength(128)

# In Ruby, strings are sequences of bytes with no contraints
# on length and content.  Any byte value can be included,
# including null bytes.
# To efficiently process binary data in Ruby, you use
# strings, not arrays of bytes.

# Create an array of 256 bytes with each byte having
# the value of its index.
# For efficiency, we first create a 256-byte string,
# and then set the values at each index.
inBytes = ' ' * 256
for i in 0..255
	inBytes[i] = i
end

# We need byte arrays for our 128-bit secret key and
# initialization vector:
keyBytes = ' ' * 16
for i in 0..15
	keyBytes[i] = i
end
ivBytes = ' ' * 16
for i in 0..15
	ivBytes[i] = i
end

# 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.new()
inData.append(inBytes,256)

# Encrypt...
encryptedData = Chilkat::CkByteData.new()
crypt.EncryptBytes(inData,encryptedData)

# Extract the encrypted bytes back into a Ruby 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 = " + encryptedBytes.length.to_s() + " 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,encryptedBytes.length,"hex") + "\n"

# Decrypt...
decryptedData = Chilkat::CkByteData.new()
crypt.DecryptBytes(encryptedData,decryptedData )

# Get the bytes.
decryptedBytes = decryptedData.getBytes()

# The size in bytes is 256.  Chilkat Crypt automatically unpadded.
print "Decrypted size = " + decryptedBytes.length.to_s() + " bytes\n"

# Display the decrypted data in hex format:
print crypt.encodeBytes(decryptedBytes,decryptedBytes.length,"hex") + "\n"

 

Need a specific example? Send a request to support@chilkatsoft.com

© 2000-2008 Chilkat Software, Inc. All Rights Reserved.