zoukankan      html  css  js  c++  java
  • 数据加密标准(DES)的C#实现(3)(将BitConverter.ToString的结果转回byte[])

    /*
     * 数据加密标准(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加密/解密
        }

    }

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

  • 相关阅读:
    FJNUOJ Yehan’s hole(容斥求路径数 + 逆元)题解
    FJNUOJ the greed of Yehan(最长路 + 权值乘积转化)题解
    BZOJ 2956 模积和
    BZOJ 2299 向量
    codeforces 718c Sasha and Array
    BZOJ 3747 Kinoman
    BZOJ 2431 逆序对数列
    BZOJ 3289 Mato的文件管理
    BZOJ 3781 小B的询问
    BZOJ 2038 小Z的袜子(hose)
  • 原文地址:https://www.cnblogs.com/SummerRain/p/1005578.html
Copyright © 2011-2022 走看看