brix / crypto-js

JavaScript library of crypto standards.
Other
15.87k stars 2.39k forks source link

help #65

Open ghost opened 8 years ago

ghost commented 8 years ago

I use python Crypto lib, and javascript crypto-js lib implements encrypt a username, but something is wrong with the output result.

# python2.7.12
from Crypto.Cipher import AES
import base64

KEY = 'ABCDEF1234567890'
IV = 'ABCDEF1234567890'

aes = AES.new(KEY, AES.MODE_ECB, IV)

user = 'root'
user = user + ' ' * (16-len(user))
en_user = aes.encrypt(user)
en_user = base64.b64encode(en_user)
#This is output
print 'en_user', en_user # en_user tPL98ukMrLRcVXPk6zQMFQ==
/*javascript*/
KEY = 'ABCDEF1234567890';
IV = 'ABCDEF1234567890';
var user='root';

for(tmp=0;tmp<16-user.length;tmp++)
{
    user = user + ' ';
}
var en_user = CryptoJS.AES.encrypt(user, KEY, {iv:IV, mode:CryptoJS.mode.ECB,
                    padding:CryptoJS.pad.NoPadding}).toString();            
en_user = btoa(en_user);
//This is output
console.log(en_user); //VTJGc2RHVmtYMS83UDRRcnpUWDFJUlJlblRTZFlvenRta2M9

What's wrong?

chiedo commented 8 years ago

I can't say for sure but you may need to update your first two lines of code to this.

KEY = CryptoJS.enc.Utf8.parse('ABCDEF1234567890');
IV = CryptoJS.enc.Utf8.parse('ABCDEF1234567890');

It was my understanding that both of those need to be CryptoJS WordArrays.

I could be misunderstood but it's worth a shot :)

evanvosberg commented 7 years ago

Did you try the suggested CryptoJS.enc.Utf8.parse(...)?