C# 解密加密文本
我在我的Windows窗体应用程序中有一个文本框和一个“解密”按钮,我在其中放入一个加密字符串并尝试解密,但问题是这个。首先,我从这个网站上的一个家伙那里得到了名为C# 解密加密文本,c#,encryption,C#,Encryption,我在我的Windows窗体应用程序中有一个文本框和一个“解密”按钮,我在其中放入一个加密字符串并尝试解密,但问题是这个。首先,我从这个网站上的一个家伙那里得到了名为DataEncryptor的课程代码: public class DataEncryptor { TripleDESCryptoServiceProvider symm; #region Factory public DataEncryptor() { this.symm = new
DataEncryptor
的课程代码:
public class DataEncryptor
{
TripleDESCryptoServiceProvider symm;
#region Factory
public DataEncryptor()
{
this.symm = new TripleDESCryptoServiceProvider();
this.symm.Padding = PaddingMode.PKCS7;
}
public DataEncryptor(TripleDESCryptoServiceProvider keys)
{
this.symm = keys;
}
public DataEncryptor(byte[] key, byte[] iv)
{
this.symm = new TripleDESCryptoServiceProvider();
this.symm.Padding = PaddingMode.PKCS7;
this.symm.Key = key;
this.symm.IV = iv;
}
#endregion
#region Properties
public TripleDESCryptoServiceProvider Algorithm
{
get { return symm; }
set { symm = value; }
}
public byte[] Key
{
get { return symm.Key; }
set { symm.Key = value; }
}
public byte[] IV
{
get { return symm.IV; }
set { symm.IV = value; }
}
#endregion
#region Crypto
public byte[] Encrypt(byte[] data) { return Encrypt(data, data.Length); }
public byte[] Encrypt(byte[] data, int length)
{
try
{
// Create a MemoryStream.
var ms = new MemoryStream();
// Create a CryptoStream using the MemoryStream
// and the passed key and initialization vector (IV).
var cs = new CryptoStream(ms,
symm.CreateEncryptor(symm.Key, symm.IV),
CryptoStreamMode.Write);
// Write the byte array to the crypto stream and flush it.
cs.Write(data, 0, length);
cs.FlushFinalBlock();
// Get an array of bytes from the
// MemoryStream that holds the
// encrypted data.
byte[] ret = ms.ToArray();
// Close the streams.
cs.Close();
ms.Close();
// Return the encrypted buffer.
return ret;
}
catch (CryptographicException ex)
{
Console.WriteLine("A cryptographic error occured: {0}", ex.Message);
}
return null;
}
public string EncryptString(string text)
{
return Convert.ToBase64String(Encrypt(Encoding.UTF8.GetBytes(text)));
}
public byte[] Decrypt(byte[] data) { return Decrypt(data, data.Length); }
public byte[] Decrypt(byte[] data, int length)
{
try
{
// Create a new MemoryStream using the passed
// array of encrypted data.
MemoryStream ms = new MemoryStream(data);
// Create a CryptoStream using the MemoryStream
// and the passed key and initialization vector (IV).
CryptoStream cs = new CryptoStream(ms,
symm.CreateDecryptor(symm.Key, symm.IV),
CryptoStreamMode.Read);
// Create buffer to hold the decrypted data.
byte[] result = new byte[length];
// Read the decrypted data out of the crypto stream
// and place it into the temporary buffer.
cs.Read(result, 0, result.Length);
return result;
}
catch (CryptographicException ex)
{
Console.WriteLine("A cryptographic error occured: {0}", ex.Message);
}
return null;
}
public string DecryptString(string data)
{
return Encoding.UTF8.GetString(Decrypt(Convert.FromBase64String(data))).TrimEnd('\0');
}
#endregion
}
他给出了它的用法:
string message="A very secret message here.";
DataEncryptor keys=new DataEncryptor();
string encr=keys.EncryptString(message);
// later
string actual=keys.DecryptString(encr);
我复制了他的代码并致力于加密和解密:
//my code
private void proceedED(string data)
{
DataEncryptor key = new DataEncryptor();
string encr = key.EncryptString(data);
string actual = key.DecryptString(encr);
encryptedLabel.Text = encr;
decryptedLabel.Text = actual;
}
然后我创建了这样一个方法:
private void proceedDecrypt(string data)
{
DataEncryptor key = new DataEncryptor();
string decr = key.DecryptString(data);
decryptedData.Text = decr;
}
class Program
{
static void Main(string[] args)
{
string key, iv;
var plain="A very secret message.";
var cipher=EncryptString(plain, out key, out iv);
// Later ...
var message=DecryptString(cipher, key, iv);
}
public static string EncryptString(string plain, out string key, out string iv)
{
var crypto=new DataEncryptor();
iv=Convert.ToBase64String(crypto.IV);
key=Convert.ToBase64String(crypto.Key);
return crypto.EncryptString(plain);
}
public static string DecryptString(string cipher, string key, string iv)
{
var crypto=new DataEncryptor(
Convert.FromBase64String(key),
Convert.FromBase64String(iv));
return crypto.DecryptString(cipher);
}
}
问题是当我提交时它崩溃了,我不知道为什么。
我认为它应该是一个真正的加密字符串,因为它只是一个普通字符串。
如何修复此问题?您正在这两个函数中创建新对象
DataEncryptor key = new DataEncryptor();
这就是你犯错误的原因
只需申报
DataEncryptor key = new DataEncryptor();
在proceedDecrypt()和proceedDecrypt()之外,我的意思是公开它
或者您可以将pass key作为参数传递给proceedDecrypt(),并在该函数中使用它
喜欢
希望对你有帮助 您可以将加密和说明与System.Security.Cryptography一起使用
1) Set encryption decription key
2) Encrypt data with encryption key
3) Decrypt data with same encryption key
请参考下面的加密和说明示例链接。
数据加密器的每个实例都会生成新密钥。您需要使用加密字符串的密钥进行解密。如果这是在同一过程中完成的,则保留对
DataEncryptor key
的引用。否则,您需要使用数据加密程序(字节[]键,字节[]iv)
构造函数进行初始化
请尝试以下代码:
private void proceedDecrypt(string data)
{
DataEncryptor key = new DataEncryptor();
string decr = key.DecryptString(data);
decryptedData.Text = decr;
}
class Program
{
static void Main(string[] args)
{
string key, iv;
var plain="A very secret message.";
var cipher=EncryptString(plain, out key, out iv);
// Later ...
var message=DecryptString(cipher, key, iv);
}
public static string EncryptString(string plain, out string key, out string iv)
{
var crypto=new DataEncryptor();
iv=Convert.ToBase64String(crypto.IV);
key=Convert.ToBase64String(crypto.Key);
return crypto.EncryptString(plain);
}
public static string DecryptString(string cipher, string key, string iv)
{
var crypto=new DataEncryptor(
Convert.FromBase64String(key),
Convert.FromBase64String(iv));
return crypto.DecryptString(cipher);
}
}
我终于解决了 我从中复制了这段代码
并删除了
数据加密机
类它给出了什么例外?在所有关于“错误”、“危机”等的帖子中,是否包含异常的消息和堆栈跟踪?可能您尚未初始化密钥,或者您的数据有问题。如果没有异常消息,我们就无法知道它。数组不能为空参数名称:bytes
行错误在DataEncryptor Classis底部的DecryptString(string data)方法中。它看起来像是您创建的方法proceedDecrypt
,但将空的数据传递给它。看看你调用这个方法的地方。我认为它需要是一个真正加密的字符串,因为它只是一个普通的字符串。嗯,这个代码对我来说很熟悉。请参阅检查编辑,如果有错误,请告诉我您遇到了什么错误。。。。!!!还有如何调用proceedDecrypt()函数?