2017-04-21 1 views
1

J'ai été la chasse à Internet pour les 3 dernières heures et j'ai finalement décidé de poser la question. Il est possible de décrypter une chaîne de texte base64 dans NodeJS qui a été cryptée via PHP.PHP mcrypt_decrypt dans NodeJS

J'ai essayé de nombreuses étapes pour le décomposer, mais rien de ce que je fais ne semble fonctionner.

Mes anciennes méthodes PHP QUI FONCTIONNENT.

class EncryptionSystem{ 
    private $iv; 
    public $iv_size; 
    public $key; 

    public function __construct(){ 
     $this->iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC); 
     $this->iv = mcrypt_create_iv($this->iv_size, MCRYPT_RAND); 
     $this->key = "SUPER SECURE STRING"; 
    } 

    public function crypt_data($data = null){ 
     $ciphertext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $this->key, $data, MCRYPT_MODE_CBC, $this->iv); 
     $ciphertext = $this->iv.$ciphertext; 
     return $ciphertext; 
    } 

    public function decrypt_data($data){ 
     $ciphertext_dec = base64_decode($data); 
     $iv_dec = substr($ciphertext_dec, 0, $this->iv_size); 
     $ciphertext_dec = substr($ciphertext_dec, $this->iv_size); 
     $plaintext_dec = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $this->key, $ciphertext_dec, MCRYPT_MODE_CBC, $iv_dec); 
     return array($iv_dec,$plaintext_dec); 
    } 
} 

Ma tentative de NodeJS en utilisant Mcrypt (ne fonctionne pas)

var post64 = results[0].post; 
var de64 = Buffer.from(post64, 'base64'); 
var desCBC = new MCrypt("rijndael-128","cbc"); 

var iv = de64.slice(0,desCBC.getIvSize()); 
var ciphered = de64.slice(desCBC.getIvSize()); 

console.log("IV:",iv); 
console.log(iv.length); 
console.log("IV SIZE:",desCBC.getIvSize()); 
desCBC.open(new Buffer(password.slice(0,desCBC.getIvSize(),"utf8")),new Buffer(iv,"utf8")); 

var plaintext = desCBC.decrypt(new Buffer(ciphered,"utf8")); 
console.log(plaintext.toString()); 

Une autre tentative à l'aide Crypto (ne fonctionne pas aussi)

console.log("Base64",post64); 
console.log("RAW",de64.toString("binary")); 
var iv = de64.toString("binary").slice(0,16); 
var ciphered = de64.toString("binary").slice(16); 

console.log("IV:",iv); 
console.log(iv.length); 
console.log("Ciphered:",ciphered); 

var decipher = crypto.createDecipheriv(algorithm,password,new Buffer(iv,'hex')); 

Répondre

0

Essayez module d'utilisation crypto-js. Cela fonctionne bien pour moi!

aesDecryptDef(cipherData) { 
    let CryptoJS = require("crypto-js"), 
     key = CryptoJS.enc.Utf8.parse(this.AES_KEY), 
     iv = CryptoJS.enc.Utf8.parse(this.AES_IV), 
     decrypted = CryptoJS.AES.decrypt(cipherData, key, { 
      iv: iv 
     }); 
    return decrypted.toString(CryptoJS.enc.Utf8); 
}; 
+0

Voir le problème est que j'ai un générateur aléatoire IV pour chaque donnée qui est enregistrée pour plus de sécurité. L'IV est stocké au début des données cryptées. –