我最近查阅了一些加密算法,写了一个SHA512的加密程序,但是我上网查阅了SHA512的散列码为512位,我的程序只得到了约160位,请朋友们帮我看看,是程序写错了吗?using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;namespace MD5Test
{
class Program
{
//MD5 16位加密
public static string md5Encode16(string source)
{
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
string t2 = BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(source)), 4, 8);
return t2;
}
//MD5 32位加密
public static string md5Encode32(string source)
{
string cl = source;
string pwd = "";
MD5 md5 = MD5.Create();//实例化一个md5对像
// 加密后是一个字节类型的数组,这里要注意编码UTF8/Unicode等的选择
byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(cl));
// 通过使用循环,将字节类型的数组转换为字符串,此字符串是常规字符格式化所得
for (int i = 0; i < s.Length; i++)
{
// 将得到的字符串使用十六进制类型格式化。'x'格式化后的字符是小写字母,如果使用大写'X',则格式后的字符是大写字符
pwd = pwd + s[i].ToString("X");
}
return pwd;
}
//SHA 512位加密
public static string sha512Encode(string source)
{
string result = "";
SHA512 sha512 = new SHA512Managed();
byte[] s = sha512.ComputeHash(Encoding.UTF8.GetBytes(source));
for (int i = 0; i < s.Length; i++)
{
result += s[i].ToString("X");
}
sha512.Clear();
return result;
}
static void Main(string[] args)
{
Console.WriteLine("Hello World! MD5 16 Encode: " + md5Encode16("Hello World!"));
Console.WriteLine("Hello World! MD5 32 Encode: " + md5Encode32("Hello World!"));
Console.WriteLine("Hello World! SHA 512 Encode: " + sha512Encode("Hello World!"));
}
}
}
/*
输出: Hello World! MD5 16 Encode: 53-2E-86-36-5E-84-1E-92
Hello World! MD5 32 Encode: ED76287532E86365E841E92BFC5D8C
Hello World! SHA 512 Encode: 861844D6704E8573FEC34D967E20BCFEF3D424CF48BE4E6DC8F2BD58C72974337115EAD891CC3CF1C9D34B49264B510751B1FF9E537937BC46B5D6FF4ECC8
*/
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;namespace MD5Test
{
class Program
{
//MD5 16位加密
public static string md5Encode16(string source)
{
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
string t2 = BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(source)), 4, 8);
return t2;
}
//MD5 32位加密
public static string md5Encode32(string source)
{
string cl = source;
string pwd = "";
MD5 md5 = MD5.Create();//实例化一个md5对像
// 加密后是一个字节类型的数组,这里要注意编码UTF8/Unicode等的选择
byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(cl));
// 通过使用循环,将字节类型的数组转换为字符串,此字符串是常规字符格式化所得
for (int i = 0; i < s.Length; i++)
{
// 将得到的字符串使用十六进制类型格式化。'x'格式化后的字符是小写字母,如果使用大写'X',则格式后的字符是大写字符
pwd = pwd + s[i].ToString("X");
}
return pwd;
}
//SHA 512位加密
public static string sha512Encode(string source)
{
string result = "";
SHA512 sha512 = new SHA512Managed();
byte[] s = sha512.ComputeHash(Encoding.UTF8.GetBytes(source));
for (int i = 0; i < s.Length; i++)
{
result += s[i].ToString("X");
}
sha512.Clear();
return result;
}
static void Main(string[] args)
{
Console.WriteLine("Hello World! MD5 16 Encode: " + md5Encode16("Hello World!"));
Console.WriteLine("Hello World! MD5 32 Encode: " + md5Encode32("Hello World!"));
Console.WriteLine("Hello World! SHA 512 Encode: " + sha512Encode("Hello World!"));
}
}
}
/*
输出: Hello World! MD5 16 Encode: 53-2E-86-36-5E-84-1E-92
Hello World! MD5 32 Encode: ED76287532E86365E841E92BFC5D8C
Hello World! SHA 512 Encode: 861844D6704E8573FEC34D967E20BCFEF3D424CF48BE4E6DC8F2BD58C72974337115EAD891CC3CF1C9D34B49264B510751B1FF9E537937BC46B5D6FF4ECC8
*/
//"X"是字符串的格式化,这里我用了以十六进制大写字母显示密文格式说明符 说明 示例 输出
C 货币 2.5.ToString("C") ¥2.50
D 十进制数 25.ToString("D5") 00025
E 科学型 25000.ToString("E") 2.500000E+005
F 固定点 25.ToString("F2") 25.00
G 常规 2.5.ToString("G") 2.5
N 数字 2500000.ToString("N") 2,500,000.00
X 十六进制 255.ToString("X") FF
md5的散列长度是128位,不清楚你的//MD5 32位加密 是指什么,难道你还能得到32byte[]的md5散列?
楼主得到的字符串是16进制表示的,字符数是126个,不正好是512个Bit吗
public static string sha512Encode(string source)
{
string result = "";
SHA512 sha512 = new SHA512Managed();
byte[] s = sha512.ComputeHash(Encoding.UTF8.GetBytes(source));
for (int i = 0; i < s.Length; i++)
{
result += s[i].ToString("X");
}
sha512.Clear();
return result;
}
string result = "";这是什么意思?初始化字符?
sha512.Clear();
这又是什么意思?清除缓存?
byte[] s = sha512.ComputeHash(Encoding.UTF8.GetBytes(source));
for (int i = 0; i < s.Length; i++)
{
result += s[i].ToString("X");
}
这又是什么意思?为什么一定要循环?