2017-09-21 1 views
1

私はRSA 2048キーで暗号化と復号化を行います。しかし、セキュリティをさらに強化するために、私はAES 128メソッドでRSAPrivateKeyのパスフレーズを使用する必要があります。JAVAのRSAPrivateKeyにパスフレーズを使用する方法は?

私はこのキーを生成することができますが、使用方法はわかりませんJAVAです。

String PRIVATE_KEY_FILE_RSA = "src/pri.der"; 
File privKeyFile = new File(PRIVATE_KEY_FILE_RSA); 

// read private key DER file 
DataInputStream dis = new DataInputStream(new FileInputStream(privKeyFile)); 
byte[] privKeyBytes = new byte[(int) privKeyFile.length()]; 
dis.read(privKeyBytes); 
dis.close(); 
KeyFactory keyFactory = KeyFactory.getInstance("RSA"); 

// decode private key 
PKCS8EncodedKeySpec privSpec = new PKCS8EncodedKeySpec(privKeyBytes); 
RSAPrivateKey privKey =(RSAPrivatKey) keyFactory.generatePublic(pubSpec); 

と使用::私はどのようにそこに入るために、任意の情報や例のために必要

Algorithm algorithm = Algorithm.RSA256(pubKey, privKey); 
... 

私のコードで

私はプライベート(パスフレーズwitout)キーを(国民が同じである)を初期化パスフレーズ。

答えて

2

Thisソリューションは私のために優れているも参照してください。リンクが動作していない場合

UPDATEは、未コモンズ-sslのを探します。

not-yet-commons-ssl-0.3.11.jarに使用しました。例えば :

//path to private key file 
String PRIVATE_KEY_FILE_RSA = "C:\\Users\\Adey"; 
FileInputStream in = new FileInputStream(PRIVATE_KEY_FILE_RSA); 
// passphrase - the key to decode private key 
String passphrase = "somepass"; 
PKCS8Key pkcs8 = new PKCS8Key(in, passphrase.toCharArray()); 
byte[] decrypted = pkcs8.getDecryptedBytes(); 
PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(decrypted); 
RSAPrivateKey privKey = (RSAPrivateKey) KeyFactory.getInstance("RSA").generatePrivate(spec); 
+0

リンクが機能していない場合は、次回にソリューションを書き、リンクを設定してください。 –

+0

@Mohamd Al-Najjarを更新しました。 – Adey

0

秘密鍵をパスワードで保護するには、PBE(Password Based Encrytion)を使用する必要があります。私はあなたのために作られたいくつかの研究の後、次のサンプルコードは、あなたを助けることがあります。

//Generating keypairs 
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(); 

Java Cryptography Architecture (JCA) Reference Guide

+1

セキュリティの強化:1024ビットRSAが小さいサイズのビット(すなわち、NSAは、おそらくそれを解読することができる)、より良好な使用2048ビットです。 PBEWithSHA1AndDESedeよりもPBKDF2WithHmacSHA512を使用するほうがよく、反復回数は少なくとも10,000でなければなりません。 – TheGreatContini

+0

右! @TheGreatContini上記のコードは単なるテンプレートであり、誰もが彼のニーズに合わせるためにいくつかの変更を加える必要があります。あなたのノートは、暗号化の場合、あなたが使用するアルゴリズムだけでなく、どのセキュリティパラメータを使用するのかに関係なく、あなたのノートは非常に役立ちます。 –

+0

@Dimitris Baltas、ありがとうございました!たぶん私はあなたを理解できませんでしたが、私はopensslで鍵を生成し、ファイルのようにロードする必要があります。次に、それらをRSAPrivateKeyクラスで暗号化して保存し、アルゴリズムクラスで使用する必要があります。最後にトークンを作成しますString token = JWT.create()。sign(algorithm); – Adey

関連する問題