# Base64.decode different behaviour

**URL:** <https://www.ruby-forum.com/t/base64-decode-different-behaviour/68518>\
**Category:** Ruby\
**Created:** [September 13, 2006, 10:41am UTC](https://www.ruby-forum.com/t/base64-decode-different-behaviour/68518 "2006-09-13T10:41:26Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![filipk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/ad7895/32.png) [@filipk](https://www.ruby-forum.com/u/filipk)\
**Post date:** [September 13, 2006, 10:41am UTC](https://www.ruby-forum.com/t/base64-decode-different-behaviour/68518/1 "2006-09-13T10:41:26Z")

</div>

I am trying to read a value stored in an encrypted cookie in Ruby. The  
cookie value is 3DES-encrypted and then Base64-encoded.

First, I stored the value of a cookie in a file. And then I managed to  
decode and decrypt it successfully using this code:

```
key64 = "3DES-CRYPT-KEY-BASE64-ENCODED-READ-FROM-FILE"
key = Base64.decode64 key64

cookie64 = "BASE64-ENCODED-COOKIE-VALUE-READ-FROM-FILE"
cookie = Base64.decode64 cookie64

des = OpenSSL::Cipher::DES.new(:EDE3)
des.decrypt
des.key = key
des.iv = key[0..7]

tmp = des.update(cookie)
value = tmp
value << des.final

```

Unfortunately, after running the above code in a Rails controller I get  
an exception “wrong final block length” thrown from the last line. Could  
anyone tell me, why the code works, but when run in a Rails controller  
it doesn’t? The only change in Rails version being:

```
cookie64 = cookies['COOKIE-NAME']

```

Trying to find some clue, I commented out the lsat line and then I got a  
too short output - it’s missing some data from the input value (which is  
understandable, because “final” is needed for proper decrypting). For  
example:  
input string = “qwerty” -\> 3DES (in java) -\> Base64 (in java) -\>  
de-Base64 (in ruby) -\> de-3DES (in ruby) -\> “qwer”
