Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BASE64 encode/decode failure #45

Closed
GoogleCodeExporter opened this issue Sep 11, 2015 · 3 comments
Closed

BASE64 encode/decode failure #45

GoogleCodeExporter opened this issue Sep 11, 2015 · 3 comments

Comments

@GoogleCodeExporter
Copy link

What steps will reproduce the problem?
1. simple test script for base64 encode/decoder...

What is the expected output? What do you see instead?
-Used text from Wikipedia base64 page; text does NOT match.



Please provide any additional information below.

<!DOCTYPE HTML>
<HTML>
<HEAD>
<META charset="utf-8">
<TITLE>De/Encode string -> BASE64</TITLE>
<SCRIPT src="javascript/crypto/components/core.js"></SCRIPT>
<SCRIPT src="javascript/crypto/components/cipher-core.js"></SCRIPT>
<SCRIPT src="javascript/crypto/components/enc-utf16.js"></SCRIPT>
<SCRIPT src="javascript/crypto/components/enc-base64.js"></SCRIPT>
</HEAD>

<BODY>
<SCRIPT language="javascript">
    var nextLine="<BR>";

//http://en.wikipedia.org/wiki/Base64
    var strWikiText= "Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure.";
    var strWikiB64 ="TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGludWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=";


    /*

    var str     = '48656c6c6f2c20576f726c6421';
    var words = CryptoJS.enc.Hex.parse(str);
    var hex   = CryptoJS.enc.Hex.stringify(str);
    document.writeln(str +nextLine + words +nextLine + hex +"<HR>");
    */

    var word8 = CryptoJS.enc.Utf8.parse(strHttp);
    document.writeln(word8 +nextLine );
    var utf8  = word8.toString(CryptoJS.enc.Utf8);
    document.writeln( utf8 +"<HR>");

    /*
    var str  = "AXLBOX";
    var hash = CryptoJS.enc.Utf16.parse(str);
    var words  = hash.toString(CryptoJS.enc.Base64);// use this for the AES key
    document.writeln(strHttp +nextLine + str +nextLine + hash +nextLine + words +nextLine);
    var encrypted = CryptoJS.AES.encrypt(strHttp, words);
    var decrypted = CryptoJS.AES.decrypt(encrypted, words);
    document.writeln(nextLine +encrypted +nextLine + nextLine +decrypted +"<HR>");
    */

    var wordT = CryptoJS.enc.Base64.parse(strWikiText);
    var words = CryptoJS.enc.Base64.parse(strWikiB64);
    var base64 = wordT.toString(CryptoJS.enc.Base64);

    document.writeln(  decode64(strWikiB64) +nextLine + "<HR>");
    document.writeln(wordT +nextLine + words +nextLine + base64 +nextLine + "<HR>");


///
/// testing this crypto lib against known values as provided by 
http://en.wikipedia.org/wiki/Base64
///
    var testString="any carnal pleasure.";
    var x=testString.length;
    for (x=testString.length; x>testString.length-6; x--) {
        var tempStr=testString.substr(0, x);
        document.writeln("Input: " + tempStr + " ; Output: " + CryptoJS.enc.Base64.parse(tempStr) +nextLine);
    }//end for x

</SCRIPT>
</BODY>
</HTML>

Original issue reported on code.google.com by lorez...@gmail.com on 12 Aug 2012 at 1:07

@GoogleCodeExporter
Copy link
Author

I found this javascript code to work perfectly as expected. Please update your 
source to work accordingly.


// This code was written by Tyler Akins and has been placed in the
// public domain.  It would be nice if you left this header intact.
// Base64 code from Tyler Akins -- http://rumkin.com

var keyStr = 
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

function encode64(input) {
   if (!String(input).length) return false;
   var output = "";
   var chr1, chr2, chr3;
   var enc1, enc2, enc3, enc4;
   var i = 0;

   do {
      chr1 = input.charCodeAt(i++);
      chr2 = input.charCodeAt(i++);
      chr3 = input.charCodeAt(i++);

      enc1 = chr1 >> 2;
      enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
      enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
      enc4 = chr3 & 63;

      if (isNaN(chr2)) {
         enc3 = enc4 = 64;
      } else if (isNaN(chr3)) {
         enc4 = 64;
      }

      output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) + 
         keyStr.charAt(enc3) + keyStr.charAt(enc4);
   } while (i < input.length);

   return output;
}

function decode64(input) {
   if (!input) return false;
   var output = "";
   var chr1, chr2, chr3;
   var enc1, enc2, enc3, enc4;
   var i = 0;

   // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
   input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

   do {
      enc1 = keyStr.indexOf(input.charAt(i++));
      enc2 = keyStr.indexOf(input.charAt(i++));
      enc3 = keyStr.indexOf(input.charAt(i++));
      enc4 = keyStr.indexOf(input.charAt(i++));

      chr1 = (enc1 << 2) | (enc2 >> 4);
      chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
      chr3 = ((enc3 & 3) << 6) | enc4;

      output = output + String.fromCharCode(chr1);

      if (enc3 != 64) {
         output = output + String.fromCharCode(chr2);
      }
      if (enc4 != 64) {
         output = output + String.fromCharCode(chr3);
      }
   } while (i < input.length);

   return output;
}

Original comment by lorez...@gmail.com on 12 Aug 2012 at 1:09

@GoogleCodeExporter
Copy link
Author

Hi, rbartlett_jr. I appreciate your taking the time to create tests, but your 
test page has a number problems. You're trying to use variables that are 
undeclared and undefined (e.g., strHttp); you're trying to parse non-base64 
strings as base64 (e.g., strWikiText, and again with testString); and you're 
using non-existent functions (e.g., decode64).

Below is your test page but corrected, and the output is exactly as expected.

<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
        <title>de/encode string -> base64</title>
        <script src="http://crypto-js.googlecode.com/svn/tags/3.0.2/build/components/core.js"></script>
        <script src="http://crypto-js.googlecode.com/svn/tags/3.0.2/build/components/cipher-core.js"></script>
        <script src="http://crypto-js.googlecode.com/svn/tags/3.0.2/build/components/enc-utf16.js"></script>
        <script src="http://crypto-js.googlecode.com/svn/tags/3.0.2/build/components/enc-base64.js"></script>
    </head>
    <body>
        <script>
            var strWikiText = "Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure.";
            var strWikiB64  = "TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGludWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=";

            document.write(
                "expected: " + strWikiB64 + "<br>" +
                "actual: " + CryptoJS.enc.Latin1.parse(strWikiText).toString(CryptoJS.enc.Base64) +
                "<hr>"
            );

            var testString = "any carnal pleasure.";
            for (var x = testString.length; x > testString.length - 6; x--) {
                var tempStr = testString.substr(0, x);

                document.write("Input: " + tempStr + " ; Output: " + CryptoJS.enc.Latin1.parse(tempStr).toString(CryptoJS.enc.Base64) + '<br>');
            }
        </script>
    </body>
</html>

Original comment by Jeff.Mott.OR on 12 Aug 2012 at 3:41

  • Changed state: Invalid

@GoogleCodeExporter
Copy link
Author

The sections that used the var strHttp were not required for this issue and I 
stripped it out before posting. In fact, the sections not related to base64 
should be ignored. I failed to remove those sections prior to posting.


Thank you for the clarification. I have confirmed that it does work as you have 
explained.
You may consider using this for your FAQ / demo section as the example 
demostrating base64 encode/decoding can be a bit confusing (at first).

<script>
    var nextLine="<BR>";

//http://en.wikipedia.org/wiki/Base64
    var strWikiText= "Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure.";
    var strWikiB64 ="TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGludWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=";

///
/// testing this crypto lib against known values as provided by 
http://en.wikipedia.org/wiki/Base64
///
             document.write(
                  "Text: " + CryptoJS.enc.Base64.parse(strWikiB64).toString(CryptoJS.enc.Latin1) +nextLine +
                "expected: " + strWikiB64 +nextLine +
                 "actual: " +
CryptoJS.enc.Latin1.parse(strWikiText).toString(CryptoJS.enc.Base64) +
                 "<hr>"
             );

             var testString = "any carnal pleasure.";
             for (var x = testString.length; x > testString.length - 6; x--) {
                 var tempStr = testString.substr(0, x);

                 document.write("Input: " + tempStr + " ; Output: " +
CryptoJS.enc.Latin1.parse(tempStr).toString(CryptoJS.enc.Base64) + '<br>');
             }

</script>

Original comment by lorez...@gmail.com on 12 Aug 2012 at 5:51

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant