Base64 multiple by 4

A

aaaa aaaaaaaaaaa

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.
 
E

elise huard

Hi,

require 'base64'
...
decoded = Base64.decode64(string)

should do the trick

Elise
 
B

Brian Candler

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

aaaa aaaaaaaaaaa

Brian said:
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):
=> "\x14\x15\x16\x17\x18\x19\x1A\e\x1C\x1D\x1E\x1F
=> "my secret string"
 
B

Brian Candler

aaaa said:
Brian said:
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):

=> "\x14\x15\x16\x17\x18\x19\x1A\e\x1C\x1D\x1E\x1F

=> "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?
 
B

Brian Candler

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.
=> "aGG+eA"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top