2015-04-08 20 views
5

Ho una domanda sulla chiave AES e sulla lunghezza IV.CryptoJS e lunghezza chiave/IV

Prima di tutto, se, per esempio, sto usando droghe OpenSSL estensione e openssl_encrypt() metodo, posso vedere chiaramente che fondamentale per a 256-bit AES dovrebbe essere byte, e IV getta avvertimento se è diverso da byte. Posso capirlo, e tutto va bene.

Tuttavia, nella libreria CryptoJS la chiave e la lunghezza IV sono frustranti. Questo è un esempio:

var text = "test", 
    key = "us5N0PxHAWuIgb0/Qc2sh5OdWBbXGady", 
    iv = "zAvR2NI87bBx746n"; 

key = CryptoJS.enc.Base64.parse(key); 
iv = CryptoJS.enc.Base64.parse(iv); 

crypted = CryptoJS.AES.encrypt(text, key, { iv: iv }); 

dove chiave è byte, IV è . CryptoJS richiede di analizzarlo, e dopo CryptoJS.enc.Base64.parse() ottengo 48 e 24 byte di conseguenza. Mi aspetto che tali valori vengano troncati alla lunghezza richiesta da a 256-bit AES, e un'ulteriore espansione a n byte sarà irrilevante, e quindi il testo cifrato risultante sarà lo stesso.

Ma questo non sta realmente accadendo. Quando passo a CryptoJS.A.a.encrypt() una chiave di dimensioni maggiori e anche a IV, sta producendo output diversi. Quindi la mia domanda è, perché? Qual è la differenza tra la libreria CryptoJS e OpenSSL in questo caso?

risposta

5

Sembra che ce l'ho.

Se si tende a passare personalizzati key e IV nell'uso CryptoJS, assicurarsi che (supponendo che CryptoJS.enc.Base64.parse()HEX stringa, che viene utilizzato in CryptoJS.AES.encrypt()).

Prendendo questo esempio, con Base64 chiave e iv (lunghezza = 22), che CryptoJS crittografa come AES-256:

var message = "some_secret_message"; 

var key = "6Le0DgMTAAAAANokdEEial"; //length=22 
var iv = "mHGFxENnZLbienLyANoi.e"; //length=22 

key = CryptoJS.enc.Base64.parse(key); 
//key is now e8b7b40e031300000000da247441226a, length=32 
iv = CryptoJS.enc.Base64.parse(iv); 
//iv is now 987185c4436764b6e27a72f2fffffffd, length=32 

var cipherData = CryptoJS.AES.encrypt(message, key, { iv: iv }); 

var data = CryptoJS.AES.decrypt(cipherData, key, { iv: iv }); 
//data contains "some_secret_message" 

durata del key è di 32 byte per AES -256. (16 byte se si desidera ottenere AES-128. Se più, CryptoJS passerà alla chiave più alta lunghezza). In altri casi su decifrare otterrai un messaggio vuoto. Esempio:

var message = "some_secret_message"; 

var key = "6Le0DgMTAAAAANokdEEial1"; //length=23 
var iv = "mHGFxENnZLbienLyANoi.e"; //length=22 

key = CryptoJS.enc.Base64.parse(key); // length = 17 bytes 
//key is now e8b7b40e031300000000da247441226a5d, length=34 (hex encoded) 
iv = CryptoJS.enc.Base64.parse(iv); // length = 16 bytes 
//iv is now 987185c4436764b6e27a72f2fffffffd, length=32 (hex encoded) 

var cipherData = CryptoJS.AES.encrypt(message, key, { iv: iv }); 

var data = CryptoJS.AES.decrypt(cipherData, key, { iv: iv }); 
//data contains "" - an empty string 

Inoltre, da quello che posso vedere, solo x % 8 == 0 byte di tale caso d'uso dà risultato valido.

Lunghezza IV dovrebbe essere di 22 byte (quando Base64 codificato), e trasformando con CryptoJS.enc.Base64.parse() si ottengono 16 byte (32 esadecimali codificati), che è massima per AES-256 dimensione blocco. Tutto più di questo verrà troncato.

var message = "some_secret_message"; 

var key = "6Le0DgMTAAAAANokdEEial"; //length=22 
var iv = "mHGFxENnZLbienLyANoi.e"; //length=22 

key = CryptoJS.enc.Base64.parse(key); // length=16 bytes 
//key is now e8b7b40e031300000000da247441226a5d, length=32 (hex encoded) 
iv = CryptoJS.enc.Base64.parse(iv); // length=16 bytes 
//iv is now 987185c4436764b6e27a72f2fffffffd, length=32 (hex encoded) 

var cipherData = CryptoJS.AES.encrypt(message, key, { iv: iv }); 

var key = "6Le0DgMTAAAAANokdEEial"; //length=22 
var iv = "mHGFxENnZLbienLyANoi.e123"; //length=25 

key = CryptoJS.enc.Base64.parse(key); // length = 16 bytes 
//key is now e8b7b40e031300000000da247441226a5d, length=32 (hex encoded) 
iv = CryptoJS.enc.Base64.parse(iv); // length = 18 bytes 
//iv is now 987185c4436764b6e27a72f2fffffffded76, length=36 (hex encoded) 

var data = CryptoJS.AES.decrypt(cipherData, key, { iv: iv }); //data contains "some_secret_message", so additional "123" in IV is irrelevant. 
+1

1. C'è qualcosa che non va con i tuoi numeri. AES ha una dimensione di blocco di 128 bit che è anche la dimensione prevista della IV. AES non è specificato per IVs più grandi o più piccoli di 16 byte.La dimensione IV non è uguale alla dimensione della chiave. 2. 22 byte non è una stringa codificata base64 valida. Deve essere divisibile per 4 (con caratteri pad opzionali). –

+0

Oggetto I. AES ha dimensioni del blocco 128 se è AES-128. Nel caso di AES-256 - la dimensione del blocco è, ovviamente, 256 bit, ovvero 32 byte, che è esattamente ciò che si ottiene con 'CryptoJS.enc.Base64.parse()' di 22 byte Base64 stringa. Secondo la specifica e l'algoritmo, IV è esattamente la lunghezza della dimensione del blocco, che è 32 byte con AES-256. – Nevertheless

+0

Per una spiegazione più approfondita, fare riferimento a http://security.stackexchange.com/questions/15740/what-are-the-variables-of-aes – Nevertheless