Hey there... well... took a while but I got it to work by going through
some code with a colleague and translating it to Ruby. The CTS cipher
is now being deciphered using CBC.
I hope this helps some other folks in Ruby land. Please suggest some
ways to better document the steps.
Take care,
Mike
#key
d='4b1114cc73fed8b5428c3dee60d7773a'
tmpArray=nil
aes=nil
alpha=nil
omega=nil
#cipher text, unpacked from Base64
a='TmI9HrNrsMBxSfwApvSaQrLIDsLboNhIW/FawPjNUB0x/G0ZDf+gfk4JaTc/tGxDg1s4mrIRFOoBJemK+txUF0+aPw8bxIgzxmB3gq18aJRoSo5PWqbzS8FCCHrb3leKf4UUNFaIAaVVY1a5ymZ/HMPhwAKbii8x9Uk/S0MxaDofHTluc1E='.unpack('m')[0]
#compute the extra cipher beyond a 16 byte boundary
partial = a.length % 16
#set up the beginning and end of the CTS blocks we need to work on
alpha = a.length-16-partial
omega = a.length-1
#grab the end of the cipher
c=a.slice(alpha..omega)
#start + partial..end-partial
c<<a.slice(alpha+partial..omega-partial)
#first decrypt the beginning
aes = OpenSSL::Cipher::Cipher.new('aes-256-cbc')
aes.decrypt
aes.key=d
aes.iv='00000000000000000000000000000000'.unpack('a2'*16).map{|x|
x.hex}.pack('c'*16)
firstBlock=aes.update a.slice(0..((a.length/32)*32)-1)
#next decrypt the end
aes = OpenSSL::Cipher::Cipher.new('aes-256-cbc')
aes.decrypt
aes.key=d
aes.iv='00000000000000000000000000000000'.unpack('a2'*16).map{|x|
x.hex}.pack('c'*16)
decrypted3=aes.update c
lastBlock=""
#here we need to Xor the result against the contents of end of the
undecrypted cipher text
(0..partial-1).each {|index| tmpA='';tmpA=
(decrypted3[index]^c[16+index]).chr; lastBlock<<tmpA}
#put a number of bytes of undecrypted c then followed with the last
number of the decrypted c... the number of bytes sliced depends upon the
number of bytes in the partial block at the end of the cipher...
tmpArray =
c.slice(16..16+partial-1)<<decrypted3.slice(partial..decrypted3.length-1)
#now handle the middle
aes = OpenSSL::Cipher::Cipher.new('aes-256-cbc')
aes.decrypt
aes.key= d
aes.iv='00000000000000000000000000000000'.unpack('a2'*16).map{|x|
x.hex}.pack('c'*16)
tmpArray=tmpArray<<tmpArray
middle=aes.update tmpArray
#now take the last block of the decipherable original block
last = a.slice(alpha-16..alpha-1)
midBlock=""
#and ^or it against the middle component
(0..middle.length-1).each {|index|
tmpB='';tmpB<<(middle[index]^last[index]).chr;midBlock<<tmpB}
finalBlock = firstBlock+midBlock+lastBlock
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---