C# 使用椭圆曲线加密消息时出错
当我通过点击按钮1多次(超过10次)使用椭圆曲线加密消息时,我得到以下错误 索引超出了数组的边界 代码如下:C# 使用椭圆曲线加密消息时出错,c#,encryption,C#,Encryption,当我通过点击按钮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; usi
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);
}
}
}
Encrypted.Text=Encoding.Unicode.GetString(密文)代码>很可能是罪魁祸首
随机字节不是字符编码。可能是一个未知的编码被转换为替换,或者根本没有字符。这种情况会时不时发生(因为加密文本与随机文本无法区分)
改为使用密文的Base64编码,有很多例子说明如何在stackoverflow上实现这一点。幸运的是,base 64编码/解码已内置到.net API中(您收到了吗,Oracle?)。您的问题应该包含文本,包括您尝试执行的操作、异常发生的位置以及异常是什么。您还应该阅读格式化指南,以便正确格式化代码。此外,对于一个简短但完整的控制台应用程序来说,这似乎是一个很好的选择——这里不需要GUI。使用控制台应用程序,我们可以复制和粘贴代码,然后直接运行。例外情况发生在“ICryptoTransform encryptor=rijndael.CreateEncryptor(SecretA,IVByteArray);”您的意思是它在GUI下无法工作@jonsket的意思是,如果您发布一个完整的(可编译且可运行的)程序,我们可以更轻松地帮助您。这样,我们就可以在我们的计算机上运行它,查看您遇到的错误,并帮助找出问题所在。我如何导入完整的文件非常感谢您。请告诉我如何在我的示例中使用base 64编码/解码?当然:“有很多关于如何在stackoverflow上执行此操作的示例。”“oodles”表示“许多”。