encryption - Coldfusion "AES/CBC/PKCS5Padding" decryption in Ruby -
i need decrypt text encrypted using aes/cbc/pkcs5padding scheme. encrypted text got generated using coldfusion.
cfml example below:
<table border="1" cellpadding="5" cellspacing="0"> <tr bgcolor="c0c0c0"> <th>decrypted string</th> <th>3deskey</th> </tr> <cfset variables.algorithm ="aes/cbc/pkcs5padding"> <cfset variables.seed ="c610297ce8570750"> <cfset variables.password = "vza0o49shpie/mr4+4jhxhapmkheyl5o2nzzdxvnqbo="> <cfset variables.decryptedstring = decrypt(variables.password, generate3deskey("#variables.seed#"), "#variables.algorithm#", "base64")> <cfoutput> <tr> <td>#variables.decryptedstring#</td> <td><cfoutput>#generate3deskey("variables.seed")#</cfoutput></td> </tr> </cfoutput> </table>
output is:
decrypted string: name322big563 3deskey: qzyxmdi5n0nfodu3mdc1ma==
i tried ruby:
require 'openssl' require 'base64' string = "vza0o49shpie/mr4+4jhxhapmkheyl5o2nzzdxvnqbo=" def decrypt(cpass) des = openssl::cipher::cipher.new('aes-256-cbc') des.decrypt des.key = 'c610297ce8570750' return des.update(base64.decode64(cpass)) + des.final end decrypted = decrypt(string) puts "decrypted string: #{decrypted}"
i key length short (openssl::cipher::ciphererror)
problem don't know key seed used c610297ce8570750
, because key returned cfml script base64 need hex key. tried openssl::cipher::aes256.new(:cbc)
same error.
require 'openssl' require 'base64' # decryption aes = openssl::cipher::aes256.new(:cbc) aes.decrypt aes.padding = 1 # it's on default aes.key = "qzyxmdi5n0nfodu3mdc1ma==" aes.iv = "c610297ce8570750" aes.update(base64::decode64("vza0o49shpie/mr4+4jhxhapmkheyl5o2nzzdxvnqbo="))+aes.final
any idea?
edit:
as hinted @leigh, need use aes-128-cbc
, did this:
require 'openssl' require 'base64' string = "vza0o49shpie/mr4+4jhxhapmkheyl5o2nzzdxvnqbo=" def decrypt(cpass) des = openssl::cipher::cipher.new('aes-128-cbc') des.decrypt des.key = 'c610297ce8570750' return des.update(base64.decode64(cpass)) + des.final end decrypted = decrypt(string) puts "decrypted string: #{decrypted}"
actually seems kinda work (...ish).
decrypted string: ▒▒.ϥd▒▒ ▒▒▒▒▒name322big563
any idea what's still wrong?
(expanded comments)
but need hex key
then convert base64 hex. in cf, can use binaryencode() , binarydecode functions:
binaryencode(binarydecode("qzyxmdi5n0nfodu3mdc1ma==", "base64"), "hex")
looks there few other problems:
the cf code generates 128 bit key, ruby code using aes 256. needs use aes 128.
the cf code generating random iv. ruby code using totally different
iv
.cbc
mode, both sides must use sameiv
expected results. "decrypting incorrect iv causes first block of plaintext corrupt ...", why decrypted value off. resolve it, ruby code should use sameiv
used encrypt.
update:
when cf generates iv automatically (as here), prepends iv encrypted value:
when coldfusion creates iv automatically, generates secure, random iv , prepends encrypted data. when coldfusion decrypts data, iv recovered , used. cryptologically important iv varies between encryptions. why encrypted value changes when repeatedly encrypt same string algorithm uses iv, des/cbc/pkcs5padding. unlike encryption key, not necessary iv kept secret.
so iv value can extracted removing first "block" of encrypted binary. block size depends on algorithm. aes, 16. not know exact ruby code, in cf extract iv so:
blocksize = 16; rawbinary = binarydecode(encryptedstring, "base64"); // iv first block ivbytes = arrayslice(rawbinary, 1, blocksize); // remaining bytes encrypted value databytes = arrayslice(rawbinary, blocksize+1, arraylen(rawbinary)-blocksize);
Comments
Post a Comment