• .NET中DES加密算法的基本使用


    //或?者?下?面?的?另?一?个?程?序?代?码?:?
    using System;
    using System.Security.Cryptography;
    using System.IO;
    using System.Text;
     
    public class Test
    {
        //加?密?
    public string DesEncrypt(string strText, string strEncrKey) 
    { 
    byte[] byKey=null; 
    byte[] IV= {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF}; 
    try 
    { 
    byKey = System.Text.Encoding.UTF8.GetBytes(strEncrKey.Substring(0,8)); 
    DESCryptoServiceProvider des = new DESCryptoServiceProvider(); 
    byte[] inputByteArray = Encoding.UTF8.GetBytes(strText); 
    MemoryStream ms = new MemoryStream(); 
    CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write) ; 
    cs.Write(inputByteArray, 0, inputByteArray.Length); 
    cs.FlushFinalBlock(); 
    return Convert.ToBase64String(ms.ToArray()); 
    } 
    catch(System.Exception error) 
    { 
    MessageBox.Show(error.Message); 
    return "error:" +error.Message+"\r"; 
    } 
    }
    //解?密?函?数?
    public string DesDecrypt(string strText,string sDecrKey) 
    { 
    byte[] byKey = null; 
    byte[] IV= {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF}; 
    byte[] inputByteArray = new Byte[strText.Length]; 
    try 
    { 
    byKey = System.Text.Encoding.UTF8.GetBytes(sDecrKey.Substring(0,8)); 
    DESCryptoServiceProvider des = new DESCryptoServiceProvider(); 
    inputByteArray = Convert.FromBase64String(strText); 
    MemoryStream ms = new MemoryStream(); 
    CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write); 
    cs.Write(inputByteArray, 0, inputByteArray.Length); 
    cs.FlushFinalBlock(); 
    System.Text.Encoding encoding = new System.Text.UTF8Encoding(); 
    return encoding.GetString(ms.ToArray()); 
    } 
    catch(System.Exception error) 
    { 
    MessageBox.Show(error.Message); 
    return "error:"+error.Message+"\r"; 
    } 
    } 
     
     
    public class EncryptStringDES 
    {
     
        public static void Main(String[] args) {
            if (args.Length < 1) {
                Console.WriteLine("Usage: des_demo ", args[0]);
                return;
            }
     
            // 使?用?UTF8函?数?加?密?输?入?参?数?
            UTF8Encoding utf8Encoding = new UTF8Encoding();
            byte[] inputByteArray = utf8Encoding.GetBytes(args[0].ToCharArray());
     
            // 方?式?一?:?调?用?默?认?的?DES实?现?方?法?DES_CSP.
            DES des = DES.Create();
            // 方?式?二?:?直?接?使?用?DES_CSP()实?现?DES的?实?体?
            //DES_CSP DES = new DES_CSP();
            // 初?始?化?DES加?密?的?密?钥?和?一?个?随?机?的?、?比?特?的?初?始?化?向?量?(IV)
            Byte[] key = {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef};
            Byte[] IV = {0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef};
            des.Key = key;
            des.IV = IV;
     
            // 建?立?加?密?流?
              SymmetricStreamEncryptor sse = des.CreateEncryptor();
     
            // 使?用?CryptoMemoryStream方?法?获?取?加?密?过?程?的?输?出?
            CryptoMemoryStream cms = new CryptoMemoryStream();
     
            // 将?SymmetricStreamEncryptor流?中?的?加?密?数?据?输?出?到?CryptoMemoryStream中?
            sse.SetSink(cms);
     
            // 加?密?完?毕?,?将?结?果?输?出?到?控?制?台?
            sse.Write(inputByteArray);
            sse.CloseStream();
     
            // 获?取?加?密?数?据?
            byte[] encryptedData = cms.Data;
     
            // 输?出?加?密?后?结?果?
            Console.WriteLine("加?密?结?果?:?");
            for (int i = 0; i < encryptedData.Length; i++) {
                Console.Write("{0:X2} ",encryptedData[i]);
            }
            Console.WriteLine();
     
            //上?面?演?示?了?如?何?进?行?加?密?,?下?面?演?示?如?何?进?行?解?密?
            SymmetricStreamDecryptor ssd = des.CreateDecryptor();
            cms = new CryptoMemoryStream();
            ssd.SetSink(cms);
            ssd.Write(encryptedData);
            ssd.CloseStream();
     
            byte[] decryptedData = cms.Data;
            char[] decryptedCharArray = utf8Encoding.GetChars(decryptedData);
            Console.WriteLine("解?密?后?数?据?:?");
            Console.Write(decryptedCharArray);
            Console.WriteLine();
        }
     
     
    }
  • 相关阅读:
    2021年欺骗式防御技术将迎来爆发
    可以简化决策过程的10个大数据源
    “AI+”改变世界!不同领域的5大人工智能趋势
    预测2021: 区块链领域新景观
    后量子时代的密码学
    考试
    进度总结8
    进度总结7
    进度总结6
    进度总结5
  • 原文地址:https://www.cnblogs.com/rockniu/p/1573960.html
Copyright © 2020-2023  润新知