मुझे पता है कि यह थोड़ा देर हो चुकी है, लेकिन मैं यह करने के लिए भी एक रास्ता तलाश रहा हूं और जब मैं खोज रहा था तो मुझे आपका प्रश्न मिला, अब मुझे यह करने का कोई तरीका मिला है, मैंने वापस आने और साझा करने का फैसला किया इस:
// generate key pair
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(1024);
KeyPair keyPair = keyPairGenerator.genKeyPair();
// extract the encoded private key, this is an unencrypted PKCS#8 private key
byte[] encodedprivkey = keyPair.getPrivate().getEncoded();
// We must use a PasswordBasedEncryption algorithm in order to encrypt the private key, you may use any common algorithm supported by openssl, you can check them in the openssl documentation http://www.openssl.org/docs/apps/pkcs8.html
String MYPBEALG = "PBEWithSHA1AndDESede";
String password = "pleaseChangeit!";
int count = 20;// hash iteration count
SecureRandom random = new SecureRandom();
byte[] salt = new byte[8];
random.nextBytes(salt);
// Create PBE parameter set
PBEParameterSpec pbeParamSpec = new PBEParameterSpec(salt, count);
PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray());
SecretKeyFactory keyFac = SecretKeyFactory.getInstance(MYPBEALG);
SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec);
Cipher pbeCipher = Cipher.getInstance(MYPBEALG);
// Initialize PBE Cipher with key and parameters
pbeCipher.init(Cipher.ENCRYPT_MODE, pbeKey, pbeParamSpec);
// Encrypt the encoded Private Key with the PBE key
byte[] ciphertext = pbeCipher.doFinal(encodedprivkey);
// Now construct PKCS #8 EncryptedPrivateKeyInfo object
AlgorithmParameters algparms = AlgorithmParameters.getInstance(MYPBEALG);
algparms.init(pbeParamSpec);
EncryptedPrivateKeyInfo encinfo = new EncryptedPrivateKeyInfo(algparms, ciphertext);
// and here we have it! a DER encoded PKCS#8 encrypted key!
byte[] encryptedPkcs8 = encinfo.getEncoded();
इस उदाहरण कोड folowing कोड मैंने पाया पर आधारित है: http://www.jensign.com/JavaScience/PEM/EncPrivKeyInfo/EncPrivKeyInfo.java
लेकिन folowing संसाधन भी मुझे मदद की एक छोटा सा बेहतर समझने के लिए: http://java.sun.com/j2se/1.4.2/docs/guide/security/jce/JCERefGuide.html
'रैंडम यादृच्छिक = नए रैंडम(); 'सिक्योररैंडम आर होना चाहिए एंडोम = नया सिक्योररैंडम(); ' – Carsten
आपके सुझाव कार्स्टन के लिए धन्यवाद! – Hrzio
जेडीके 8 के रूप में, किसी को 'सिक्योररैंडम यादृच्छिक = SecureRandom.getInstanceStrong();' यह सुनिश्चित करने के लिए उपयोग करना चाहिए कि आप ओरेकल से इस दस्तावेज़ द्वारा सुझाए गए मजबूत कार्यान्वयन का उपयोग कर रहे हैं: https://docs.oracle.com/javase/tutorial /security/apisign/step2.html – CrashproofCode