博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
DES,AeS加解密,MD5,SHA加密
阅读量:6210 次
发布时间:2019-06-21

本文共 11406 字,大约阅读时间需要 38 分钟。

1、DES一共就有4个参数参与运作:明文、密文、密钥、向量。其中这4者的关系可以理解为:

  密文=明文+密钥+向量;

  明文=密文-密钥-向量;
  为什么要向量这个参数呢?因为如果有一篇文章,有几个词重复,那么这个词加上密钥形成的密文,仍然会重复,这给破解者有机可乘,破解者可以根据重复的内容,猜出是什么词,然而一旦猜对这个词,那么,他就能算出密钥,整篇文章就被破解了!加上向量这个参数以后,每块文字段都会依次加上一段值,这样,即使相同的文字,加密出来的密文,也是不一样的,算法的安全性大大提高!

 

2、AES是美国联邦政府采用的商业及政府数据加密标准,预计将在未来几十年里代替DES在各个领域中得到广泛应用。AES提供128位密钥,因此,128位AES的加密强度是56位DES加密强度的1021倍还多。假设可以制造一部可以在1秒内破解DES密码的机器,那么使用这台机器破解一个128位AES密码需要大约149亿万年的时间。

 

3、MD5和SHA1散列通常被用于密码中,很多人称其为散列算法。散列是不可逆的,也就是没有了"解密"这个说法。

 

des加解密参考:http://www.faceye.net/search/110023.html

加解密只是参考:http://blog.csdn.net/z1x1c1v1/article/details/8755032

 

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Security.Cryptography;using System.IO;namespace WinForms_Crypt{    public partial class Form3 : Form    {        ///         /// 加密解密方式        ///         private enum CryptType        {            MD5,DES,AES        }        ///         /// 当前使用的加解密方式        ///         CryptType type = CryptType.MD5;        public Form3()        {            InitializeComponent();            if (type == CryptType.MD5)            {                button_Decrypt.Enabled = false;                button_Encrypt.Enabled = true;            }            else if (type == CryptType.DES)            {                button_Decrypt.Enabled = true;                button_Encrypt.Enabled = true;            }            else if (type == CryptType.AES)            {                button_Decrypt.Enabled = true;                button_Encrypt.Enabled = true;            }        }        ///         /// 加密        ///         ///         ///         private void button_Encrypt_Click(object sender, EventArgs e)        {            //明文             string MingWen = textBox_DecryptText.Text.Trim();            //加密得到的密文            string MiWen = "";            if (MingWen != "")            {                if (type == CryptType.MD5)                {                    MiWen = "MD5Base64:"+MD5_Crypt.GetHashStr(MingWen);                    MiWen += "\r\n MD5:" + MD5_Crypt2.MD5(MiWen);                    MiWen += "\r\n SHA128:" + MD5_Crypt2.SHA128(MiWen);                    MiWen += "\r\n SHA256:" + MD5_Crypt2.SHA256(MiWen);                    MiWen += "\r\n SHA384:" + MD5_Crypt2.SHA384(MiWen);                    MiWen += "\r\n SHA512:" + MD5_Crypt2.SHA512(MiWen);                }                else if (type == CryptType.DES)                {                    MiWen = DES_Crypt.Encrypt(MingWen);                }                else if (type == CryptType.AES)                {                    MiWen = AeS_Crypt.Encrypt(MingWen);                }             }            textBox_EncyptText.Text = MiWen;        }        ///         /// 解密        ///         ///         ///         private void button_Decrypt_Click(object sender, EventArgs e)        {            //密文            string MiWen = textBox_EncyptText.Text.Trim();            //解密得到的明文            string MingWen = "";            //无法解析哈希值            if (MiWen != "")            {                if (type == CryptType.MD5)                {                    MessageBox.Show("MD5无法解密");                }                else if (type == CryptType.DES)                {                    MingWen = DES_Crypt.Decrypt(MiWen);                }                else if (type == CryptType.AES)                {                    MingWen = AeS_Crypt.Decrypt(MiWen);                }            }            textBox_DecryptText.Text = MingWen;        }    }    ///     /// MD5加密    ///     public class MD5_Crypt    {        ///         /// MD5加密        ///         /// 原文        /// 
加密后的字节数组
public static byte[] GetHash(byte[] inputByt) { HashAlgorithm algorithm = HashAlgorithm.Create("MD5"); return algorithm.ComputeHash(inputByt); } /// /// MD5加密 /// /// 原文 ///
加密后的字节数组
public static byte[] GetHash(string inputStr) { byte[] inputByt = new UnicodeEncoding().GetBytes(inputStr); return GetHash(inputByt); } /// /// MD5加密 /// /// 原文 ///
BASE64编码字符串
public static string GetHashStr(string inputStr) { return Convert.ToBase64String(GetHash(inputStr)); } /// /// MD5加密 /// /// 原文 ///
32位编码
public static string GetHashString32(string inputStr) { string hash = BitConverter.ToString(GetHash(inputStr)); return hash.Replace("-", ""); } /// /// MD5加密 /// /// 原文 ///
32位编码
public static string GetHashString(byte[] inputStr) { string hash = BitConverter.ToString(GetHash(inputStr)); return hash.Replace("-", ""); } } /// /// DES加解密 /// public class DES_Crypt { /// /// 获取密钥 /// private static string Key { get { return @"P@+#wG%A"; } } /// /// 获取向量 /// private static string IV { get { return @"L*n67}G\Mk@k%:~Y"; } } /// /// DES加密 /// /// 明文字符串 ///
密文
public static string Encrypt(string plainStr) { byte[] bKey = Encoding.UTF8.GetBytes(Key); byte[] bIV = Encoding.UTF8.GetBytes(IV); byte[] byteArray = Encoding.UTF8.GetBytes(plainStr); string encrypt = null; DESCryptoServiceProvider des = new DESCryptoServiceProvider(); try { using (MemoryStream mStream = new MemoryStream()) { using (CryptoStream cStream = new CryptoStream(mStream, des.CreateEncryptor(bKey, bIV), CryptoStreamMode.Write)) { cStream.Write(byteArray, 0, byteArray.Length); cStream.FlushFinalBlock(); encrypt = Convert.ToBase64String(mStream.ToArray()); } } } catch { } des.Clear(); return encrypt; } /// /// DES解密 /// /// 密文字符串 ///
明文
public static string Decrypt(string encryptStr) { byte[] bKey = Encoding.UTF8.GetBytes(Key); byte[] bIV = Encoding.UTF8.GetBytes(IV); byte[] byteArray = Convert.FromBase64String(encryptStr); string decrypt = null; DESCryptoServiceProvider des = new DESCryptoServiceProvider(); try { using (MemoryStream mStream = new MemoryStream()) { using (CryptoStream cStream = new CryptoStream(mStream, des.CreateDecryptor(bKey, bIV), CryptoStreamMode.Write)) { cStream.Write(byteArray, 0, byteArray.Length); cStream.FlushFinalBlock(); decrypt = Encoding.UTF8.GetString(mStream.ToArray()); } } } catch { } des.Clear(); return decrypt; } } /// /// AES加解密 /// public class AeS_Crypt { /// /// 获取密钥 /// private static string Key { get { return @"qO[NB]6,YF}gefcaj{+oESb9d8>Z'e9M"; } } /// /// 获取向量 /// private static string IV { get { return @"L+\~f4.Ir)b$=pkf"; } } /// /// AES加密 /// /// 明文字符串 ///
密文
public static string Encrypt(string plainStr) { byte[] bKey = Encoding.UTF8.GetBytes(Key); byte[] bIV = Encoding.UTF8.GetBytes(IV); byte[] byteArray = Encoding.UTF8.GetBytes(plainStr); string encrypt = null; Rijndael aes = Rijndael.Create(); try { using (MemoryStream mStream = new MemoryStream()) { using (CryptoStream cStream = new CryptoStream(mStream, aes.CreateEncryptor(bKey, bIV), CryptoStreamMode.Write)) { cStream.Write(byteArray, 0, byteArray.Length); cStream.FlushFinalBlock(); encrypt = Convert.ToBase64String(mStream.ToArray()); } } } catch { } aes.Clear(); return encrypt; } /// /// AES解密 /// /// 密文字符串 ///
明文
public static string Decrypt(string encryptStr) { byte[] bKey = Encoding.UTF8.GetBytes(Key); byte[] bIV = Encoding.UTF8.GetBytes(IV); byte[] byteArray = Convert.FromBase64String(encryptStr); string decrypt = null; Rijndael aes = Rijndael.Create(); try { using (MemoryStream mStream = new MemoryStream()) { using (CryptoStream cStream = new CryptoStream(mStream, aes.CreateDecryptor(bKey, bIV), CryptoStreamMode.Write)) { cStream.Write(byteArray, 0, byteArray.Length); cStream.FlushFinalBlock(); decrypt = Encoding.UTF8.GetString(mStream.ToArray()); } } } catch { } aes.Clear(); return decrypt; } } public class MD5_Crypt2 { //16字节,128位 public static string MD5(string str) { byte[] buffer = Encoding.UTF8.GetBytes(str); MD5CryptoServiceProvider MD5 = new MD5CryptoServiceProvider(); byte[] byteArr = MD5.ComputeHash(buffer); return BitConverter.ToString(byteArr); } //20字节,160位 public static string SHA128(string str) { byte[] buffer = Encoding.UTF8.GetBytes(str); SHA1CryptoServiceProvider SHA1 = new SHA1CryptoServiceProvider(); byte[] byteArr = SHA1.ComputeHash(buffer); return BitConverter.ToString(byteArr); } //32字节,256位 public static string SHA256(string str) { byte[] buffer = Encoding.UTF8.GetBytes(str); SHA256CryptoServiceProvider SHA256 = new SHA256CryptoServiceProvider(); byte[] byteArr = SHA256.ComputeHash(buffer); return BitConverter.ToString(byteArr); } //48字节,384位 public static string SHA384(string str) { byte[] buffer = Encoding.UTF8.GetBytes(str); SHA384CryptoServiceProvider SHA384 = new SHA384CryptoServiceProvider(); byte[] byteArr = SHA384.ComputeHash(buffer); return BitConverter.ToString(byteArr); } //64字节,512位 public static string SHA512(string str) { byte[] buffer = Encoding.UTF8.GetBytes(str); SHA512CryptoServiceProvider SHA512 = new SHA512CryptoServiceProvider(); byte[] byteArr = SHA512.ComputeHash(buffer); return BitConverter.ToString(byteArr); } }}

 

转载于:https://www.cnblogs.com/gsk99/p/5150382.html

你可能感兴趣的文章
环绕型布局
查看>>
CSS中font-style的属性有Italic oblique,它们俩的区别是什么呢?
查看>>
gitlab结构分析
查看>>
c++类构造函数初始化列表
查看>>
实验二+138+牟平
查看>>
Eval
查看>>
WebForm 使用点滴。。。。
查看>>
fiddler还是浏览器的问题
查看>>
springmvc全局异常处理
查看>>
Unity 执行命令行
查看>>
游戏爱好调查问卷结果
查看>>
[C++11]shared_ptr效率比较
查看>>
防止 NSTimer retain 作为 target 的 self
查看>>
tabhost使用
查看>>
Android笔记之启动界面的设置
查看>>
js的数据类型
查看>>
【转】Oracle/PLSQL: Decode Function
查看>>
读书是为了生命的完整
查看>>
使用位运算计算两个整数的加减
查看>>
你真的会玩SQL吗?查询指定节点及其所有父节点的方法
查看>>