如何在C#中实现三重DES(完整示例) [英] How to implement Triple DES in C# (complete example)

查看:171
本文介绍了如何在C#中实现三重DES(完整示例)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在C#中使用三重DES在(utf8)字符串中加密/解密(utf8)的任何长度的密钥。



我正在寻找这些三个函数

  public static string Encrypt(string data,string key)
{
...
}

public static string Decrypt(string data,string key)
{
...
}

public static string GenerateKeyFromString(string str)
{
...
}

注意:在任何人链接之前, http:// www.geekzilla.co.uk/view7B360BD8-A77C-4F1F-BCA0-ACD0F6795F61.htm 实现不起作用,我已经彻底测试了。



我已经做了大量的搜索,发现问题的一些问题,如 EncryptOneBlock 等等,但我正在寻找一个完整的假人实现,我认为这对其他人也是有用的。



提前感谢

解决方案

= http://www.codeproject.com/Articles/14150/Encrypt-and-Decrypt-Data-with-C> http://www.codeproject.com/Articles/14150/Encrypt-and-Decrypt-Data -with-C



加密:

  public static string Encrypt(string toEncrypt,bool useHashing)
{
byte [] keyArray;
byte [] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);

System.Configuration.AppSettingsReader settingsReader =
new AppSettingsReader();
//从配置文件获取密钥

string key =(string)settingsReader.GetValue(SecurityKey,
typeof(String));
//System.Windows.Forms.MessageBox.Show(key);
//如果哈希使用get哈希码对您的密钥
if(useHashing)
{
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
//始终释放加密服务提供的资源并刷新数据
//。最佳实践

hashmd5.Clear();
}
else
keyArray = UTF8Encoding.UTF8.GetBytes(key);

TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
//设置tripleDES算法的秘密密钥
tdes.Key = keyArray;
//操作模式。还有其他4种模式。
//我们选择ECB(电子代码簿)
tdes.Mode = CipherMode.ECB;
//填充模式(如果添加任何额外的字节)

tdes.Padding = PaddingMode.PKCS7;

ICryptoTransform cTransform = tdes.CreateEncryptor();
//将字节数组的指定区域转换为resultArray
byte [] resultArray =
cTransform.TransformFinalBlock(toEncryptArray,0,
toEncryptArray.Length);
//发布TripleDes Encryptor保存的资源
tdes.Clear();
//将加密的数据返回到不可读的字符串格式
return Convert.ToBase64String(resultArray,0,resultArray.Length);
}

解密:

  public static string Decrypt(string cipherString,bool useHashing)
{
byte [] keyArray;
//获取字符串的字节码

byte [] toEncryptArray = Convert.FromBase64String(cipherString);

System.Configuration.AppSettingsReader settingsReader =
new AppSettingsReader();
//从配置文件获取密钥以打开锁定!
string key =(string)settingsReader.GetValue(SecurityKey,
typeof(String));

if(useHashing)
{
//如果使用散列,就可以获得关于密钥的哈希码
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
//释放由MD5CryptoServiceProvider持有的任何资源

hashmd5.Clear();
}
else
{
//如果哈希没有被实现获得密钥的字节码
keyArray = UTF8Encoding.UTF8.GetBytes(key);
}

TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
//设置tripleDES算法的秘密密钥
tdes.Key = keyArray;
//操作模式。还有其他4种模式。
//我们选择ECB(电子代码簿)

tdes.Mode = CipherMode.ECB;
//填充模式(如果添加任何额外的字节)
tdes.Padding = PaddingMode.PKCS7;

ICryptoTransform cTransform = tdes.CreateDecryptor();
byte [] resultArray = cTransform.TransformFinalBlock(
toEncryptArray,0,toEncryptArray.Length);
//发布TripleDes Encryptor保存的资源
tdes.Clear();
//返回清除解密的TEXT
返回UTF8Encoding.UTF8.GetString(resultArray);
}


I want to use triple DES in C# for encryption/decryption of (utf8) strings with a (utf8) key of any length.

I am looking for these three functions

public static string Encrypt(string data, string key)
{
    ...
}

public static string Decrypt(string data, string key)
{
    ...
}

public static string GenerateKeyFromString(string str)
{
    ...
}

Note: Before anyone links to it, the http://www.geekzilla.co.uk/view7B360BD8-A77C-4F1F-BCA0-ACD0F6795F61.htm implementation does not work, I have thoroughly tested it.

I have done lots of searching and found bits of the problem like EncryptOneBlock and others but I am looking for a complete 'for dummies' implementation which I think would be useful for others too.

Thanks in advance!

解决方案

Complete source here: http://www.codeproject.com/Articles/14150/Encrypt-and-Decrypt-Data-with-C

Encrypt:

public static string Encrypt(string toEncrypt, bool useHashing)
{
    byte[] keyArray;
    byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);

    System.Configuration.AppSettingsReader settingsReader = 
                                        new AppSettingsReader();
    // Get the key from config file

    string key = (string)settingsReader.GetValue("SecurityKey", 
                                                     typeof(String));
    //System.Windows.Forms.MessageBox.Show(key);
    //If hashing use get hashcode regards to your key
    if (useHashing)
    {
        MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
        keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
        //Always release the resources and flush data
        // of the Cryptographic service provide. Best Practice

        hashmd5.Clear();
    }
    else
        keyArray = UTF8Encoding.UTF8.GetBytes(key);

    TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
    //set the secret key for the tripleDES algorithm
    tdes.Key = keyArray;
    //mode of operation. there are other 4 modes.
    //We choose ECB(Electronic code Book)
    tdes.Mode = CipherMode.ECB;
    //padding mode(if any extra byte added)

    tdes.Padding = PaddingMode.PKCS7;

    ICryptoTransform cTransform = tdes.CreateEncryptor();
    //transform the specified region of bytes array to resultArray
    byte[] resultArray = 
      cTransform.TransformFinalBlock(toEncryptArray, 0, 
      toEncryptArray.Length);
    //Release resources held by TripleDes Encryptor
    tdes.Clear();
    //Return the encrypted data into unreadable string format
    return Convert.ToBase64String(resultArray, 0, resultArray.Length);
}

Decrypt:

public static string Decrypt(string cipherString, bool useHashing)
{
    byte[] keyArray;
    //get the byte code of the string

    byte[] toEncryptArray = Convert.FromBase64String(cipherString);

    System.Configuration.AppSettingsReader settingsReader = 
                                        new AppSettingsReader();
    //Get your key from config file to open the lock!
    string key = (string)settingsReader.GetValue("SecurityKey", 
                                                 typeof(String));

    if (useHashing)
    {
        //if hashing was used get the hash code with regards to your key
        MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
        keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
        //release any resource held by the MD5CryptoServiceProvider

        hashmd5.Clear();
    }
    else
    {
        //if hashing was not implemented get the byte code of the key
        keyArray = UTF8Encoding.UTF8.GetBytes(key);
    }

    TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
    //set the secret key for the tripleDES algorithm
    tdes.Key = keyArray;
    //mode of operation. there are other 4 modes. 
    //We choose ECB(Electronic code Book)

    tdes.Mode = CipherMode.ECB;
    //padding mode(if any extra byte added)
    tdes.Padding = PaddingMode.PKCS7;

    ICryptoTransform cTransform = tdes.CreateDecryptor();
    byte[] resultArray = cTransform.TransformFinalBlock(
                         toEncryptArray, 0, toEncryptArray.Length);
    //Release resources held by TripleDes Encryptor                
    tdes.Clear();
    //return the Clear decrypted TEXT
    return UTF8Encoding.UTF8.GetString(resultArray);
}

这篇关于如何在C#中实现三重DES(完整示例)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆