# Base64 multiple by 4

**URL:** <https://www.ruby-forum.com/t/base64-multiple-by-4/193452>\
**Category:** Ruby\
**Created:** [August 18, 2010, 12:05am UTC](https://www.ruby-forum.com/t/base64-multiple-by-4/193452 "2010-08-18T00:05:35Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![nginxchina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f0a364/32.png) [@nginxchina](https://www.ruby-forum.com/u/nginxchina)\
**Post date:** [August 18, 2010, 12:05am UTC](https://www.ruby-forum.com/t/base64-multiple-by-4/193452/1 "2010-08-18T00:05:35Z")

</div>

Hello,

I’m in the need of a Base64 decoding method that can decode a string  
multiplied by 4. At the current moment I have the following method:

```
def decode(encoded)
  if not encoded.length % 12 == 0
    raise BadInputError,
      "can only decode strings which are a multiple of 4

```

characters."  
end

```
  decoded = String.new

  k = -1
  while (k < encoded.length - 1) do
    right = 0
    left = 0

    (0..5).each do |i|
      k = k + 1
      right |= encoding_characters.index(encoded[k]) << (i * 6)
    end

    (0..5).each do |i|
      k = k + 1
      left |= encoding_characters.index(encoded[k]) << (i * 6)
    end

    (0..3).each do |i|
      decoded += ((left & (0xFF << ((3 - i) * 8))) >> ((3 - i) *

```

8)).chr  
end

```
    (0..3).each do |i|
      decoded += ((right & (0xFF << ((3 - i) * 8))) >> ((3 - i) *

```

8)).chr  
end  
end

```
  return decoded
end

```

What I need (to be exact) is a ruby port of the encode/decode method  
from  
[http://www.koders.com/java/fid2B76F86B8F8FF984EFE3C04AC6DE198E40D8EF41.aspx](http://www.koders.com/java/fid2B76F86B8F8FF984EFE3C04AC6DE198E40D8EF41.aspx)

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![nginxchina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f0a364/32.png) [@nginxchina](https://www.ruby-forum.com/u/nginxchina)\
**Post date:** [August 18, 2010, 12:06am UTC](https://www.ruby-forum.com/t/base64-multiple-by-4/193452/2 "2010-08-18T00:06:26Z")

</div>

aaaa aaaaaaaaaaa wrote:

> ```
> "can only decode strings which are a multiple of 4
> 
> ```
> 
> characters."

Ignore that string.

---

<div class="post-metadata">

**Author:** ![nginxchina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f0a364/32.png) [@nginxchina](https://www.ruby-forum.com/u/nginxchina)\
**Post date:** [August 18, 2010, 3:35am UTC](https://www.ruby-forum.com/t/base64-multiple-by-4/193452/3 "2010-08-18T03:35:48Z")

</div>

aaaa aaaaaaaaaaa wrote:

> Hello,
> 
> I’m in the need of a Base64 decoding method

Are you looking for  
string.unpack(“m\*”).first  
?

[http://ruby-doc.org/core/classes/String.html#M000760](http://ruby-doc.org/core/classes/String.html#M000760)

---

<div class="post-metadata">

**Author:** ![nginxchina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f0a364/32.png) [@nginxchina](https://www.ruby-forum.com/u/nginxchina)\
**Post date:** [August 18, 2010, 11:18am UTC](https://www.ruby-forum.com/t/base64-multiple-by-4/193452/4 "2010-08-18T11:18:14Z")

</div>

irb(main):001:0\> s = [“hello world”].pack(“m”)  
=\> “aGVsbG8gd29ybGQ=\n”  
irb(main):002:0\> s.unpack(“m”).first  
=\> “hello world”

---

<div class="post-metadata">

**Author:** ![nginxchina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f0a364/32.png) [@nginxchina](https://www.ruby-forum.com/u/nginxchina)\
**Post date:** [August 18, 2010, 9:38am UTC](https://www.ruby-forum.com/t/base64-multiple-by-4/193452/5 "2010-08-18T09:38:29Z")

</div>

Hi,

require ‘base64’  
…  
decoded = Base64.decode64(string)

should do the trick

Elise

---

<div class="post-metadata">

**Author:** ![nginxchina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f0a364/32.png) [@nginxchina](https://www.ruby-forum.com/u/nginxchina)\
**Post date:** [August 18, 2010, 11:23am UTC](https://www.ruby-forum.com/t/base64-multiple-by-4/193452/6 "2010-08-18T11:23:35Z")

</div>

Brian C. wrote:

> irb(main):001:0\> s = [“hello world”].pack(“m”)  
> =\> “aGVsbG8gd29ybGQ=\n”  
> irb(main):002:0\> s.unpack(“m”).first  
> =\> “hello world”

Once again, that’s what not I wanted. Heres an example of what I wanted  
(and now have):

> > RBase64.charset = (20…84).map(&:chr).join  
> > =\> “\x14\x15\x16\x17\x18\x19\x1A\e\x1C\x1D\x1E\x1F  
> > !”#$%&’()\*+,-./0123456789:;\<=\>?@ABCDEFGHIJKLMNOPQRST"
> 
> > RBase64.encode “my secret string”  
> > =\> “/+840J)70:)H\x1C\e!H0:9B-D==”
> 
> > RBase64.decode “/+840J)70:)H\x1C\e!H0:9B-D==”  
> > =\> “my secret string”

---

<div class="post-metadata">

**Author:** ![nginxchina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f0a364/32.png) [@nginxchina](https://www.ruby-forum.com/u/nginxchina)\
**Post date:** [August 18, 2010, 4:11pm UTC](https://www.ruby-forum.com/t/base64-multiple-by-4/193452/7 "2010-08-18T16:11:16Z")

</div>

aaaa aaaaaaaaaaa wrote:

> Brian C. wrote:
> 
> > irb(main):001:0\> s = [“hello world”].pack(“m”)  
> > =\> “aGVsbG8gd29ybGQ=\n”  
> > irb(main):002:0\> s.unpack(“m”).first  
> > =\> “hello world”
> 
> Once again, that’s what not I wanted.

Then you need to specify more precisely what you want.

At a glance, the Java code you linked to implements a perfectly standard  
Base64 encoding (although I’ve not attempted to run it), which is why I  
gave the Ruby equivalent. You’re saying it doesn’t? What does it  
implement then?

“decode a string multiplied by 4” doesn’t really mean anything by  
itself. The Java code appears to take groups of 3 bytes and turns them  
into 4 characters, as Base64 does.

> Heres an example of what I wanted  
> (and now have):
> 
> > > RBase64.charset = (20…84).map(&:chr).join  
> > > =\> “\x14\x15\x16\x17\x18\x19\x1A\e\x1C\x1D\x1E\x1F  
> > > !”#$%&’()\*+,-./0123456789:;\<=\>?@ABCDEFGHIJKLMNOPQRST"
> > 
> > > RBase64.encode “my secret string”  
> > > =\> “/+840J)70:)H\x1C\e!H0:9B-D==”
> > 
> > > RBase64.decode “/+840J)70:)H\x1C\e!H0:9B-D==”  
> > > =\> “my secret string”

Test vectors are certainly useful.

Now, can you describe the encode/decode algorithm you’re implementing,  
other than by means of your existing Ruby code? (Incidentally, you’ve  
only shown your decode method so far, not the encode)

What character set are you using in the encoded string? Why are the  
unprintable characters \x1C and \e in there?

---

<div class="post-metadata">

**Author:** ![nginxchina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f0a364/32.png) [@nginxchina](https://www.ruby-forum.com/u/nginxchina)\
**Post date:** [August 18, 2010, 4:15pm UTC](https://www.ruby-forum.com/t/base64-multiple-by-4/193452/8 "2010-08-18T16:15:55Z")

</div>

Also, I tried to run the code you posted with the decode test vector,  
but it fails because encoding\_characters is not defined.

If all you’re doing is base64 with some obfuscated character set, then  
String#tr may do what you want.

> > str = “foobar”  
> > =\> “foobar”
> 
> > str.tr(“ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/”, “V8yOtbKFR4mIDuwEzP7Mv/TLQSe+qC6aHkolZ50dG2jAUpWxh1J3N9YsBiXrcgfn”)  
> > =\> “aGG+eA”
