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

时间:2014-10-07 04:42:06

标签: c# encryption key des

在我的C#程序中,我正在加密/解密文件。我将此代码从Microsoft网站复制到玩具中并进行剖析。我问你们是否有人知道如何将预先确定的字符串作为加密/解密的密钥。

在我的主要方法中:

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);

1 个答案:

答案 0 :(得分:0)

这取决于你想做什么。在以下行中......

ASCIIEncoding.ASCII.GetString(desCrypto.Key);

您正在获取加密对象的自动生成密钥的ascii表示。但是,DES.Key属性包含公开set和公开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解码为字符串时,字节将从十六进制“解码”为more readable ascii characters(假设您已经提供了一些有意义的十六进制值来开始(当然)。

相关问题