我正在编写一个用于传输文件的小应用程序,或多或少是一种了解更多程序加密基础的方法.我们的想法是生成RSA密钥对,交换公钥,并发送AES iv和密钥以进行进一步解密.我想用接收器RSA公钥加密AES密钥,如下所示:
// encode the SecretKeySpec
private byte[] EncryptSecretKey ()
{
Cipher cipher = null;
byte[] key = null;
try
{
cipher = Cipher.getInstance("RSA/ECB/NOPADDING");
// contact.getPublicKey returns a public key of type Key
cipher.init(Cipher.ENCRYPT_MODE, contact.getPublicKey() );
// skey is the SecretKey used to encrypt the AES data
key = cipher.doFinal(skey.getEncoded());
}
catch(Exception e )
{
System.out.println ( "exception encoding key: " + e.getMessage() );
e.printStackTrace();
}
return key;
}
Run Code Online (Sandbox Code Playgroud)
然后我将键值写入接收器,并像这样解密:
private SecretKey decryptAESKey(byte[] data )
{
SecretKey key = null; …Run Code Online (Sandbox Code Playgroud)