Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#将用户输入字符串添加到密钥DES_C#_Encryption_Key_Des - Fatal编程技术网

C#将用户输入字符串添加到密钥DES

C#将用户输入字符串添加到密钥DES,c#,encryption,key,des,C#,Encryption,Key,Des,在我的C#程序中,我正在加密/解密文件。我从微软网站上复制了这段代码来玩弄它并对其进行剖析。我在问你们中是否有人知道如何使用预先确定的字符串作为加密/解密的密钥 在我的主要方法中: string sSecretKey; sSecretKey = GenerateKey(); GenerateKey: static string GenerateKey() { DESCryptoServiceProvider desCrypto = (DESCrypt

在我的C#程序中,我正在加密/解密文件。我从微软网站上复制了这段代码来玩弄它并对其进行剖析。我在问你们中是否有人知道如何使用预先确定的字符串作为加密/解密的密钥

在我的主要方法中:

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
既有一个public
set
又有一个public
get
,因此您可以执行以下操作:

// 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
解码为字符串时,字节将从十六进制“解码”为(当然,假设您首先提供了一些有意义的十六进制值)