Base64 multiple by 4

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

Thanks in advance.

aaaa aaaaaaaaaaa wrote:

      "can only decode strings which are a multiple of 4

characters."

Ignore that string.

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

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

Hi,

require ‘base64’

decoded = Base64.decode64(string)

should do the trick

Elise

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”

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?

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”

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs