2009-06-04 36 views
1

AESとRSAを使用してデジタルエンベロープを実装する必要がありますが、RSAアルゴリズムの.NET実装に問題があります。モジュラスと公開指数を使用したC#RSA暗号化

ランダムな対称鍵でデータ(AES)を暗号化できましたが、今ではRSAで鍵を暗号化する必要があります。

キーはバイトの配列(byte[])で、私にはモジュラスと公開指数だけが通知されています。両方のバイト配列(byte[])です。

これらの2つのパラメータのみを使用して、AESで生成された鍵をRSAで暗号化するにはどうすればよいですか?

次のコードは、ファイルからメッセージを取得し、AESで暗号化します。 その後、公開鍵は公開鍵ファイルから読み込まれ、モジュラスと指数は適切なバイト配列に格納されます。私はsymmetricKeyをRSAで暗号化し続けますか?

String msgString = Systematic.GetFileContents(messagePath); 
Byte[] initVector = new byte[] { 50, 60, 70, 80, 90, 40, 50, 60, 70, 80, 90, 40, 60, 80, 70, 90 }; 
Byte[] symetricKey = AesCrypt.GenerateRandomKey(); 
Byte[] encryptedMessage = AesCrypt.Encrypt(msgString, symetricKey, initVector, mode); 
Byte[] modulus = null; 
Byte[] publicExp = null; 
DataFormatHelper.ReadPublicKey(publicKeyPath, "RSA", ref modulus, ref publicExp); 

P.S.答えに返信するとrsa.ImportParameters: 私はrsa.ImportParameters(keyInfo)で試しましたが、CryptographicException"Bad Data")を投げます。配列のサイズはどうですか? 現在、モジュラスは128バイトで、指数は64バイトです。 RSACryptoServiceProvider

static public byte[] RSAEncrypt(byte[] data, 
    RSAParameters keyInfo, 
    bool doOAEPPadding) 
{ 
    byte[] encryptedData; 
    using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider()) 
    { 
     //Import the RSA Key information. This only needs 
     //toinclude the public key information. 
     rsa.ImportParameters(keyInfo); 

     //Encrypt the passed byte array and specify OAEP padding. 
     //OAEP padding is only available on Microsoft Windows XP or later. 
     encryptedData = rsa.Encrypt(data, doOAEPPadding); 
    } 
    return encryptedData;  
} 

だから何が必要RSAParametersですが、あなたが設定する必要があるすべては、モジュラスおよび指数は暗号化するために使用している

+0

rsa.ExportParameters(false)を使用する場合。 3バイトの指数と128バイトのモジュラスが得られます – ShuggyCoUk

答えて

関連する問題