2010-04-16 27 views
13

PKCS8 DER formatında dosyada saklı ve parola ile korunan özel anahtarım var. Okumanın en kolay yolu nedir?Java ile şifre şifreli anahtar nasıl okunur?

InputStream in = new FileInputStream(privateKeyFilename); 
byte[] privateKeydata = new byte[in.available()]; 
in.read(privateKeydata); 
in.close(); 
KeyFactory privateKeyFactory = KeyFactory.getInstance("RSA"); 
PKCS8EncodedKeySpec encodedKeySpec = new PKCS8EncodedKeySpec(privateKeydata); 
PrivateKey privateKey = privateKeyFactory.generatePrivate(encodedKeySpec); 

Aynı şartnameye şifrelenmemiş tuşlar için çalışıyor: İşte

Bir şifresiz yüklemek için kullanın koddur. Bu arada, BouncyCastle kullanıyorum.

Ben Yardım Lütfen

openssl pkcs8 -in ./privatekey.key -inform DER -passin pass:thisismypass 

openssl komutunun ardından kullanarak bu özel anahtarı görebilirsiniz !!!

Bu konuya kendi yanıtımla bazı çözümler gönderdim. Ancak, herhangi bir kişinin ekstra kütüphane olmadan çalışmasına yardım etmesi durumunda cevap vermemeye çalışıyorum, sadece BouncyCastle.

cevap

7

Çözümü buldum! Belki onun kadar zarif değil ama yayınlayacağız İşte ... iki çözüm:

  1. prefferable, ancak biri Çalışma
  2. çalışmıyor ama

İlk ek kütüphane gerektirir :

Bir çeşit çözüm buldum here, ancak istisna atar. Çözüm:

import java.io.*; 
import java.security.*; 
import java.security.interfaces.RSAPrivateCrtKey; 
import java.security.interfaces.RSAPublicKey; 
import java.security.spec.*; 

import javax.crypto.*; 
import javax.crypto.spec.*; 

/* 
* This class demonstrates how to import an encrypted RSA private key as 
* generated by openssl. The input file is presumed to be in DER 
* format. 
*/ 
public class ImportEncryptedPrivateKey 
{ 
    public static byte[] readPK8FromFile(String fileName) throws IOException 
    { 
     File f = new File(fileName); 
     DataInputStream dis = new DataInputStream(new FileInputStream(f)); 
     byte[] theData = new byte[(int) f.length()]; 
     dis.readFully(theData); 
     return theData; 
    } 

    public static void main(String[] args) throws IOException, 
      NoSuchAlgorithmException, NoSuchPaddingException, 
      InvalidKeySpecException, InvalidKeyException, 
      InvalidAlgorithmParameterException 
    { 
     byte[] encryptedPKInfo = readPK8FromFile("rsapriv.pk8"); 
     EncryptedPrivateKeyInfo ePKInfo = new EncryptedPrivateKeyInfo(
       encryptedPKInfo); 
     char[] password = { 'p', 'a', 's', 's', 'w', 'o', 'r', 'd' }; 
     Cipher cipher = Cipher.getInstance(ePKInfo.getAlgName()); 
     PBEKeySpec pbeKeySpec = new PBEKeySpec(password); 
     // Now create the Key from the PBEKeySpec 
     SecretKeyFactory skFac = SecretKeyFactory.getInstance(ePKInfo 
       .getAlgName()); 
     Key pbeKey = skFac.generateSecret(pbeKeySpec); 
     // Extract the iteration count and the salt 
     AlgorithmParameters algParams = ePKInfo.getAlgParameters(); 
     cipher.init(Cipher.DECRYPT_MODE, pbeKey, algParams); 
     // Decrypt the encryped private key into a PKCS8EncodedKeySpec 
     KeySpec pkcs8KeySpec = ePKInfo.getKeySpec(cipher); 
     // Now retrieve the RSA Public and private keys by using an 
     // RSA keyfactory. 
     KeyFactory rsaKeyFac = KeyFactory.getInstance("RSA"); 
     // First get the private key 
     RSAPrivateCrtKey rsaPriv = (RSAPrivateCrtKey) rsaKeyFac.generatePrivate(pkcs8KeySpec); 
     // Now derive the RSA public key from the private key 
     RSAPublicKeySpec rsaPubKeySpec = new RSAPublicKeySpec(rsaPriv.getModulus(), rsaPriv.getPublicExponent()); 
     RSAPublicKey rsaPubKey = (RSAPublicKey) rsaKeyFac.generatePublic(rsaPubKeySpec); 
    } 

} 

Ve istisna:

Exception in thread "main" java.security.NoSuchAlgorithmException: No such algorithm: 1.2.840.113549.1.5.13 

İkinci:

Ve bu çözüm

+0

+1. O zaman Bouncy Kalesi'ne bile ihtiyacın yok. – Thilo

+1

İkinci çözüm için. Kodu buraya yapıştırabilir misin? Bu bağlantıyı açamıyorum. Hangi kütüphaneyi kullandın? – BRabbit27

+0

Bu aslında benim için çalışıyor. –

1

çalışma gidip ikinciye okuyabilirsiniz bu http://juliusdavies.ca/commons-ssl/pkcs8.html olduğu aşağıdakiler benim kod ve iş yeriniz :)

Not-yet-commons-ssl için
File f = new File(keyFile); 
FileInputStream fis = new FileInputStream(f); 
DataInputStream dis = new DataInputStream(fis); 
byte[] keyBytes = new byte[(int)f.length()]; 
dis.readFully(keyBytes); 
dis.close(); 
EncryptedPrivateKeyInfo encryptPKInfo = new EncryptedPrivateKeyInfo(keyBytes); 
Cipher cipher = Cipher.getInstance(encryptPKInfo.getAlgName()); 
PBEKeySpec pbeKeySpec = new PBEKeySpec(passwd.toCharArray()); 
SecretKeyFactory secFac = SecretKeyFactory.getInstance(encryptPKInfo.getAlgName()); 
Key pbeKey = secFac.generateSecret(pbeKeySpec); 
AlgorithmParameters algParams = encryptPKInfo.getAlgParameters(); 
cipher.init(Cipher.DECRYPT_MODE, pbeKey, algParams); 
KeySpec pkcs8KeySpec = encryptPKInfo.getKeySpec(cipher); 
KeyFactory kf = KeyFactory.getInstance("RSA"); 
return kf.generatePrivate(pkcs8KeySpec); 
+1

Sadece JDK (Bouncy Castle yok) ile "1.2.840.113549.1.5.13 destekleyen herhangi bir sağlayıcı bulamıyorum" (PBEWithMD5AndDES için olan id). – Thilo

+1

Sadece java API ile özel anahtarı okumayı başardınız mı? – BRabbit27

İlgili konular