में आरएसए कुंजी के साथ डिक्रिप्ट मैं एन्क्रिप्ट की जगह और 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 सही नहीं हैं ... लेकिन मैं नहीं जानता कि क्या
द्वारा उत्पन्न कुंजी के साथ/decript Encript करने में सक्षम था और समस्या है ...? –
मेरा प्रयास – caeycae
काम नहीं करता [स्टैक ओवरफ़्लो एक मन रीडर नहीं है।] (Http://meta.stackexchange.com/a/128551/133242) –