C#将用户输入字符串添加到密钥DES
在我的C#程序中,我正在加密/解密文件。我从微软网站上复制了这段代码来玩弄它并对其进行剖析。我在问你们中是否有人知道如何使用预先确定的字符串作为加密/解密的密钥 在我的主要方法中:C#将用户输入字符串添加到密钥DES,c#,encryption,key,des,C#,Encryption,Key,Des,在我的C#程序中,我正在加密/解密文件。我从微软网站上复制了这段代码来玩弄它并对其进行剖析。我在问你们中是否有人知道如何使用预先确定的字符串作为加密/解密的密钥 在我的主要方法中: string sSecretKey; sSecretKey = GenerateKey(); GenerateKey: static string GenerateKey() { DESCryptoServiceProvider desCrypto = (DESCrypt
string sSecretKey;
sSecretKey = GenerateKey();
GenerateKey:
static string GenerateKey()
{
DESCryptoServiceProvider desCrypto =
(DESCryptoServiceProvider) DESCryptoServiceProvider.Create();
return ASCIIEncoding.ASCII.GetString(desCrypto.Key);
}
这是在我的加密和解密函数中:我不知道它们做什么
DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
这取决于你想做什么。在下面一行中
ASCIIEncoding.ASCII.GetString(desCrypto.Key);
您正在获取加密对象自动生成的密钥的ascii表示形式。但是属性DES.Key
既有一个publicset
又有一个publicget
,因此您可以执行以下操作:
// Set key to a pre-determined set of bytes (of length 8):
desCrypto.Key = new byte[] {0x4b, 0x4a, 0x41, 0x52, 0x54, 0x41, 0x4e, 0x21};
当然,这个值是这样的,当下一行将desCrypto.Key
解码为字符串时,字节将从十六进制“解码”为(当然,假设您首先提供了一些有意义的十六进制值)