Coder Social home page Coder Social logo

Comments (3)

GoogleCodeExporter avatar GoogleCodeExporter commented on May 27, 2024
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 [email protected] on 12 Aug 2012 at 1:09

from crypto-js.

GoogleCodeExporter avatar GoogleCodeExporter commented on May 27, 2024
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

from crypto-js.

GoogleCodeExporter avatar GoogleCodeExporter commented on May 27, 2024
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 [email protected] on 12 Aug 2012 at 5:51

from crypto-js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.