【发布时间】:2012-02-22 08:11:07
【问题描述】:
我正在 C#.NET 中进行简单的自定义加密,加密成功通过,但解密出错。该算法非常直观,但我不知道为什么它解密错误。 这是我的代码:
private void button1_Click(object sender, RoutedEventArgs e)
{
//Encrypting
byte[] initial_text_bytes = Encoding.UTF8.GetBytes(initial_text_tb.Text);
byte[] secret_word_bytes = Encoding.UTF8.GetBytes(secret_word_tb.Text);
byte[] encrypted_bytes = new byte[initial_text_bytes.Length];
int secret_word_index = 0;
for (int i=0; i < initial_text_bytes.Length; i++)
{
if (secret_word_index == secret_word_bytes.Length)
{
secret_word_index = 0;
}
encrypted_bytes[i] = (byte)(initial_text_bytes[i] + initial_text_bytes[secret_word_index]);
secret_word_index++;
}
// String s = Encoding.UTF8.GetString(encrypted_bytes);
//new String(Encoding.UTF8.GetChars(encrypted_bytes));
text_criptat_tb.Text = Convert.ToBase64String(encrypted_bytes);
}
private void button2_Click(object sender, RoutedEventArgs e)
{
//Decrypting
byte[] initial_text_bytes = Encoding.UTF8.GetBytes(text_criptat_tb.Text);
byte[] secret_word_bytes = Encoding.UTF8.GetBytes(secret_word_tb.Text);
byte[] encrypted_bytes = new byte[initial_text_bytes.Length];
int secret_word_index = 0;
for (int i = 0; i < initial_text_bytes.Length; i++)
{
if (secret_word_index == secret_word_bytes.Length)
{
secret_word_index = 0;
}
encrypted_bytes[i] = (byte)(initial_text_bytes[i] - initial_text_bytes[secret_word_index]);
secret_word_index++;
}
// String s = new String(Encoding.UTF8.GetChars(encrypted_bytes));
initial_text_tb.Text = Convert.ToBase64String(encrypted_bytes);
}
这是我加密时得到的: 这是我解密的时候: 谢谢
【问题讨论】:
-
“我正在做一个简单的自定义加密” - 不要编写自己的加密。会有缺陷的……
-
您似乎没有在任何地方从 base64 转换回来...
-
米奇,这是给 uni 的实验室
标签: c# encryption