2016-04-17 3 views
0

私はRSAでファイルを暗号化し解読しようとしています。暗号化は正常に動作しています。しかし、私が解読しているときに私はエラーになります。RSA解読 - キーが存在しません

エラーは存在しません。ここで

は誤りです:http://i.imgur.com/ebF09cU.png

public byte[] RSA_Encrypt(byte[] bytesToBeEncrypted, byte[] passwordBytes, RSAParameters RSAKeyInfo) 
{ 
    //initialze the byte arrays to the public key information. 
    byte[] PublicKey = {214,46,220,83,160,73,40,39,201,155,19,202,3,11,191,178,56, 
          74,90,36,248,103,18,144,170,163,145,87,54,61,34,220,222, 
          207,137,149,173,14,92,120,206,222,158,28,40,24,30,16,175, 
          108,128,35,230,118,40,121,113,125,216,130,11,24,90,48,194, 
          240,105,44,76,34,57,249,228,125,80,38,9,136,29,117,207,139, 
          168,181,85,137,126,10,126,242,120,247,121,8,100,12,201,171, 
          38,226,193,180,190,117,177,87,143,242,213,11,44,180,113,93, 
          106,99,179,68,175,211,164,116,64,148,226,254,172,147}; 

    //Values to store encrypted symmetric keys. 
    byte[] EncryptedSymmetricKey; 
    byte[] EncryptedSymmetricIV; 

    byte[] encryptedBytes = null; 

    // Set your salt here, change it to meet your flavor: 
    // The salt bytes must be at least 8 bytes. 
    byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }; 

    using (MemoryStream ms = new MemoryStream()) 
    { 
     using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(_stBitov)) 
     { 
      //Set RSAKeyInfo to the public key values. 
      RSAKeyInfo.Modulus = PublicKey; 
      //Import key parameters into RSA. 
      RSA.ImportParameters(RSAKeyInfo); 

      //Create a new instance of the RijndaelManaged class. 
      RijndaelManaged RM = new RijndaelManaged(); 

      var key = new Rfc2898DeriveBytes(PublicKey, saltBytes, 1000); 

      //Encrypt the symmetric key and IV. 
      EncryptedSymmetricKey = RSA.Encrypt(RM.Key, false); 
      EncryptedSymmetricIV = RSA.Encrypt(RM.IV, false); 

      encryptedBytes = RSA.Encrypt(bytesToBeEncrypted, false); 
     } 
    } 

    return encryptedBytes; 
} 

RSAParameters _RSAKeyInfo; 

public void EncryptFile() 
{ 
    RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(); 

    //Get an instance of RSAParameters from ExportParameters function. 
    RSAParameters RSAKeyInfo = RSA.ExportParameters(false); 
    _RSAKeyInfo = RSAKeyInfo; 

    string path = ofd.FileName; 

    if (File.Exists(path)) 
    { 
     string dirPath = Path.GetDirectoryName(path); 

     byte[] bytesToBeEncrypted = File.ReadAllBytes(path); 

     byte[] passwordBytes = File.ReadAllBytes(dirPath + "/KEY_" + ofd.SafeFileName); 

     byte[] bytesEncrypted = RSA_Encrypt(bytesToBeEncrypted, passwordBytes, RSAKeyInfo); 

     string fileEncrypted = dirPath + "/ENCRYPTED_" + ofd.SafeFileName; 

     File.WriteAllBytes(fileEncrypted, bytesEncrypted); 
    } 
} 

private void button5_Click(object sender, EventArgs e) 
{ 
    string path = ofd2.FileName; 

    if (File.Exists(path)) 
    { 
     DecryptFile(); 
     richTextBox4.Text = "Dekripcija uspesna"; 
    } 
    else 
    { 
     richTextBox6.Text = "Datoteka ni dodana"; 
    } 
} private void richTextBox4_TextChanged(object sender, EventArgs e) { } 

public byte[] RSA_Decrypt(byte[] bytesToBeDecrypted, byte[] passwordBytes, RSAParameters RSAKeyInfo) 
{ 
    byte[] PublicKey = {214,46,220,83,160,73,40,39,201,155,19,202,3,11,191,178,56, 
          74,90,36,248,103,18,144,170,163,145,87,54,61,34,220,222, 
          207,137,149,173,14,92,120,206,222,158,28,40,24,30,16,175, 
          108,128,35,230,118,40,121,113,125,216,130,11,24,90,48,194, 
          240,105,44,76,34,57,249,228,125,80,38,9,136,29,117,207,139, 
          168,181,85,137,126,10,126,242,120,247,121,8,100,12,201,171, 
          38,226,193,180,190,117,177,87,143,242,213,11,44,180,113,93, 
          106,99,179,68,175,211,164,116,64,148,226,254,172,147}; 

    //Values to store encrypted symmetric keys. 
    byte[] EncryptedSymmetricKey; 
    byte[] EncryptedSymmetricIV; 

    byte[] decryptedBytes = null; 

    // Set your salt here, change it to meet your flavor: 
    // The salt bytes must be at least 8 bytes. 
    byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }; 

    using (MemoryStream ms = new MemoryStream()) 
    { 
     using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(_stBitov)) 
     { 
      //Set RSAKeyInfo to the public key values. 
      RSAKeyInfo.Modulus = PublicKey; 
      //Import key parameters into RSA. 
      RSA.ImportParameters(RSAKeyInfo); 

      //Create a new instance of the RijndaelManaged class. 
      RijndaelManaged RM = new RijndaelManaged(); 

      //Encrypt the symmetric key and IV. 
      EncryptedSymmetricKey = RSA.Encrypt(RM.Key, false); 
      EncryptedSymmetricIV = RSA.Encrypt(RM.IV, false); 

      decryptedBytes = RSA.Decrypt(bytesToBeDecrypted, false); 
     } 
    } 

    return decryptedBytes; 
} 

public void DecryptFile() 
{ 
    string path = ofd2.FileName; 

    if (File.Exists(path)) 
    { 
     string dirPath = Path.GetDirectoryName(path); 

     byte[] bytesToBeDecrypted = File.ReadAllBytes(path); 

     byte[] passwordBytes = File.ReadAllBytes(dirPath + "/KEY_" + ofd.SafeFileName); 

     byte[] bytesDecrypted = RSA_Decrypt(bytesToBeDecrypted, passwordBytes, _RSAKeyInfo); 

     string file = dirPath + "/DECRYPTED_" + ofd.SafeFileName; 

     File.WriteAllBytes(file, bytesDecrypted); 
    } 
} 

誰かが解読が仕事に行くされていることを何をすべきかを教えてもらえます。

+1

非対称(RSA)暗号化が必要な場合はどうしますか?聞かれる質問は、公開鍵と秘密鍵が別々に必要なことです。そうでない場合は、AESなどの対称アルゴリズムを使用します。達成しようとしていることに関する情報を追加すると、質問に答えるのに役立ちます。 – zaph

+1

この質問をお寄せいただきありがとうございます。私のECDSAロジックには「キーは存在しません」とあります。私はなぜ人々がdownvoteを知りません。 –

答えて

1

RSA + AESでhybrid encryptionを実行しようとしていますが、実際にAESを使用して平文を暗号化するのを忘れてしまって、RSAで対称鍵を暗号化しなかった。また、対称鍵をランダムに生成する必要があり、定数であると想定される公開鍵から派生されるべきではありません。公開

あなたがここで提示したエラーはあなたの問題の中では最少ですが、ElectroBytがすでに述べたように、秘密鍵(RSACryptoServiceProvider#ExportParameters(true))を使用してRSAで何かを復号化する必要があります。あなたのケースでは、RSAを使って解読し、対称鍵を取得して対称暗号文を解読して実際のメッセージを戻す必要があります。

+0

私は 'RSACryptoServiceProvider#ExportParameters(true)'(秘密鍵)のみ必要でした。ありがとうございました! – klemsi123

1

RSAは一種の公開鍵暗号方式です。つまり、メッセージを暗号化するための公開鍵とメッセージを復号化するための秘密鍵が必要です。暗号化と復号化の両方に公開鍵を使用しているようです。秘密鍵はどこですか?

関連する問題