GUID与MD5加密

GUID可以生成一个不太容易重复的ID号;
MD5加密可以保护你的密码,加密过程认为是不可逆,把MD5码放在服务器数据库中,即使服务器被攻破,密码也不能被盗取。
第二个程序使用了ToString的格式化.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _32_GUID
{
    class Program
    {
        static void Main(string[] args)
        {
            //GUID is a static class ,it can help us to generate a distinguished ID;
            //we can use it as an product ID;
            Console.WriteLine(Guid.NewGuid().ToString());
            Console.ReadKey();
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace MD5加密
{
    /// <summary>
    /// MD5加密是不可逆的过程;
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("请输入要加密内容");
            string input = Console.ReadLine();
            Console.WriteLine("加密后的结果为:" + GetMD5(input));
            //x可以将十进制转换为16禁止;x2并不略去其中少的0;
            //Console.WriteLine("{0:X2}", 64);
            Console.ReadKey();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string GetMD5(string str)
        {
            //创建MD5对象;
            MD5 md5 = MD5.Create();
            //开始加密;
            //需要将字符串转换为字节数组,二进制的;
            byte[] buffer = Encoding.UTF8.GetBytes(str);

            byte[] MD5buffer = md5.ComputeHash(buffer);
            //将字节数组中每个元素按照编码格式解析成字符串;
            //string str2 = Encoding.UTF8.GetString(MD5buffer);
            //直接将数组ToString();
            //将字节数组中的每个元素ToString();  
            StringBuilder result=new StringBuilder();
            for (int i = 0; i < MD5buffer.Length; i++)
            {
                result.Append(MD5buffer[i].ToString("x2"));
            }

            return result.ToString();

        }
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值