2012-05-09 16 views
8

ho bisogno di sostituire il crittografare e decrittografare passo da Unix a codice Java con i tasti rsaprivatekey.pem e rsapublickey.pem generati con OpenSSLCome cripta decripta con chiavi RSA in java

ho generare le chiavi

openssl genrsa -out /tmp/rsaprivatekey.pem -des3 1024 
openssl rsa -in /tmp/rsaprivatekey.pem -pubout -out /tmp/rsapublickey.pem 

io uso le chiavi a UNIX (ho bisogno di farlo in java)

echo "Text to encript"| openssl rsautl -encrypt -inkey /tmp/rsapublickey.pem -pubin -out out.enc 
openssl rsautl -decrypt -inkey /tmp/rsaprivatekey.pem -in out.enc 

Questo è stato il mio tentativo di farlo

public static void main(String[] args) { 


    Base64 base64 = new Base64(); 

    String TextStream = "this is the input text"; 
    byte[] Cipher; 
    System.out.println("input:\n" + TextStream); 
    Cipher = encrypt(TextStream); 
    System.out.println("cipher:\n" + base64.encodeAsString(Cipher)); 
    System.out.println("decrypt:\n" + decrypt(Cipher)); 
} 

private static byte[] encrypt(String Buffer) { 
    try { 

     Cipher rsa; 
     rsa = Cipher.getInstance("RSA"); 
     rsa.init(Cipher.ENCRYPT_MODE, getPrivateKey(PRIVATE_PATH)); 
     return rsa.doFinal(Buffer.getBytes()); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return null; 
} 


private static String decrypt(byte[] buffer) { 
    try { 
     Cipher rsa; 
     rsa = Cipher.getInstance("RSA"); 
     rsa.init(Cipher.DECRYPT_MODE, getPrivateKey(PUBLIC_PATH)); 
     byte[] utf8 = rsa.doFinal(buffer); 
     return new String(utf8, "UTF8"); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return null; 
} 

public static PrivateKey getPrivateKey(String filename) throws Exception { 
    File f = new File(filename); 
    FileInputStream fis = new FileInputStream(f); 
    DataInputStream dis = new DataInputStream(fis); 
    byte[] keyBytes = new byte[(int) f.length()]; 
    dis.readFully(keyBytes); 
    dis.close(); 

    PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes); 
    KeyFactory kf = KeyFactory.getInstance("RSA"); 
    return kf.generatePrivate(spec); 
} 

public static PublicKey getPublicKey(String filename) throws Exception { 
    File f = new File(filename); 
    FileInputStream fis = new FileInputStream(f); 
    DataInputStream dis = new DataInputStream(fis); 
    byte[] keyBytes = new byte[(int) f.length()]; 
    dis.readFully(keyBytes); 
    dis.close(); 

    X509EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes); 
    KeyFactory kf = KeyFactory.getInstance("RSA"); 
    return kf.generatePublic(spec); 
} 

ma non funziona, il PKCS8EncodedKeySpec/X509EncodedKeySpec non sono corretti ... ma non so cosa mettere

+0

e il problema è ...? –

+0

il mio tentativo non ha funzionato – caeycae

+2

[Stack Overflow non è un lettore Mind.] (Http://meta.stackexchange.com/a/128551/133242) –

risposta

5
Non

sicuro esattamente quello che stai chiedendo, ma penso che si hanno problemi di lettura File PEM. JPA non supporta direttamente il formato PEM. Hai due opzioni, o convertirle in file codificati DER (puoi usare openSSL per farlo) o puoi usare l'API bouncy castle per leggere (o scrivere) i file PEM. la classe che ti interessa è chiamata PEMReader (e forse anche PEMWriter). Here is the Javadoc sul sito web di bouncycastle.

12

Soluzione:

Grazie a @Sanjeev, utilizzando l'API castello gonfiabile, ero in grado di Encript/decript con le chiavi generate da OpenSSL

public static void main(String[] args) throws IOException { 

    Security.addProvider(new BouncyCastleProvider()); 

    KeyPair keyPair = readKeyPair(new File(PRIVATE_PATH), "pass"); 
    // if the private key is not encripted, pass can be anything. 
    Key publickey = readPublicKey(new File(PUBLIC_PATH), "pass"); 
    Base64 base64 = new Base64(); 
    String text = "this is the input text"; 
    byte[] encripted; 
    System.out.println("input:\n" + text); 
    encripted = encrypt(keyPair.getPublic(), text); 
    System.out.println("cipher:\n" + base64.encodeAsString(encripted)); 
    System.out.println("decrypt:\n" + decrypt(keyPair.getPrivate(), encripted));   
} 

private static byte[] encrypt(Key pubkey, String text) { 
    try { 
     Cipher rsa; 
     rsa = Cipher.getInstance("RSA"); 
     rsa.init(Cipher.ENCRYPT_MODE, pubkey); 
     return rsa.doFinal(text.getBytes()); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return null; 
} 


private static String decrypt(Key decryptionKey, byte[] buffer) { 
    try { 
     Cipher rsa; 
     rsa = Cipher.getInstance("RSA"); 
     rsa.init(Cipher.DECRYPT_MODE, decryptionKey); 
     byte[] utf8 = rsa.doFinal(buffer); 
     return new String(utf8, "UTF8"); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return null; 
} 

private static KeyPair readKeyPair(File privateKey, String keyPassword) throws IOException { 
    FileReader fileReader = new FileReader(privateKey); 
    PEMReader r = new PEMReader(fileReader, new DefaultPasswordFinder(keyPassword.toCharArray())); 
    try { 
     return (KeyPair) r.readObject(); 
    } catch (IOException ex) { 
     throw ex; 
    } finally { 
     r.close(); 
     fileReader.close(); 
    } 
} 

private static Key readPublicKey(File privateKey, String keyPassword) throws IOException { 
    FileReader fileReader = new FileReader(privateKey); 
    PEMReader r = new PEMReader(fileReader, new DefaultPasswordFinder(keyPassword.toCharArray())); 
    try { 
     return (RSAPublicKey) r.readObject(); 
    } catch (IOException ex) { 
     throw ex; 
    } finally { 
     r.close(); 
     fileReader.close(); 
    } 
} 
+1

Non dovrebbe essere "UTF-8"? – drew

+4

Ciao @caeycae, ti sto seguendo, anche io ho dei problemi. In quale JAR/Library contiene DefaultPasswordFinder? – danisupr4

+0

Hai trovato una soluzione per quale file jar dobbiamo includere per DefaultPasswordFinder? – sasi

Problemi correlati