数据加密标准(DES)的C#实现(3)--(将BitConverter.ToString的结果转回byte[])

简介: /**//* * 数据加密标准(DES)的C#实现(3) * 将BitConverter.ToString的结果转回byte[] *  * 采用随机的密钥Key和初始化向量IV加密 * 使用随机密码的好处:系统不会产生弱密钥 * 备注:本例与《数据加密标准(DES)的C#实现(2)》本质相同,只是采用BitConverter.
/**/ /*
 * 数据加密标准(DES)的C#实现(3)
 * 将BitConverter.ToString的结果转回byte[]
 * 
 * 采用随机的密钥Key和初始化向量IV加密
 * 使用随机密码的好处:系统不会产生弱密钥
 * 备注:本例与《数据加密标准(DES)的C#实现(2)》本质相同,只是采用BitConverter.ToString
 * 输出密文、密钥和初始化向量,而不是采用Base64编码格式
 * 
 * 
 * 夏春涛 Email:xChuntao@163.com 
 * Blog:
http://bluesky521.cnblogs.com
 * 运行环境:.net2.0 framework
 
*/


/**/ /* 
 * 关于DES加密中的初始化向量IV:
 * 对于给定的密钥 k,不使用初始化向量的简单块密码将同一个纯文本输入块加密为
 * 同一个密码文本输出块。如果您的纯文本流中有重复块,则您的密码文本流中也会
 * 有重复块。如果未经授权的用户知道了您的纯文本块结构的任何信息,他们就可以
 * 利用该信息来解密已知的密码文本块,并有可能重新获得您的密钥。为了防止这个
 * 问题,前一个块中的信息被混合到下一个块的加密过程中。这样一来,两个相同的
 * 纯文本块的输出就变得不一样了。由于此技术使用前一个块加密下一个块,因此需
 * 要初始化向量来加密数据的第一个块。 
 
*/


using  System;
using  System.Collections.Generic;
using  System.Text;
using  System.Security.Cryptography;
using  System.IO;

namespace  DES_App3
{
    
class Program
    
{
        
static void Main(string[] args)
        
{
            
string str_plain_text = "How are you?";
            Console.WriteLine(
"原文:" + str_plain_text);

            
string KEY_64 = ""
            
string IV_64 = "";  

            
string str_cypher_text = DES_Encrypt(str_plain_text, out KEY_64, out IV_64);
            Console.WriteLine(
"密文:" + str_cypher_text);
            
            Console.WriteLine(
"解密:" + DES_Decrypt(str_cypher_text, KEY_64, IV_64));

            Console.WriteLine(
"本次密钥:" + KEY_64);
            Console.WriteLine(
"本次初始化向量:" + IV_64);
            Console.WriteLine();

            
//-------------------------------

            str_cypher_text 
= DES_Encrypt(str_plain_text, out KEY_64, out IV_64, false);
            Console.WriteLine(
"密文:" + str_cypher_text);

            Console.WriteLine(
"解密:" + DES_Decrypt(str_cypher_text, KEY_64, IV_64));

            Console.WriteLine(
"本次密钥:" + KEY_64);
            Console.WriteLine(
"本次初始化向量:" + IV_64);
            Console.WriteLine();
            
        }


        
//将BitConverter.ToString字符串,如"98-ED-0S-9A",还原转换为byte[]
        static public byte[] BitStr_ToBytes(string bit_str)
        
{
            
string[] arrSplit = bit_str.Split('-');
            
byte[] byteTemp = new byte[arrSplit.Length];
            
for (int i = 0; i < byteTemp.Length; i++)
            
{
                byteTemp[i] 
= byte.Parse(arrSplit[i], System.Globalization.NumberStyles.AllowHexSpecifier);

            }


            
return byteTemp;
        }


        
//将BitConverter.ToString字符串(不含'-'),如"98ED0S9A",还原转换为byte[]
        static public byte[] BitStr_ToBytes2(string bit_str)
        
{
            
int n = bit_str.Length / 2 - 1;
            
for (int i = n; i > 0; i--)
            
{
                bit_str 
= bit_str.Insert(i * 2"-");
            }


            
return BitStr_ToBytes(bit_str);
        }


        
//----
        DES加密/解密#region DES加密/解密
        
/**//// <summary>
        
/// DES加密
        
/// </summary>
        
/// <param name="str_plain_text">明文</param>
        
/// <param name="str_des_key">密钥,8个字符(64bit)</param>
        
/// <param name="str_des_iv">初始向量,8个字符(64bit)</param>
        
/// <param name="hasSubSign">输出密文、str_des_key和str_des_iv时,是否保留BitConvert.ToString中的减号</param>
        
/// <returns>密文</returns>

        static public string DES_Encrypt(string str_plain_text, out string str_des_key, out string str_des_iv, bool hasSubSign)
        
{
            
string str_cypher_text = "";

            DESCryptoServiceProvider cryptoProvider 
= new DESCryptoServiceProvider();

            MemoryStream ms 
= new MemoryStream();
            CryptoStream cst 
= new CryptoStream(ms, cryptoProvider.CreateEncryptor(), CryptoStreamMode.Write);

            StreamWriter sw 
= new StreamWriter(cst);
            sw.Write(str_plain_text);
            sw.Flush();
            cst.FlushFinalBlock();
            sw.Flush();
            
//-----
            str_cypher_text = BitConverter.ToString(ms.GetBuffer(),0,(int)ms.Length);//**
            str_des_key = BitConverter.ToString(cryptoProvider.Key);//**
            str_des_iv = BitConverter.ToString(cryptoProvider.IV);  //**

            
if (!hasSubSign)
            
{
                str_cypher_text 
= str_cypher_text.Replace("-""");
                str_des_key 
= str_des_key.Replace("-","");
                str_des_iv 
= str_des_iv.Replace("-","");
            }


            
return str_cypher_text;
        }


        
static public string DES_Encrypt(string str_plain_text, out string str_des_key, out string str_des_iv)
        
{
            
return DES_Encrypt(str_plain_text, out str_des_key, out str_des_iv, true);
        }


        
/**//// <summary>
        
/// DES解密
        
/// </summary>
        
/// <param name="str_cypher_text">密文</param>
        
/// <param name="str_des_key">密钥,8个字符(64bit)</param>
        
/// <param name="str_des_iv">初始向量,8个字符(64bit)</param>
        
/// <returns>明文</returns>

        static public string DES_Decrypt(string str_cypher_text, string str_des_key, string str_des_iv)
        
{
            
byte[] byKey;
            
byte[] byIV;
            
byte[] byEnc;
            
try
            
{
                
if (str_cypher_text.IndexOf('-'> 0 && str_des_key.IndexOf('-'> 0 && str_des_iv.IndexOf('-'> 0)//有"-"号
                {
                    byKey 
= BitStr_ToBytes(str_des_key);
                    byIV 
= BitStr_ToBytes(str_des_iv);
                    byEnc 
= BitStr_ToBytes(str_cypher_text);
                }

                
else//无"-"号
                {
                    byKey 
= BitStr_ToBytes2(str_des_key);
                    byIV 
= BitStr_ToBytes2(str_des_iv);
                    byEnc 
= BitStr_ToBytes2(str_cypher_text);
                }

            }

            
catch
            
{
                
return null;
            }


            DESCryptoServiceProvider cryptoProvider 
= new DESCryptoServiceProvider();
            MemoryStream ms 
= new MemoryStream(byEnc);
            CryptoStream cst 
= new CryptoStream(ms, cryptoProvider.CreateDecryptor(byKey, byIV), CryptoStreamMode.Read);
            StreamReader sr 
= new StreamReader(cst);
            
string str_plain_text = sr.ReadToEnd();


            
return str_plain_text;
        }

        
#endregion

    }

}

源码附件: /Files/bluesky521/DES_Hash_Demo.rar

目录
相关文章
|
24天前
|
Java 数据安全/隐私保护
des加密+base64编码,base64解码+des解密
des加密+base64编码,base64解码+des解密
22 0
|
4月前
|
算法 搜索推荐 Java
DES - 对称加密算法简要介绍与JAVA实现
DES - 对称加密算法简要介绍与JAVA实现
53 2
|
3月前
|
算法 安全 数据安全/隐私保护
C/C++学习 -- 分组加密算法(DES算法)
C/C++学习 -- 分组加密算法(DES算法)
34 0
|
3月前
|
JavaScript 前端开发 算法
JavaScript学习 -- 对称加密算法DES
JavaScript学习 -- 对称加密算法DES
15 0
|
19天前
|
算法 安全 程序员
详解 DES加密技术 | 凯撒密码 | 栅栏密码
详解 DES加密技术 | 凯撒密码 | 栅栏密码
105 0
|
6月前
|
算法 安全 数据安全/隐私保护
DES加密算法
DES加密算法
23 0
|
6月前
|
算法 安全 数据安全/隐私保护
C/C++学习 -- 分组加密算法(DES算法)
C/C++学习 -- 分组加密算法(DES算法)
131 0
|
6月前
|
JavaScript 前端开发 算法
JavaScript学习 -- 对称加密算法DES
JavaScript学习 -- 对称加密算法DES
21 0
|
6月前
|
算法 安全 调度
DES加密算法解析
DES加密算法解析
112 0
|
6月前
|
数据采集 JavaScript 前端开发
“探秘JS加密算法:MD5、Base64、DES/AES、RSA你都知道吗?”
“探秘JS加密算法:MD5、Base64、DES/AES、RSA你都知道吗?”
111 0