zoukankan      html  css  js  c++  java
  • C#几种加解密方法

    public static string key = "esgdkcmf";
    

     DES加密

                string encryptKeyStr = textBox1.Text;
                byte[] keyBytes = System.Text.Encoding.UTF8.GetBytes(key.Substring(0, 8));
                byte[] keyIv = keyBytes;
                byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptKeyStr);
                System.Security.Cryptography.DESCryptoServiceProvider provider = new System.Security.Cryptography.DESCryptoServiceProvider();
                System.IO.MemoryStream stream = new System.IO.MemoryStream();
                System.Security.Cryptography.CryptoStream cStream = new System.Security.Cryptography.CryptoStream(stream, provider.CreateEncryptor(keyBytes, keyIv), System.Security.Cryptography.CryptoStreamMode.Write);
                cStream.Write(inputByteArray, 0, inputByteArray.Length);
                cStream.FlushFinalBlock();
                richTextBox1.Text = Convert.ToBase64String(stream.ToArray());
    View Code

    DES解密

                tring encryptKeyStr = textBox1.Text;
                byte[] keyBytes = System.Text.Encoding.UTF8.GetBytes(key.Substring(0, 8));
                byte[] keyIv = keyBytes;
                byte[] inputByteArray = Convert.FromBase64String(encryptKeyStr);
                System.Security.Cryptography.DESCryptoServiceProvider provider = new System.Security.Cryptography.DESCryptoServiceProvider();
                System.IO.MemoryStream stream = new System.IO.MemoryStream();
                System.Security.Cryptography.CryptoStream cStream = new System.Security.Cryptography.CryptoStream(stream, provider.CreateDecryptor(keyBytes, keyIv), System.Security.Cryptography.CryptoStreamMode.Write);
                cStream.Write(inputByteArray, 0, inputByteArray.Length);
                cStream.FlushFinalBlock();
                richTextBox1.Text = Encoding.UTF8.GetString(stream.ToArray());
    View Code

    MD5加密

                string encryptKeyStr = textBox1.Text;
                System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
                richTextBox1.Text = BitConverter.ToString(md5.ComputeHash(Encoding.Default.GetBytes(encryptKeyStr))).Replace("-", "");
    View Code

    RC2加密

                string encryptKeyStr = textBox1.Text;
                byte[] keyBytes = System.Text.Encoding.UTF8.GetBytes(key.Substring(0, 8));
                byte[] keyIv = keyBytes;
                byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptKeyStr);
                System.Security.Cryptography.RC2CryptoServiceProvider provider = new System.Security.Cryptography.RC2CryptoServiceProvider();
                System.IO.MemoryStream stream = new System.IO.MemoryStream();
                System.Security.Cryptography.CryptoStream cStream = new System.Security.Cryptography.CryptoStream(stream, provider.CreateEncryptor(keyBytes, keyIv), System.Security.Cryptography.CryptoStreamMode.Write);
                cStream.Write(inputByteArray, 0, inputByteArray.Length);
                cStream.FlushFinalBlock();
                richTextBox1.Text = Convert.ToBase64String(stream.ToArray());
    View Code

    RC2解密

                string encryptKeyStr = textBox1.Text;
                byte[] keyBytes = System.Text.Encoding.UTF8.GetBytes(key.Substring(0, 8));
                byte[] keyIv = keyBytes;
                byte[] inputByteArray = Convert.FromBase64String(encryptKeyStr);
                System.Security.Cryptography.RC2CryptoServiceProvider provider = new System.Security.Cryptography.RC2CryptoServiceProvider();
                System.IO.MemoryStream stream = new System.IO.MemoryStream();
                System.Security.Cryptography.CryptoStream cStream = new System.Security.Cryptography.CryptoStream(stream, provider.CreateDecryptor(keyBytes, keyIv), System.Security.Cryptography.CryptoStreamMode.Write);
                cStream.Write(inputByteArray, 0, inputByteArray.Length);
                cStream.FlushFinalBlock();
                richTextBox1.Text = Encoding.UTF8.GetString(stream.ToArray());
    View Code

    AES加密

                string encryptKeyStr = textBox1.Text;
                byte[] keyBytes = System.Text.Encoding.UTF8.GetBytes(key.Substring(0, 8));
                byte[] keyIv = Convert.FromBase64String("Rkb4jvUy/ye7Cd7k89QQgQ==");
                byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptKeyStr);
                System.Security.Cryptography.Rijndael provider = System.Security.Cryptography.Rijndael.Create();
                System.IO.MemoryStream stream = new System.IO.MemoryStream();
                System.Security.Cryptography.CryptoStream cStream = new System.Security.Cryptography.CryptoStream(stream, provider.CreateEncryptor(keyBytes, keyIv), System.Security.Cryptography.CryptoStreamMode.Write);
                cStream.Write(inputByteArray, 0, inputByteArray.Length);
                cStream.FlushFinalBlock();
                richTextBox1.Text = Convert.ToBase64String(stream.ToArray());
    View Code

    AES解密

                string encryptKeyStr = textBox1.Text;
                byte[] keyBytes = System.Text.Encoding.UTF8.GetBytes(key.Substring(0, 8));
                byte[] keyIv = Convert.FromBase64String("Rkb4jvUy/ye7Cd7k89QQgQ==");
                byte[] inputByteArray = Convert.FromBase64String(encryptKeyStr);
                System.Security.Cryptography.Rijndael provider = System.Security.Cryptography.Rijndael.Create();
                System.IO.MemoryStream stream = new System.IO.MemoryStream();
                System.Security.Cryptography.CryptoStream cStream = new System.Security.Cryptography.CryptoStream(stream, provider.CreateDecryptor(keyBytes, keyIv), System.Security.Cryptography.CryptoStreamMode.Write);
                cStream.Write(inputByteArray, 0, inputByteArray.Length);
                cStream.FlushFinalBlock();
                richTextBox1.Text = Encoding.UTF8.GetString(stream.ToArray());
    View Code
  • 相关阅读:
    图像检索(image retrieval)- 11
    图像检索(image retrieval)- 10相关
    Mock.js简易教程,脱离后端独立开发,实现增删改查功能
    Azure Monitor (3) 对虚拟机磁盘设置自定义监控
    Azure Monitor (1) 概述
    Azure SQL Managed Instance (2) 备份SQL MI
    Azure Virtual Network (17) Private Link演示
    Azure Virtual Network (16) Private Link
    Azure Virtual Network (15) Service Endpoint演示
    Azure Virtual Network (14) Service Endpoint服务终结点
  • 原文地址:https://www.cnblogs.com/lishuo/p/4062991.html
Copyright © 2011-2022 走看看