我正在加密一些文本并将其转换为base64,并且在解密时我似乎有一些奇怪的字符,因为它会阻止输出的其余部分......
例如,我想加密10,2013-12-16 10:22:24,1387189344转换为MaEA8gd7Xyg8tNBrtVBlb75U / a0J9x5x7UryI81gy3R + ZvL01p05uzDtzdBWL5Pg(base64)然后当我解密时我尝试将输出打印为“[” +解密+“]”但第二个括号不出来.....
这是我的代码。提前谢谢。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DataSync;
using System.IO;
using System.Security.Cryptography;
namespace EncryptionTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// encrypt
private void button1_Click(object sender, EventArgs e)
{
txtOutput.Text = Encrypt(txtData.Text, txtCIV.Text, txtSecret.Text);
}
// decrypt
private void button2_Click(object sender, EventArgs e)
{
txtOutput.Text = "[" + Decrypt(txtData.Text, txtCIV.Text, txtSecret.Text) + "\r\n" + "]";
}
public string GenerateIV()
{
RijndaelManaged RM = new RijndaelManaged();
RM.KeySize = 128;
RM.BlockSize = 128;
RM.Mode = CipherMode.CBC;
RM.Padding = PaddingMode.Zeros;
RM.GenerateIV();
return Convert.ToBase64String(RM.IV);
}
public string Decrypt(string Encrypted, string IV, string SecretKey)
{
byte[] DecryptThis = Convert.FromBase64String(Encrypted);
byte[] DecryptIV = Convert.FromBase64String(IV);
byte[] Key = Encoding.UTF8.GetBytes(SecretKey);
SHA256 SHA256Obj = SHA256.Create();
byte[] PrivateKeyHash = SHA256Obj.ComputeHash(Key, 0, Key.Length);
RijndaelManaged RM = new RijndaelManaged();
RM.Mode = CipherMode.CBC;
RM.Padding = PaddingMode.Zeros;
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, RM.CreateDecryptor(PrivateKeyHash, DecryptIV), CryptoStreamMode.Write);
cs.Write(DecryptThis, 0, DecryptThis.Length);
cs.FlushFinalBlock();
cs.Close();
// Strip null (padding) characters
int Position = -1;
foreach (byte Char in ms.ToArray())
{
++Position;
if (Char == 0)
{
break;
}
}
string Temp = Encoding.UTF8.GetString(ms.ToArray());
return Temp.Substring(0, Position+1).Trim();
}
public string Encrypt(string ToBeEncrypted, string IV, string SecretKey)
{
byte[] EncryptThis = Encoding.UTF8.GetBytes(ToBeEncrypted);
byte[] EncryptIV = Convert.FromBase64String(IV);
byte[] Key = Encoding.UTF8.GetBytes(SecretKey);
SHA256 SHA256Obj = SHA256.Create();
byte[] PrivateKeyHash = SHA256Obj.ComputeHash(Key, 0, Key.Length);
RijndaelManaged RM = new RijndaelManaged();
RM.Mode = CipherMode.CBC;
RM.Padding = PaddingMode.Zeros;
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, RM.CreateEncryptor(PrivateKeyHash, EncryptIV), CryptoStreamMode.Write);
cs.Write(EncryptThis, 0, EncryptThis.Length);
cs.FlushFinalBlock();
cs.Close();
return Convert.ToBase64String(ms.ToArray());
}
}
}
答案 0 :(得分:0)
您的代码中有换行符:
txtOutput.Text = "[" + Decrypt(txtData.Text, txtCIV.Text, txtSecret.Text) + "\r\n" + "]";
^^^
您必须在括号
之后放置换行符txtOutput.Text = "[" + Decrypt(txtData.Text, txtCIV.Text, txtSecret.Text) + "]" + "\r\n";
此外,我不确定你是否需要换行。您使用的是多行文字框吗?
答案 1 :(得分:0)
您没有删除Decrypt()
方法中的所有“\ 0”。
在第82行,替换
return Temp.Substring(0, Position + 1).Trim();
通过
return Temp.Substring(0, Position).Trim();