using System;
十载的宣威网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。成都全网营销推广的优势是能够根据用户设备显示端的尺寸不同,自动调整宣威建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。创新互联从事“宣威网站设计”,“宣威网站推广”以来,每个客户项目都认真落实执行。
using System.Text;
using System.Security.Cryptography;
namespace MD5
{
public class MD5
{
// 32 位
public static String Encrypt(String s)
{
MD5 md5 = new MD5CryptoServiceProvider();
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(s);
bytes = md5.ComputeHash(bytes);
md5.Clear();
string ret = "";
for (int i = 0; i bytes.Length; i++)
{
ret += Convert.ToString(bytes[i], 16).PadLeft(2, '0');
}
return ret.PadLeft(32, '0');
}
// 16 位
public static string Encrypt16(string ConvertString)
{
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
string t2 = BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(ConvertString)), 4, 8);
t2 = t2.Replace("-", "");
return t2;
}
}
}
-------------------------------------
引用MD5命名空间
using MD5;
直接用MD5.Encrypt(textbox1.Text);即可加密;
MD5没有解密的算法,要不然就不用加密了,网上有一些解密的网站,有些能解密,有些解密不了,有些还需要收费才能解密。
刚好以前写过一篇文章,提取了一部分
对敏感的数据进行加密是必要的,如用户的密码的加密。在要对数据进行加密前得确定要用何种加密方式,加密分类有很多种,从可逆角度可分为可逆和不可逆,从加密算法可分为秘密密钥算法、公开密钥算法(用于加密、数据签名和密钥管理)以及单向散列函数等。md5加密是不可逆的,md5加密的具体实现又分为许多种。
加密为什么要采用不可逆?举个例子,当你输入的密码进行加密的密文被截取,那他也要把密文解密成原文,这时由于不可逆,那他要破解密码的难度就提高,除非他可以越过加密那一步直接提供密文,这样就达到安全的目的。每当我们忘记密码进行找回密码,大多数网站要求我们输入新的密码而不是直接告诉我们原来的密码,这是由于不可逆造成的,但这又不是坏处,为什么这么说?如果在找回密码时能够知道原来的密码,那么卧底就不用修改密码来监控所有人,而所有人又不知情。但并不是所有网站都采用不可逆的算法,笔者在某网站注册过用户,有一段没登录过,那网站就会给笔者一份提醒邮件,而这提醒邮件又显示着笔者的密码,这很可能是这个网站采用的不是不可逆的算法。
今天为一个网友实现了一个简易的自定义加密方式,使用的是Visual Basic 6.0。关键代码如下:
Dim decode As String, encode As String, oldString As String, newString As String
Dim i As Integer
decode = "1234567890" '加密原文对照字符,不应该出错相同的字符
encode = "eoSDriKjsd" '加密成密文的对照字符,字符个数不能少于decode,否则极易造成出错
oldString = "87232" '待加密的字符串,所有字符都必需能够在decode里找到对应位置
newString = "" '存放加密后的字符串
For i = 1 To Len(oldString)
newString = newString + Mid$(encode, Instr(decode, Mid$(oldString, i, 1)), 1)
Next i
Print "原文:"; oldString
Print "密文:"; newString
decode和encode的值可以根据需要设置,oldString可以由文框输入,如果encode里的字符俩俩不相同,那么只需调换decode和encode的值,就可以实现解密,否则加密后的密文为不可逆。
当然,在实际的应用种应增加加密的算法复杂度,让密文不至于被人轻而易举破解。更多的加密知识请参考相关文档。
希望回答对你有帮助
下面是完整的类,可以设置任意密码
'DES及md5加密解密----添加引用中添加对system.web的引用。
Imports System.Security.Cryptography
Imports System
Imports System.Text
Imports System.Web
''' summary
''' DES加密类
''' /summary
''' remarks/remarks
Public Class DESEncrypt
Public Sub DESEncrypt()
End Sub
Public Shared Function Encrypt(ByVal Text As String) As String
Return Encrypt(Text, "12345678")
End Function
Public Shared Function Encrypt(ByVal Text As String, ByVal sKey As String) As String
Dim des As New DESCryptoServiceProvider()
Dim inputByteArray As Byte()
inputByteArray = Encoding.Default.GetBytes(Text)
des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8))
des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8))
Dim ms As New System.IO.MemoryStream()
Dim cs As New CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write)
cs.Write(inputByteArray, 0, inputByteArray.Length)
cs.FlushFinalBlock()
Dim ret As New StringBuilder()
Dim b As Byte
For Each b In ms.ToArray()
ret.AppendFormat("{0:X2}", b)
Next
Return ret.ToString()
End Function
Public Shared Function Decrypt(ByVal Text As String) As String
Return Decrypt(Text, "12345678")
End Function
Public Shared Function Decrypt(ByVal Text As String, ByVal sKey As String) As String
Dim des As New DESCryptoServiceProvider()
Dim len As Integer
len = Text.Length / 2
Dim inputByteArray(len - 1) As Byte
Dim x, i As Integer
For x = 0 To len - 1
i = Convert.ToInt32(Text.Substring(x * 2, 2), 16)
inputByteArray(x) = CType(i, Byte)
Next
des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8))
des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8))
Dim ms As New System.IO.MemoryStream()
Dim cs As New CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write)
cs.Write(inputByteArray, 0, inputByteArray.Length)
cs.FlushFinalBlock()
Return Encoding.Default.GetString(ms.ToArray())
End Function
End Class
'以下是调用方法
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click '加密
Dim str_Encrypt As String = DESEncrypt.Encrypt("你要加密的文本,可以是任意长度", "密码,可以很长,如果省略这个参数就是默认的12345678")
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click '解密
Dim str_Decrypt As String = DESEncrypt.Decrypt("你要解密的文本, 可以是任意长度", "加密时用到的密码,如果省略这个参数就是默认的12345678")
End Sub
Public Function md5(ByVal a As String) As String
Dim tempmd5 As System.Security.Cryptography.MD5 = New System.Security.Cryptography.MD5CryptoServiceProvider()
Dim bytResult() As Byte = tempmd5.ComputeHash(System.Text.Encoding.Default.GetBytes(a))
Dim strResult As String = BitConverter.ToString(bytResult)
strResult = strResult.Replace("-", "")
Return strResult
End Function
如果要计算文件的就把参数改成字节数组就可以了,然后获取文件GetBytes()传进去就可以了。
这个是我之前写的。在需要时调用即可。
Public Shared Function Encrypt(ByVal Text As String, ByVal sKey As String) As String
Dim provider As New DESCryptoServiceProvider()
Dim bytes As Byte() = Encoding.[Default].GetBytes(Text)
provider.Key = Encoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8))
provider.IV = Encoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8))
Dim stream As New MemoryStream()
Dim stream2 As New CryptoStream(stream, provider.CreateEncryptor(), CryptoStreamMode.Write)
stream2.Write(bytes, 0, bytes.Length)
stream2.FlushFinalBlock()
Dim builder As New StringBuilder()
For Each num As Byte In stream.ToArray()
builder.AppendFormat("{0:X2}", num)
Next
Return builder.ToString()
End Function
希望能帮到你
Dim md5 As New MD5CryptoServiceProvider
Dim username As Byte() = (New ASCIIEncoding).GetBytes(TextBox1.Text)
'转换为哈希值Byte数组
Dim mdByte As Byte() = md5.ComputeHash(username)
'Dim mdString As String = System.BitConverter.ToString(mdByte)
Dim mdString As String = (New ASCIIEncoding).GetString(mdByte)
TextBox2.Text = mdString
md5理论上是不可破解的,要对照,只能再次加密后对照,网上也有些专门破解的网站,不过我想你要的不是那个吧