Base64 decoding yields different results in Java and Ruby

I decode some text using Base64. I tried three different libraries (general Base64, Java.misc.as well as java.mail) in Java and all of them give the same result for the following text, which is wrong.

However, when I use Ruby to decode the line below, I get a different output. I am getting the correct result using Ruby. Ruby Code print Base64.decode64('<Below String>')String

RkxWAQEAAAAJAAAAABIAAK4AAAAAAAAAAgAKb25NZXRhRGF0YQgAAAAHAAV3aWR0aABAdAAAAAAA
AAAGaGVpZ2h0AEB0AAAAAAAAAAlmcmFtZXJhdGUAQBAAAAAAAAAADHZpZGVvY29kZWNpZABACAAA
AAAAAAAMY2FuU2Vla1RvRW5kAQEAD21ldGFkYXRhY3JlYXRvcgIAKVNpbXBsZUZMVldyaXRlci5h
cyB2MC44IHplcm9wb2ludG5pbmUuY29tAAAJ

Exit should start with FLV. I am not sure what I am missing and why the output is different from Java.

+3
source share
3 answers

Base64 decoding results in binary data. You should not try to print it as if it were text.

Ruby, , Base64.decode64 - ... .

, , , .

(, , , Base64.decode64 , - base64 . , . ...)

+4

? , ByteArray .

:

BASE64Decoder decoder = new BASE64Decoder();
byte[] decodedBytes = decoder.decodeBuffer(encodedBytes);
+1

'FLV':

require 'base64'
Base64.decode64('RkxWAQEAAAAJAAAAABIAAK4AAAAAAAAAAgAKb25NZXRhRGF0YQgAAAAHAAV3aWR0aABAdAAAAAAA AAAGaGVpZ2h0AEB0AAAAAAAAAAlmcmFtZXJhdGUAQBAAAAAAAAAADHZpZGVvY29kZWNpZABACAAA AAAAAAAMY2FuU2Vla1RvRW5kAQEAD21ldGFkYXRhY3JlYXRvcgIAKVNpbXBsZUZMVldyaXRlci5h cyB2MC44IHplcm9wb2ludG5pbmUuY29tAAAJ')
=> "FLV\001\001\000\000\000\t\000\000\000\000\022\000\000\256\000\000\000\000\000\000\000\002\000\nonMetaData\b\000\000\000\a\000\005width\000@t\000\000\000\000\000\000\000\006height\000@t\000\000\000\000\000\000\000\tframerate\000@\020\000\000\000\000\000\000\000\fvideocodecid\000@\b\000\000\000\000\000\000\000\fcanSeekToEnd\001\001\000\017metadatacreator\002\000)SimpleFLVWriter.as v0.8 zeropointnine.com\000\000\t"

JRuby 1.6.1

0

All Articles