Comments (3)
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.
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.
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)
- AES encrypted string to be converted to base 64 string and then to byte array in Javascript
- Uncaught TypeError: Cannot read property 'init' of undefined HOT 1
- Clarify license HOT 2
- Automatic key expiration
- Type a is undefined in rollup version of aes.js line 27. HOT 4
- Crypto.JS PBKDF2 Results Differnt then .Net Rfc2898DeriveBytes HOT 1
- Crypto-js decryption implemention HOT 1
- Most of the hashs wont works regarding file encoding HOT 4
- Not able to decrypt the encrypted data on server side which was encrypted using CryproJS on client side..
- TripleDES result is different each time, and is not correct
- AES 256 decrypt from data oracle function encrypting HOT 1
- Broken hashing with sigBytes< bytes
- Error: Unable to get property 'createEncryptor' of undefined or null reference
- [IE8]Object doesn't support property or method
- decrypt of ciphertext not working for strings HOT 2
- [documentation] Add recommendation to use https
- Using pbkdf2Sync function with sha256 gives different key with different system architecture (32 bit and 64 bit) HOT 2
- After encryption CryptoJS returning script then encrypted text
- I am using CryptoJS MD5 algorith for large files, it is very very very slow, how to icrease performance
- Sha3 result different for long strings on android 4.2.2 webview
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from crypto-js.