using System;
using System.Security.Cryptography;
using System.Text; namespace DimoNetwork.Common.DEncrypt
{
public enum MD5ResultMode : byte
{
Strong = ,
Weak =
} /// <summary>
/// 在应用程序中定义用于单向加密文本的方法
/// </summary>
public class TextEncrypt
{
private TextEncrypt()
{
} #region ========加密======== /// <summary>
/// 加密
/// </summary>
/// <param name="Text"></param>
/// <returns></returns>
public static string Encrypt(string Text)
{
return Encrypt(Text, "DimoNet");
}
/// <summary>
/// 加密数据
/// </summary>
/// <param name="Text"></param>
/// <param name="sKey"></param>
/// <returns></returns>
public static string Encrypt(string Text, string sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray;
inputByteArray = Encoding.Default.GetBytes(Text);
des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(, ));
des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(, ));
System.IO.MemoryStream ms = new System.IO.MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, , inputByteArray.Length);
cs.FlushFinalBlock();
StringBuilder ret = new StringBuilder();
foreach (byte b in ms.ToArray())
{
ret.AppendFormat("{0:X2}", b);
}
return ret.ToString();
} /// <summary>
/// MD5 加密
/// </summary>
/// <param name="password">要加密的字符串</param>
/// <returns></returns>
public static string MD5EncryptPassword(string password, int? length = null)
{
if (password == null)
{
throw new ArgumentNullException("password");
}
return MD5EncryptPassword(password, MD5ResultMode.Strong, length);
} /// <summary>
/// MD5 加密
/// </summary>
/// <param name="password">要加密的字符串</param>
/// <param name="mode">加密强度</param>
/// <returns></returns>
public static string MD5EncryptPassword(string password, MD5ResultMode mode, int? length = null)
{
if (password == null)
{
throw new ArgumentNullException("password");
}
MD5CryptoServiceProvider provider = new MD5CryptoServiceProvider();
string str = BitConverter.ToString(provider.ComputeHash(Encoding.UTF8.GetBytes(password)));
if (length != null && length == )
{
str = BitConverter.ToString(provider.ComputeHash(Encoding.UTF8.GetBytes(password)), , );
}
provider.Clear();
if (mode != MD5ResultMode.Strong)
{
return str.Replace("-", null).Substring(, 0x10);
}
return str.Replace("-", null);
} #endregion #region ========解密======== /// <summary>
/// 解密
/// </summary>
/// <param name="Text"></param>
/// <returns></returns>
public static string Decrypt(string Text)
{
return Decrypt(Text, "DimoNet");
}
/// <summary>
/// 解密数据
/// </summary>
/// <param name="Text"></param>
/// <param name="sKey"></param>
/// <returns></returns>
public static string Decrypt(string Text, string sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
int len;
len = Text.Length / ;
byte[] inputByteArray = new byte[len];
int x, i;
for (x = ; x < len; x++)
{
// i = Convert.ToInt32(Text.Substring(x * 2, 2), 16);
i = System.Convert.ToInt32(Text.Substring(x * , ), );
inputByteArray[x] = (byte)i;
}
des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(, ));
des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(, ));
System.IO.MemoryStream ms = new System.IO.MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, , inputByteArray.Length);
cs.FlushFinalBlock();
return Encoding.Default.GetString(ms.ToArray());
} #endregion /// <summary>
/// Base64 解码
/// </summary>
/// <param name="message"></param>
/// <returns></returns>
public static string Base64Decode(string message)
{
byte[] bytes = Convert.FromBase64String(message);
return Encoding.UTF8.GetString(bytes);
}
/// <summary>
/// Base64 编码
/// </summary>
/// <param name="message"></param>
/// <returns></returns>
public static string Base64Encode(string message)
{
return Convert.ToBase64String(Encoding.UTF8.GetBytes(message));
}
/// <summary>
/// DSA 加密
/// </summary>
/// <param name="password">要加密的字符串</param>
/// <returns></returns>
public static string DSAEncryptPassword(string password)
{
if (password == null)
{
throw new ArgumentNullException("password");
}
DSACryptoServiceProvider provider = new DSACryptoServiceProvider();
string str = BitConverter.ToString(provider.SignData(Encoding.UTF8.GetBytes(password)));
provider.Clear();
return str.Replace("-", null);
}
/// <summary>
/// MD5 加密
/// </summary>
/// <param name="password">要加密的字符串</param>
/// <returns></returns>
public static string EncryptPassword(string password)
{
if (password == null)
{
throw new ArgumentNullException("password");
}
return MD5EncryptPassword(password);
}
/// <summary>
/// MD5 加密
/// </summary>
/// <param name="password">要加密的字符串</param>
/// <returns></returns>
public static string MD5EncryptPassword(string password)
{
if (password == null)
{
throw new ArgumentNullException("password");
}
return MD5EncryptPassword(password, MD5ResultMode.Strong);
}
/// <summary>
/// MD5 加密
/// </summary>
/// <param name="password">要加密的字符串</param>
/// <param name="mode">加密强度</param>
/// <returns></returns>
public static string MD5EncryptPassword(string password, MD5ResultMode mode)
{
if (password == null)
{
throw new ArgumentNullException("password");
}
MD5CryptoServiceProvider provider = new MD5CryptoServiceProvider();
string str = BitConverter.ToString(provider.ComputeHash(Encoding.UTF8.GetBytes(password)));
provider.Clear();
if (mode != MD5ResultMode.Strong)
{
return str.Replace("-", null).Substring(, 0x10);
}
return str.Replace("-", null);
}
/// <summary>
/// SHA1 加密
/// </summary>
/// <param name="password">要加密的字符串</param>
/// <returns></returns>
public static string SHA1EncryptPassword(string password)
{
if (password == null)
{
throw new ArgumentNullException("password");
}
SHA1CryptoServiceProvider provider = new SHA1CryptoServiceProvider();
string str = BitConverter.ToString(provider.ComputeHash(Encoding.UTF8.GetBytes(password)));
provider.Clear();
return str.Replace("-", null);
}
/// <summary>
/// SHA256 加密
/// </summary>
/// <param name="password">要加密的字符串</param>
/// <returns></returns>
public static string SHA256(string password)
{
byte[] bytes = Encoding.UTF8.GetBytes(password);
SHA256Managed managed = new SHA256Managed();
return Convert.ToBase64String(managed.ComputeHash(bytes));
}
}
}

MD5密码加密的更多相关文章

  1. 使用okHttp登录、Md5密码加密

    1.使用okHttp3登录 2.Md5密码加密 3.完整代码 4.项目案例 使用okHttp3登录: 使用okHttp3之前要在build.gradle引入okHttp3的依赖(顺便引入解析数据的gs ...

  2. Apach Shiro MD5密码加密过程(明文生成密码过程)详细解析

    前言: 最近再项目当中使用的ApachShiro安全框架,对于权限和服务器资源的保护都有一个很好的管理.前期主要参考的文章有 项目中设计密码的加盐处理以及二次加密问题,跟着断点 一步步揭开Apach ...

  3. Django -MD5密码加密与登录

    直接贴代码 login_reg.py from django.shortcuts import render, redirect from web.forms.login_reg import Reg ...

  4. 015 Android md5密码加密及其工具类

    1.md5加密介绍 MD5算法是广泛使用的杂凑函数,也就是哈希函数,英文全拼是:Message Digest Algorithm,对应的中文名字是消息摘要算法. MD5加密:将字符串转换成 32位的字 ...

  5. 添加MD5 密码加密

        编辑 /etc/grub/grub.conf 配置文件 password = 123456 password --md5 $5$H.........SS grub-crypt  --md5   ...

  6. C#:使用MD5对用户密码加密与解密

    C#中常涉及到对用户密码的加密于解密的算法,其中使用MD5加密是最常见的的实现方式.本文总结了通用的算法并结合了自己的一点小经验,分享给大家. 一.使用16位.32位.64位MD5方法对用户名加密 1 ...

  7. JAVA中使用MD5加密实现密码加密

    1.新建Md5.java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 package c ...

  8. 使用md5的密码加密,处理用户的密码

    需求 1.新增用户保存:使用md5的密码加密,如果用户没有填写密码,设置初始密码“123”: 2.修改用户保存:使用md5的加密加密 *如果修改了密码,需要进行md5的密码加密: *如果没有修改密码, ...

  9. 转 C#:使用MD5对用户密码加密与解密

    C#中常涉及到对用户密码的加密于解密的算法,其中使用MD5加密是最常见的的实现方式.本文总结了通用的算法并结合了自己的一点小经验,分享给大家. 一.使用16位.32位.64位MD5方法对用户名加密 1 ...

随机推荐

  1. java学习-排序及加密签名时数据排序方式

    排序有两种 1. 类实现comparable接口调用List.sort(null)或Collections.sort(List<T>)方法进行排序 jdk内置的基本类型包装类等都实现了Co ...

  2. 使用秘钥ssh登录远程服务器

    一.使用公钥远程登录ssh服务器方式 1.1 在客户端使用ssh-keygen  生成密匙 steven:~ admin$ ssh-keygen //客户端生成秘钥 Generating public ...

  3. (Dijkstra)迪杰斯特拉算法-最短路径算法

    迪杰斯特拉算法是从一个顶点到其余各顶点的最短路径算法,解决的是有向图中最短路径问题.迪杰斯特拉算法主要特点是以起始点为中心向外层层扩展,直到扩展到终点为止. 算法思想:设G=(V,E)是一个带权有向图 ...

  4. PHP 使用 GeoIP 进行不同国家 ip 测试

    $ip = "67.220.91.30";// USA switch (mt_rand(0, 15)) { case 0:// India $ip = "210.212. ...

  5. JavaScript 片段

    js split 的用法和定义 js split分割字符串成数组的实例代码 <script language="javascript"> str="2,2,3 ...

  6. 如何替代即将淘汰的Flash方案?

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由MarsBoy发表于云+社区专栏 | 导语 Web技术飞速发展的如今,我们在感受新技术带来的便捷和喜悦的同时,也时常在考虑着一个问题: ...

  7. Linux分区方式及关闭iptables和selinux的方式

    分区方式一般有三种 第一种:数据不是很重要 /boot(系统的引导分区): 系统引导的信息/软件 系统的内核   200M swap( 交换分区): 为了避免系统内存用光了导致系统 宕机 如果系统内存 ...

  8. SQL Server 数据库的鼠标操作

    在数据库中一些操作用鼠标进行可视化操作更方便快捷 一 SQL Server 开启 任务栏——任务管理器——服务——MSSQLSERVER 开启 我的电脑——控制面板——管理工具——服务——MSSQLS ...

  9. JSON & Ajax

    Ajax是异步JavaScript和XML是用来在客户端作为一组相互关联的Web开发技术,以创建异步Web应用程序. Ajax模型,Web应用程序可以发送数据和检索数据从一个服务器,而不干扰现有的页面 ...

  10. [日常] Go语言圣经--作用域,基础数据类型,整型

    go语言圣经-作用域 1.一个声明语句将程序中的实体和一个名字关联,比如一个函数或一个变量 2.一个变量的生命周期是指程序运行时变量存在的有效时间段;声明语句的作用域对应的是一个源代码的文本区域,它是 ...