# Why first 16 bytes are wrong with ruby openssl aes 128 cbc?

**URL:** <https://www.ruby-forum.com/t/why-first-16-bytes-are-wrong-with-ruby-openssl-aes-128-cbc/234930>\
**Category:** Ruby\
**Created:** [May 19, 2013, 4:55pm UTC](https://www.ruby-forum.com/t/why-first-16-bytes-are-wrong-with-ruby-openssl-aes-128-cbc/234930 "2013-05-19T16:55:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![alcina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a587f6/32.png) [@alcina](https://www.ruby-forum.com/u/alcina)\
**Post date:** [May 19, 2013, 4:55pm UTC](https://www.ruby-forum.com/t/why-first-16-bytes-are-wrong-with-ruby-openssl-aes-128-cbc/234930/1 "2013-05-19T16:55:38Z")

</div>

I use openssl in ruby and openssl in bash, but the first 16 bytes with  
ruby/openssl are wrong, why ? (ruby version : 1.8.7)

for exemple :  
with ruby openssl  
77 70 30 21 30 30 80 3d 30 31 f1 30 30 30 31 de wrong bytes  
ff 36 90 e2 3d ff ff ff ff ff ff ff ff ff ff ff good bytes  
ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff good bytes  
\*\ ***good bytes** good bytes

with bash openssl  
47 40 00 11 00 00 b0 0d 00 01 c1 00 00 00 01 ef  
ff 36 90 e2 3d ff ff ff ff ff ff ff ff ff ff ff same bytes that ruby  
ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff same bytes that ruby  
\*\*\*\*_ **a lot of bytes** _ same bytes that ruby

ruby code :

require ‘openssl’

c = OpenSSL::Cipher::Cipher.new(“aes-128-cbc”)  
c.decrypt  
c.key = “\177\373\2002\337\363:\357\232\251.\232\311b9\323”  
c.iv = “00000000000000000000000000000001”  
data = File.read("/tmp/file\_crypt")  
d = c.update(data)  
d \<\< c.final  
file = File.open("/tmp/file\_decrypt\_ruby", “w”)  
file.write(d)  
file.close

bash code :

openssl aes-128-cbc -d -in /tmp/file\_crypt -out /tmp/file\_decrypt\_bash  
-nosalt -iv 00000000000000000000000000000001 -K  
7ffb8032dff33aef9aa92e9ac96239d3

Thank you.

---

<div class="post-metadata">

**Author:** ![davox](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/90ced4/32.png) [@davox](https://www.ruby-forum.com/u/davox)\
**Post date:** [May 20, 2013, 5:50pm UTC](https://www.ruby-forum.com/t/why-first-16-bytes-are-wrong-with-ruby-openssl-aes-128-cbc/234930/2 "2013-05-20T17:50:59Z")

</div>

Hello David,

If I understand you correctly, this seems to be caused by what is  
outlined here:

> **[Class: OpenSSL::Cipher (Ruby 2.0.0)](https://ruby-doc.org/stdlib-2.0.0/libdoc/openssl/rdoc/OpenSSL/Cipher.html)**
>
> Class : OpenSSL::Cipher - Ruby 2.0.0

Under the section “auth\_tag([tag\_len] → string”

It looks like this is happening because you didn’t invoke tag\_len.

---

<div class="post-metadata">

**Author:** ![davox](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/90ced4/32.png) [@davox](https://www.ruby-forum.com/u/davox)\
**Post date:** [May 24, 2013, 12:43am UTC](https://www.ruby-forum.com/t/why-first-16-bytes-are-wrong-with-ruby-openssl-aes-128-cbc/234930/3 "2013-05-24T00:43:07Z")

</div>

I’m sorry for my late. Thank Ryan.

I’m sorry but auth\_tag doesn’t exist for ruby 1.8.7. For be clear a  
exemple this : [http://pastebin.com/p08QTXru](http://pastebin.com/p08QTXru) who is an encrypted file  
encode to base64.  
didier@laptop-E520:/tmp$ pbget “[http://pastebin.com/p08QTXru](http://pastebin.com/p08QTXru)” | base64  
-d \> file\_enc  
didier@laptop-E520:/tmp$ md5sum file\_enc  
30b8f5e7d700c108cd9815c00ca1de2d file\_enc

pbget is this [pbget.sh - Pastebin.com](http://pastebin.com/fXsuA9bn) a bash program for pastebin.

decrypte the file :

didier@laptop-E520:/tmp/ruby\_forum$ openssl aes-128-cbc -d -in file\_enc  
-out pic\_bash.jpg -nosalt -iv 00000000000000000000000000000001 -K  
7ffb8032dff33aef9aa92e9ac96239d3

ruby code :

require ‘openssl’

c = OpenSSL::Cipher::Cipher.new(“aes-128-cbc”)  
c.decrypt  
c.key = “\177\373\2002\337\363:\357\232\251.\232\311b9\323”  
c.iv = “00000000000000000000000000000001”  
data = File.read(“/tmp/file\_enc”)  
d = c.update(data)  
d \<\< c.final  
file = File.open(“/tmp/file\_decrypt\_ruby”, “w”)  
file.write(d)  
file.close

didier@laptop-E520:/tmp/ruby\_forum$ hd pic\_bash.jpg | head  
00000000 ff d8 ff e1 22 d2 45 78 69 66 00 00 4d 4d 00 2a  
|…".Exif…MM.\*|  
00000010 00 00 00 08 00 0c 01 1a 00 05 00 00 00 01 00 00  
|…|  
00000020 01 46 01 1b 00 05 00 00 00 01 00 00 01 4e 01 28  
|.F…N.(|  
00000030 00 03 00 00 00 01 00 02 00 00 02 13 00 03 00 00  
|…|  
00000040 00 01 00 01 00 00 87 69 00 04 00 00 00 01 00 00  
|…i…|  
00000050 00 9e 01 3b 00 02 00 00 00 0d 00 00 01 56 01 0e  
|…;…V…|  
00000060 00 02 00 00 00 13 00 00 01 64 01 32 00 02 00 00  
|…d.2…|  
00000070 00 14 00 00 01 78 01 0f 00 02 00 00 00 06 00 00  
|…x…|  
00000080 01 8c 01 10 00 02 00 00 00 08 00 00 01 92 01 31  
|…1|  
00000090 00 02 00 00 00 08 00 00 01 9a 01 12 00 03 00 00  
|…|

didier@laptop-E520:/tmp/ruby\_forum$ hd file\_decrypt\_ruby | head  
00000000 cf e8 cf d1 12 e2 75 48 59 56 30 30 7d 7d 30 1b  
|…uHYV00}}0.|  
00000010 00 00 00 08 00 0c 01 1a 00 05 00 00 00 01 00 00  
|…|  
00000020 01 46 01 1b 00 05 00 00 00 01 00 00 01 4e 01 28  
|.F…N.(|  
00000030 00 03 00 00 00 01 00 02 00 00 02 13 00 03 00 00  
|…|  
00000040 00 01 00 01 00 00 87 69 00 04 00 00 00 01 00 00  
|…i…|  
00000050 00 9e 01 3b 00 02 00 00 00 0d 00 00 01 56 01 0e  
|…;…V…|  
00000060 00 02 00 00 00 13 00 00 01 64 01 32 00 02 00 00  
|…d.2…|  
00000070 00 14 00 00 01 78 01 0f 00 02 00 00 00 06 00 00  
|…x…|  
00000080 01 8c 01 10 00 02 00 00 00 08 00 00 01 92 01 31  
|…1|  
00000090 00 02 00 00 00 08 00 00 01 9a 01 12 00 03 00 00  
|…|

This is independent of key. Thank you.

---

<div class="post-metadata">

**Author:** ![davox](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/90ced4/32.png) [@davox](https://www.ruby-forum.com/u/davox)\
**Post date:** [May 24, 2013, 9:00pm UTC](https://www.ruby-forum.com/t/why-first-16-bytes-are-wrong-with-ruby-openssl-aes-128-cbc/234930/4 "2013-05-24T21:00:14Z")

</div>

Thank Ryan

I have the response my ruby code is wrong. The good code is here :

require ‘openssl’

c = OpenSSL::Cipher::Cipher.new(“aes-128-cbc”)  
c.decrypt  
c.key =  
“7ffb8032dff33aef9aa92e9ac96239d3”.unpack(‘a2’\*16).map{|x|  
x.hex}.pack(‘c’\*16)  
c.iv =  
“00000000000000000000000000000001”.unpack(‘a2’\*16).map{|x|  
x.hex}.pack(‘c’\*16)  
data = File.read("/tmp/file\_enc")  
d = c.update(data)  
d \<\< c.final  
file = File.open("/tmp/file\_decrypt\_ruby", “w”)  
file.write(d)  
file.close

This code work very well.
