Comments (10)
You will likely want to use a base64 decoding library. I am still debating whether to include a base64 coder in the utils, as it is a common use case, but often there are existing libraries available.
Are you using node.js? Or have the SlowBuffer available? If so, the Buffer object already supports base64 conversion.
node.js:
> a = new Buffer([1, 2, 3])
<Buffer 01 02 03>
> a.toString('base64')
'AQID'
> new Buffer('AQID', 'base64')
<Buffer 01 02 03>
Browser:
> btoa('\01\02\03')
< "AQID"
> atob('AQID')
< "\01\02\03"
from aes-js.
I am trying to impelement a decryption on the new Salesforce.com lighting interface. This is an Aura environment. Strangely there is something in java that i am missing in javascript.
i have now fetched the byte array from the Java application and copied that one over to the JavaScript side of things. This makes that I should not worry about the correct encoding of the pass. What I find is that the Java encoding of the pass is giving a different encoding then when I do the same on JavaScript. While on Java, the array returns me a singed range of negative and positive, the btoa returns me all positive numbers, so an unsigned array representation.
The UTF-8 encoding of the value returns the same byte array.
I have changed your JavaScript so that the check on the integers always returns true. This to prevent the fact that negative byte elements could not be used. The encryption now "works", however returns a different result than my Java one. This is also not meant to be of course.
from aes-js.
with the encrypted version, based on allowing in the encryption the signed byta array, I now get on the JavaScript side:
[186, 172, 108, 98, 188, 21, 126, 22, 109, 193, 210, 5, 85, 252, 137, 29, 166, 18, 238, 154, 63, 207, 211, 40, 200, 23, 70, 26, 186, 23, 9, 220]
on the Java side I get the signed version of this Byte Array
[-70, -84, 108, 98, -68, 21, 126, 22, 109, -63, -46, 5, 85, -4, -119, 29, -90, 18, -18, -102, 63, -49, -45, 40, -56, 23, 70, 26, -70, 23, 9, -36]
This means that actually there is progress.
I now need to convert this to the same String version.
On the Java side I am using sun.misc.BASE64Encoder().encode(enc).
The issue I am having really seems to be around moving from String to Byte Array and the other way around then. atob and btoa give completely different results than the sun.... version.
from aes-js.
from aes-js.
There is only one AES standard. There are multiple modes of operation though, maybe this is what you are encountering in the different online tools? Or perhaps the padding method (cipher stealing vs. PKCS#7)
The above array is equal if you ignore the signs and just take the twos-compliment. Is there no unsigned byte array in Java?
In Base64 though, the signs do not matter since Base64 is a binary encoding, and does not have signs. The signs only happen once you decode into values. You should not need to modify the aes-js library.
Are you trying to decode the base64 data in a browser? Or in node?
For browsers, you can simply do something akin to:
var output = [];
var bytes = atob('AQID');
for (var i = 0; i < bytes.length; i++) {
output.push(s.charCodeAt([i]));
}
For node, you can simply use:
var output = new Buffer('AQID', 'base64');
from aes-js.
In the last version, I now can encrypt the value to the same result as the one I am getting on Java ECB.
I just need to avoid the pwd base64 encoding and change the aes js library to accept signed byte array (no check on <0, later on if I find a way to base64 encode decently i can drop changing the aes js of course).
var key = [12, -45, ....];
var value = 'toEncrypt';
var valueAsBytes = aesjs.utils.utf8.toBytes(value);
var paddedData = aesjs.padding.pkcs7.pad(valueAsBytes);
var aesEcb = new aesjs.ModeOfOperation.ecb(key);
var encryptedBytes = aesEcb.encrypt(paddedData);
var base64String = helper.arrayBufferToBase64(encryptedBytes);
where helper contains
arrayBufferToBase64 : function (str){
var binary = '';
var bytes = new Uint8Array( str );
var len = bytes.byteLength;
for (var i = 0; i < len; i++) {
binary += String.fromCharCode( bytes[ i ] );
}
return window.btoa( binary );
}
For decrypting, I however now really need to get a base64 encoding that represents the one in Java.
So "hello world" needs to translate to
[-123, -23, 101, -93, -4, 40, -82, 87, 104]
or the unsigned version of it.
[133,233,101,163,252,40,174,87,104]
btoa proposed above gives
[133, 233, 101, 163, 10, 43, 149]
from aes-js.
OK, all working now.
so using the functions
arrayBufferToBase64 : function (str){
var binary = '';
var bytes = new Uint8Array( str );
var len = bytes.byteLength;
for (var i = 0; i < len; i++) {
binary += String.fromCharCode( bytes[ i ] );
}
return window.btoa( binary );
},
base642ArrayBuffer : function (str){
var binary_string = window.atob(str);
var len = binary_string.length;
var bytes = new Uint8Array(len);
for (var i = 0; i < len; i++) {
bytes[i] = binary_string.charCodeAt(i);
}
return bytes;
}
makes that i can do the encryption/decryption with the method
doDecrypt : function(component, event, helper) {
var origKey = 'base64encodedkey';
var key = helper.base642ArrayBuffer(origKey);
var value = 'encrypted stuff';
var valueAsBytes = helper.base642ArrayBuffer(value);
console.log('valueAsBytes : ', valueAsBytes);
var aesEcb = new aesjs.ModeOfOperation.ecb(key);
var decryptedBytes = aesEcb.decrypt(valueAsBytes);
var decryptedText = aesjs.utils.utf8.fromBytes(decryptedBytes);
console.log('decrypt result : ',decryptedText);
},
doEncrypt : function(component, event, helper) {
var origKey = 'base64encodedkey';
var key = helper.base642ArrayBuffer(origKey);
var value = 'somevalue';
var valueAsBytes = aesjs.utils.utf8.toBytes(value);
var paddedData = aesjs.padding.pkcs7.pad(valueAsBytes);
console.log('paddedvaluebyte : ',paddedData);
var aesEcb = new aesjs.ModeOfOperation.ecb(key);
var encryptedBytes = aesEcb.encrypt(paddedData);
var base64String = helper.arrayBufferToBase64(encryptedBytes);
console.log('encrypt result : ',base64String);
return base64String;
}
from aes-js.
Thanks for posting your solution. It works for me.
But I cannot resist the urge to scratch out my eyes seeing your method names dude:
base642ArrayBuffer
is just heavy. Why would you replace 'To' with a numerical '2'?? You're headed for so much maintainability pain down the road.... btw: arrayBufferToBase64
has a 'To' in it. What?
Sorry, this is just something you cannot leave uncommented when your mind is set to writing 'clean' code. 😆
from aes-js.
@sylverg so this issue can be closed?
from aes-js.
from aes-js.
Related Issues (20)
- Big endian HOT 2
- Is this still maintained? HOT 2
- Transform key and iv bytes in base64/Hex for another languages decript HOT 1
- export an es module HOT 2
- CBC encrypt and decrypt is wrong on output HOT 3
- CTR mode: Counter increment issue with 2 different IVs HOT 1
- Uncaught Error: invalid plaintext size (must be 16 bytes) HOT 3
- Recommending CTR is dangerous, if the same key is used twice HOT 7
- Encrypt CBC padding 7 is different from Java HOT 6
- Please add index.min.js
- need php/openssl advice HOT 1
- Make note of `yarn add -D @types/aes-js` in README HOT 1
- Error: invalid plaintext size (must be multiple of 16 bytes) HOT 6
- version 2.1.1 , ECB
- Incorrect UTF-8 decoding HOT 2
- Getting Chinese letters
- Should provide method to generate IV HOT 2
- Feature Request: Import key from hex HOT 1
- how do I encrypt images with this module
- How to convert a string to key? HOT 2
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 aes-js.