2012-05-09 15 views
8

में आरएसए कुंजी के साथ डिक्रिप्ट मैं एन्क्रिप्ट की जगह और openssl साथ उत्पन्न rsaprivatekey.pem और rsapublickey.pem कुंजी के साथ जावा कोड को यूनिक्स से कदम डिक्रिप्ट करने के लिए की जरूरत हैजावा

मैं कुंजी उत्पन्न एन्क्रिप्ट करने के लिए कैसे

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

मैं यूनिक्स में कुंजी का उपयोग

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 

(मैं जावा में कर की जरूरत है) यह यह

करने के लिए मेरे प्रयास किया गया था
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); 
} 

लेकिन ऐसा नहीं काम करता है, PKCS8EncodedKeySpec/X509EncodedKeySpec सही नहीं हैं ... लेकिन मैं नहीं जानता कि क्या

+0

द्वारा उत्पन्न कुंजी के साथ/decript Encript करने में सक्षम था और समस्या है ...? –

+0

मेरा प्रयास – caeycae

+2

काम नहीं करता [स्टैक ओवरफ़्लो एक मन रीडर नहीं है।] (Http://meta.stackexchange.com/a/128551/133242) –

उत्तर

5
नहीं

सुनिश्चित करें कि आप वास्तव में क्या कह रहे हैं डाल करने के लिए, लेकिन मैं आपको पढ़ने में समस्या हो रही लगता है पीईएम फाइलें जेपीए सीधे पीईएम प्रारूप का समर्थन नहीं करता है। आपके पास दो विकल्प हैं, या तो उन्हें डीईआर एन्कोडेड फ़ाइलों में परिवर्तित करें (आप इसे करने के लिए ओपनएसएसएल का उपयोग कर सकते हैं) या आप पीईएम फाइलों को पढ़ने (या लिखने) के लिए उछाल वाले महल एपीआई का उपयोग कर सकते हैं। जिस वर्ग में आप रुचि रखते हैं उसे पीईएमआरडर (और शायद पीईएमवाइटर) भी कहा जाता है। Bouncycastle वेबसाइट पर Here is the Javadoc

12

समाधान:

@Sanjeev के लिए धन्यवाद, उछाल वाले महल एपीआई का उपयोग कर, मैं 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

यह "यूटीएफ -8" नहीं होना चाहिए? – drew

+4

हाय @caeycae, मैं आपके पोस्ट का अनुसरण कर रहा हूं, मुझे इस पर भी परेशानी है। जिसमें जेएआर/लाइब्रेरी में DefaultPasswordFinder है? – danisupr4

+0

क्या आपको एक समाधान मिला है जिसके लिए हमें जार फ़ाइल है जिसे हमें डिफ़ॉल्ट पासवर्डवर्डर के लिए शामिल करना है? – sasi