误差对加密使用椭圆曲线

问题描述:

当我通过点击按钮1几次(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

你的问题应该包含* text * - 包括你想要做什么,发生了什么异常以及什么是异常。您还应该阅读格式指南,以便您的代码格式正确。此外,这看起来像一个短的,但完整的*控制台*应用程序的好候选人 - 这里不需要GUI。使用控制台应用程序,我们可以复制并粘贴代码,然后直接运行它。 – 2012-07-22 11:10:37

+0

在“ICryptoTransform encryptor = rijndael.CreateEncryptor(SecretA,IVByteArray);” – 2012-07-22 11:12:21

+0

您的意思是它不能在GUI下工作 – 2012-07-22 11:14:39

Encrypted.Text = Encoding.Unicode.GetString(cipherText);可能是罪魁祸首。

随机字节不是字符编码。这可能是因为一个未知的编码在这里完全被翻译为替代或没有人物。这会随时发生(因为加密文本与随机无法区分)。

改为使用base 64编码的密文,在stackoverflow上有如何做到这一点的例子。幸运的是,基础64编码/解码被构建到.net API中(您是否接收到这个消息,Oracle?)。

+0

非常感谢猫头鹰。告诉我在我的例子中如何使用base 64编码/解码? – 2012-07-23 10:09:41

+0

确定的事情:“有堆栈的例子说明如何在stackoverflow上做到这一点。” “oodles”的意思是“很多”。 – 2012-07-23 17:58:00