2014-04-06 18 views
5

Sto utilizzando nella mia app crittografia RSA. Per memorizzare la chiave pubblica generata, la converto in stringa e la salvo nel database.Chiave da String in Java RSA

Key publicKey=null; 
    Key privateKey=null; 

    KeyPair keyPair=RsaCrypto.getKeyPairRSA(1024); 
    publicKey=keyPair.getPublic(); 
    privateKey=keyPair.getPrivate(); 



    String publicK=Base64.encodeToString(publicKey.getEncoded(), Base64.DEFAULT); 
    String privateK=Base64.encodeToString(privateKey.getEncoded(), Base64.DEFAULT); 

ho salvare le stringhe publicK e privateK. Il mio problema è che quando voglio crittografare/decifrare il testo con RSA e usare la mia chiave salvata in formato stringa non so come convertirlo in Key.

public static String encrypt(Key publicKey, String inputText){ 
    byte[]encodedBytes=null; 
    String encryptedText=""; 
    try { 
     Cipher cipher=Cipher.getInstance("RSA"); 
     cipher.init(Cipher.ENCRYPT_MODE, publicKey); 
     encodedBytes=cipher.doFinal(inputText.getBytes()); 
    } catch (Exception e) {Log.e("Error", "RSA encryption error"); } 

    encryptedText=Base64.encodeToString(encodedBytes, Base64.DEFAULT); 
    return encryptedText; 
} 

Avete qualche idea? Grazie mille

+0

favore, per favore, per favore non memorizzare le chiavi private non crittografate nel tuo database. Se devi archiviare chiavi private conosciute come escrowing e dovresti crittografarle con una chiave simmetrica nota solo alla tua applicazione. – KyleM

+0

Grazie, sì ci sto pensando. Potrebbe darmi un consiglio, qual è il modo migliore per memorizzare una chiave in app? Grazie – rgreso

+0

utilizzare un keystore Java protetto da password. Cerca il keytool Java ... – KyleM

risposta

7

Per convertire Publick (String) per Public Key fare come di seguito:

byte[] keyBytes = Base64.decode(publicK.getBytes("utf-8")); 
X509EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes); 
KeyFactory keyFactory = KeyFactory.getInstance("RSA"); 
PublicKey key = keyFactory.generatePublic(spec); 

Per convertire privateK (String) per chiave privata fare come di seguito:

byte[] keyBytes = Base64.decode(privateK.getBytes("utf-8")); 
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes); 
KeyFactory fact = KeyFactory.getInstance("RSA"); 
PrivateKey priv = fact.generatePrivate(keySpec); 
+0

grazie per la risposta. Eclipse mi mostra un errore: il metodo decodeBase64 (byte []) non è definito per il tipo Base64. Potresti aiutarmi con questo per favore? grazie mille – rgreso

+0

@rgreso prova questo codice. – Rahil2952

+0

Grazie per la risposta ma mostra ancora un errore, questa volta vuole un altro parametro: Il metodo decodifica (byte [], int) nel tipo Base64 non è applicabile per gli argomenti (byte []). Hai qualche idea? Grazie mille per il tuo tempo – rgreso

1

Could not in grado di commentare il post precedente. Vorrei sapere che cosa è "chiaro" in

PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(clear); 
0

Posso solo supporre la linea:

PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(clear); 

con "chiaro" voleva essere "keyBytes"

Problemi correlati