CSharp - 使用椭圆曲线加密消息时出错

  显示原文与译文双语对照的内容
0 0

当我通过单击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);
 }
}
}

时间: 原作者:

0 0

Encrypted.Text = Encoding.Unicode.GetString(cipherText); 很可能是。

随机字节不是字符编码。 这可能是一个未知的编码被转换成一个替代或者根本没有任何字符。 这将发生在现在,然后( 加密文本与随机文本是不可区分的) 。

对密文使用 Base 64 编码,关于如何在stackoverflow上做这个有很多例子。 幸运的是,Base 64 编码/解码被构建到. NET API ( 你收到了,甲骨文) 中。

原作者:
...