Giter Club home page Giter Club logo

Comments (10)

ricmoo avatar ricmoo commented on July 28, 2024

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.

sylverg avatar sylverg commented on July 28, 2024

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.

sylverg avatar sylverg commented on July 28, 2024

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.

Ruffio avatar Ruffio commented on July 28, 2024

from aes-js.

ricmoo avatar ricmoo commented on July 28, 2024

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.

sylverg avatar sylverg commented on July 28, 2024

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.

sylverg avatar sylverg commented on July 28, 2024

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.

CodingMeSwiftly avatar CodingMeSwiftly commented on July 28, 2024

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.

Ruffio avatar Ruffio commented on July 28, 2024

@sylverg so this issue can be closed?

from aes-js.

sylverg avatar sylverg commented on July 28, 2024

from aes-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.