-1

button1 を数回 (10 回以上) クリックして楕円曲線を使用してメッセージを暗号化すると、次のエラーが表示されます。

インデックスが配列の範囲外だった。

コードを以下に示します。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DiffieHellmanMerkle;
using System.Security.Cryptography;
using System.IO;

namespace TestEllipticCurveDiffieHellman
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        byte[] SecretA = null;
        byte[] SecretB = null;
        try
        {
            ECDiffieHellmanMerkle A = new ECDiffieHellmanMerkle(ECDHAlgorithm.ECDH_384);
            ECDiffieHellmanMerkle B = new ECDiffieHellmanMerkle(ECDHAlgorithm.ECDH_384);
            A.KeyDerivationFunction = ECDHKeyDerivationFunction.HASH;
            B.KeyDerivationFunction = ECDHKeyDerivationFunction.HASH;
            A.HashAlgorithm = DerivedKeyHashAlgorithm.SHA256_ALGORITHM;
            B.HashAlgorithm = DerivedKeyHashAlgorithm.SHA256_ALGORITHM;
            SecretA = A.RetrieveSecretKey(B.PublicKey);
            SecretB = B.RetrieveSecretKey(A.PublicKey);
        }
        catch(Exception ex)
        {
            MessageBox.Show(ex.Message,"Win32 Error Message");
        }

        //Alice encrypts the message with her secret key
        string SecretMessage = plain.Text;// "The owl of Minerva only flies at dusk.";
        byte[] SecretMessageByteArray = Encoding.Unicode.GetBytes(SecretMessage);
        string IVString = "initialV";
        byte[] IVByteArray = Encoding.Unicode.GetBytes(IVString);
        RijndaelManaged rijndael = new RijndaelManaged();
        ICryptoTransform encryptor = rijndael.CreateEncryptor(SecretA, IVByteArray);
        MemoryStream memoryStream = new MemoryStream();
        CryptoStream cryptoStream = new CryptoStream(memoryStream, Encryptor,CryptoStreamMode.Write);
        cryptoStream.Write(SecretMessageByteArray, 0, SecretMessageByteArray.Length);
        cryptoStream.FlushFinalBlock();
        byte[] cipherText = memoryStream.ToArray();
        memoryStream.Close();
        cryptoStream.Close();

        Encrypted.Text = Encoding.Unicode.GetString(cipherText);

        /* string strcipherTextUni = Encoding.Unicode.GetString(cipherText);
        MessageBox.Show("Encrypted Unicode = " + strcipherTextUni.ToString());*/

        //Bob decrypts the message with his secret key
        ICryptoTransform decryptor = rijndael.CreateDecryptor(SecretB, IVByteArray);
        memoryStream = new MemoryStream(cipherText);
        cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read);
        byte[] clearText = new byte[cipherText.Length];
        int clearTextByteSize = cryptoStream.Read(clearText, 0, clearText.Length);
        memoryStream.Close();
        cryptoStream.Close();
        this.Decrypted.Text = Encoding.Unicode.GetString(clearText, 0, clearTextByteSize);
    }
}
}
4

1 に答える 1

0

Encrypted.Text = Encoding.Unicode.GetString(cipherText);おそらく犯人です。

ランダム バイトは文字エンコーディングではありません。不明なエンコーディングが置換に変換されているか、文字がまったくない可能性があります。これはときどき発生します (暗号化されたテキストはランダムと区別できないため)。

代わりに暗号文の base 64 エンコーディングを使用してください。stackoverflow でこれを行う方法の例がたくさんあります。幸いなことに、base 64 エンコーディング/デコーディングは .net API に組み込まれています (それを受け取っていますか、オラクル?)。

于 2012-07-22T22:55:37.903 に答える