1 Star 1 Fork 0

zyb518/winform_smalltools

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
frm_cryptography.cs 4.51 KB
一键复制 编辑 原始数据 按行查看 历史
zyb518 提交于 2016-12-11 20:49 . 20161211
using System;
using System.Text;
using System.Windows.Forms;
namespace Nathan_tools
{
public partial class frm_cryptography : Form
{
public frm_cryptography()
{
InitializeComponent();
}
private void frm_cryptography_Load(object sender, EventArgs e)
{
}
private void btn_file_md5_Click(object sender, EventArgs e)
{
if (tb_filename.Text == "")
MessageBox.Show("请输入文件名!","友情提示");
else
tb_filecrypt.Text = GetFileMd5Hash(tb_filename.Text);
}
public static string GetStrMd5Hash(string str) // //计算符串md5
{
StringBuilder sb = new StringBuilder();
foreach (byte b in System.Security.Cryptography.MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(str)))
{ sb.Append(b.ToString("X2")); }
return sb.ToString();
}
public static string GetFileMd5Hash(string pathName) //计算文件md5
{
string strResult = "";
string strHashData = "";
byte[] arrbytHashValue;
System.IO.FileStream oFileStream = null;
System.Security.Cryptography.MD5CryptoServiceProvider oMD5Hasher = new System.Security.Cryptography.MD5CryptoServiceProvider();
try
{
oFileStream = new System.IO.FileStream(pathName.Replace("\"", ""), System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite);
arrbytHashValue = oMD5Hasher.ComputeHash(oFileStream); //计算指定Stream 对象的哈希值
oFileStream.Close();
//由以连字符分隔的十六进制对构成的String,其中每一对表示value 中对应的元素;例如“F-2C-4A”
strHashData = System.BitConverter.ToString(arrbytHashValue);
//替换-
strHashData = strHashData.Replace("-", "");
strResult = strHashData;
}
catch (System.Exception ex)
{
MessageBox .Show (ex.Message);
}
return strResult;
}
public static string GetStrSHAHash(string str) // //计算符串md5
{
StringBuilder sb = new StringBuilder();
foreach (byte b in System.Security.Cryptography.SHA1.Create().ComputeHash(Encoding.UTF8.GetBytes(str)))
{ sb.Append(b.ToString("X2")); }
return sb.ToString();
}
public static string GetFileShaHash(string pathName)
{
string strResult = "";
string strHashData = "";
byte[] arrbytHashValue;
System.IO.FileStream oFileStream = null;
System.Security.Cryptography.SHA1CryptoServiceProvider SHAHasher = new System.Security.Cryptography.SHA1CryptoServiceProvider();
try
{
oFileStream = new System.IO.FileStream(pathName.Replace("\"", ""), System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite);
arrbytHashValue = SHAHasher.ComputeHash(oFileStream); //计算指定Stream 对象的哈希值
oFileStream.Close();
//由以连字符分隔的十六进制对构成的String,其中每一对表示value 中对应的元素;例如“F-2C-4A”
strHashData = System.BitConverter.ToString(arrbytHashValue);
//替换-
strHashData = strHashData.Replace("-", "");
strResult = strHashData;
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
return strResult;
}
private void btn_openfile_Click(object sender, EventArgs e)
{
if (DialogResult.OK == openFileDialog1.ShowDialog())
{ tb_filename.Text = openFileDialog1.FileName; }
}
private void btn_str_md5_Click(object sender, EventArgs e)
{
tb_crypt_str.Text = GetStrMd5Hash(tb_str.Text);
}
private void btn_file_hash_Click(object sender, EventArgs e)
{
if (tb_filename.Text == "")
MessageBox.Show("请输入文件名!", "友情提示");
else
tb_filecrypt.Text = GetFileShaHash(tb_filename.Text);
}
private void btn_str_hash_Click(object sender, EventArgs e)
{
tb_crypt_str.Text = GetStrSHAHash(tb_str.Text);
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C#
1
https://gitee.com/zyb518/winform_smalltools.git
[email protected]:zyb518/winform_smalltools.git
zyb518
winform_smalltools
winform_smalltools
master

搜索帮助