2016-01-17 46 views
5

Ho un sistema che richiede una coppia di chiavi RSA da generare in javascript, avere la chiave pubblica quindi memorizzata in un database sul lato server (come stringa), quindi il lato server che è in Java crittograferà una stringa con la chiave pubblica memorizzata e la invierà al lato client che decodificherà la stringa con la chiave privata.La crittografia RSA Java e la decodifica Node.js non funzionano

Sto utilizzando una versione browserizzata di node-rsa sul browser del mio client.

Prima presso il cliente ho generare una coppia di chiavi e di esportare le chiavi, la loro memorizzazione come stringhe

var NodeRSA = require('node-rsa'); 
var key = new NodeRSA({b: 1024}); 
key.exportKey("pkcs8-private"); 
key.exportKey("pkcs8-public-pem"); 

La chiave privata esportato viene memorizzato il client e il pubblico sul server

successivo ho usato java per crittografare una stringa con la chiave pubblica ricevuta, quindi analizzo la chiave pubblica pkcs8 in un oggetto PublicKey Java.

String pubKey = "<Retrieved pkcs8 public key>"; 
pubKey = pubKey.replaceAll("(-+BEGIN PUBLIC KEY-+\\r?\\n|-+END PUBLIC KEY-+\\r?\\n?)", ""); 
byte[] keyBytes = Base64.decodeBase64(pubKey); 
X509EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes); 
KeyFactory kf = KeyFactory.getInstance("RSA"); 
PublicKey pk = kf.generatePublic(spec); 

e cifrare un testo con esso

byte[] cipherText; 
Cipher cipher = Cipher.getInstance("RSA"); 
cipher.init(Cipher.ENCRYPT_MODE, pk); 
cipherText = cipher.doFinal("Hello World!".getBytes()); 
return Base64.encodeBase64String(cipherText); 

che funziona bene e mi restituisce un Base64 cifrato stringa come questa

WTS1J2f4w5icsUOCtulyHDaBmB5lN7D8mnj0QWMDBkUGiPHkM8nHVx9pd0MtbQAQNasQS2X8kisLMYyEMPasFZtDH0zX1e8lNYaW0xMKsg++ge87f+95nl+TmxDy6S1m7Ce/n0wXno+0MbSv8YsJtsUcAleyyfQX2bxqX8u7Gjs= 

poi cerco di decifrare la stringa al lato client

Prima reimportare le chiavi memorizzate in node-rsa

var NodeRSA = require('node-rsa'); 
var key = new NodeRSA(); 
key.importKey("<exported private key string>","pkcs8-private"); 
key.importKey("<exported public key string>","pkcs8-public-pem"); 

poi cerco di decifrare il codifica Base64 crittografato stringa

key.decrypt("<Base64 Encoded Encrypted>", 'utf-8'); 

Questo è dove il problema si verifica, javascript getta questo errore

Uncaught Error: Error during decryption (probably incorrect key). Original error: Error: Error decoding message, the lHash calculated from the label provided and the lHash in the encrypted data do not match.(…) However i have tested that if i encrypt and decrypt the text just within javascript, it works just fine. This makes me think that it's some difference between the way i encrypted it at java and how it's done at javascript

Qualcuno potrebbe far notare l'errore che ho hai fatto qui per favore?

risposta

4

Oh ho trovato la soluzione. Era una differenza nel metodo di crittografia.

ho dovuto inizializzare Cipher con

Cipher.getInstance("RSA/ECB/OAEPWithSHA1AndMGF1Padding"); 

anziché

Cipher.getInstance("RSA"); 

per abbinare nodo-rsa