using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes; using System.Text;
using System.Windows.Media.Imaging;
namespace XguanjiaMsg
{
/// <summary>
/// MD5 32位加密
/// </summary>
public class MD5CryptoServiceProvider : MD5
{
public MD5CryptoServiceProvider()
: base()
{
}
}
/// <summary>
/// Summary description for MD5.
/// </summary>
public class MD5 : IDisposable
{
/// <summary>
/// Create 加密方法
/// </summary>
/// <param name="hashName"></param>
/// <returns></returns>
static public MD5 Create(string hashName)
{
if (hashName == "MD5")
return new MD5();
else
throw new NotSupportedException();
} static public String GetMd5String(String source)
{
MD5 md = MD5CryptoServiceProvider.Create();
byte[] hash; //Create a new instance of ASCIIEncoding to
//convert the string into an array of Unicode bytes.
UTF8Encoding enc = new UTF8Encoding();
// ASCIIEncoding enc = new ASCIIEncoding(); //Convert the string into an array of bytes.
byte[] buffer = enc.GetBytes(source); //Create the hash value from the array of bytes.
hash = md.ComputeHash(buffer); StringBuilder sb = new StringBuilder();
foreach (byte b in hash)
sb.Append(b.ToString("x2"));
return sb.ToString();
} static public MD5 Create()
{
return new MD5();
} #region base implementation of the MD5
#region constants
private const byte S11 = ;
private const byte S12 = ;
private const byte S13 = ;
private const byte S14 = ;
private const byte S21 = ;
private const byte S22 = ;
private const byte S23 = ;
private const byte S24 = ;
private const byte S31 = ;
private const byte S32 = ;
private const byte S33 = ;
private const byte S34 = ;
private const byte S41 = ;
private const byte S42 = ;
private const byte S43 = ;
private const byte S44 = ;
static private byte[] PADDING = new byte[] {
0x80, , , , , ,
, , , , , , , , , , , , , , , ,
, , , , , , ,
, , , , , , , , , , , , , , , ,
, , , , , , ,
, , , , , , , , , , ,
};
#endregion #region F, G, H and I are basic MD5 functions.
static private uint F(uint x, uint y, uint z)
{
return (((x) & (y)) | ((~x) & (z)));
}
static private uint G(uint x, uint y, uint z)
{
return (((x) & (z)) | ((y) & (~z)));
}
static private uint H(uint x, uint y, uint z)
{
return ((x) ^ (y) ^ (z));
}
static private uint I(uint x, uint y, uint z)
{
return ((y) ^ ((x) | (~z)));
}
#endregion #region rotates x left n bits.
/// <summary>
/// rotates x left n bits.
/// </summary>
/// <param name="x"></param>
/// <param name="n"></param>
/// <returns></returns>
static private uint ROTATE_LEFT(uint x, byte n)
{
return (((x) << (n)) | ((x) >> ( - (n))));
}
#endregion #region FF, GG, HH, and II transformations
/// FF, GG, HH, and II transformations
/// for rounds 1, 2, 3, and 4.
/// Rotation is separate from addition to prevent recomputation.
static private void FF(ref uint a, uint b, uint c, uint d, uint x, byte s, uint ac)
{
(a) += F((b), (c), (d)) + (x) + (uint)(ac);
(a) = ROTATE_LEFT((a), (s));
(a) += (b);
}
static private void GG(ref uint a, uint b, uint c, uint d, uint x, byte s, uint ac)
{
(a) += G((b), (c), (d)) + (x) + (uint)(ac);
(a) = ROTATE_LEFT((a), (s));
(a) += (b);
}
static private void HH(ref uint a, uint b, uint c, uint d, uint x, byte s, uint ac)
{
(a) += H((b), (c), (d)) + (x) + (uint)(ac);
(a) = ROTATE_LEFT((a), (s));
(a) += (b);
}
static private void II(ref uint a, uint b, uint c, uint d, uint x, byte s, uint ac)
{
(a) += I((b), (c), (d)) + (x) + (uint)(ac);
(a) = ROTATE_LEFT((a), (s));
(a) += (b);
}
#endregion #region context info
/// <summary>
/// state (ABCD)
/// </summary>
uint[] state = new uint[]; /// <summary>
/// number of bits, modulo 2^64 (lsb first)
/// </summary>
uint[] count = new uint[]; /// <summary>
/// input buffer
/// </summary>
byte[] buffer = new byte[];
#endregion internal MD5()
{
Initialize();
} /// <summary>
/// MD5 initialization. Begins an MD5 operation, writing a new context.
/// </summary>
/// <remarks>
/// The RFC named it "MD5Init"
/// </remarks>
public virtual void Initialize()
{
count[] = count[] = ; // Load magic initialization constants.
state[] = 0x67452301;
state[] = 0xefcdab89;
state[] = 0x98badcfe;
state[] = 0x10325476;
} /// <summary>
/// MD5 block update operation. Continues an MD5 message-digest
/// operation, processing another message block, and updating the
/// context.
/// </summary>
/// <param name="input"></param>
/// <param name="offset"></param>
/// <param name="count"></param>
/// <remarks>The RFC Named it MD5Update</remarks>
protected virtual void HashCore(byte[] input, int offset, int count)
{
int i;
int index;
int partLen; // Compute number of bytes mod 64
index = (int)((this.count[] >> ) & 0x3F); // Update number of bits
if ((this.count[] += (uint)((uint)count << )) < ((uint)count << ))
this.count[]++;
this.count[] += ((uint)count >> ); partLen = - index; // Transform as many times as possible.
if (count >= partLen)
{
Buffer.BlockCopy(input, offset, this.buffer, index, partLen);
Transform(this.buffer, ); for (i = partLen; i + < count; i += )
Transform(input, offset + i); index = ;
}
else
i = ; // Buffer remaining input
Buffer.BlockCopy(input, offset + i, this.buffer, index, count - i);
} /// <summary>
/// MD5 finalization. Ends an MD5 message-digest operation, writing the
/// the message digest and zeroizing the context.
/// </summary>
/// <returns>message digest</returns>
/// <remarks>The RFC named it MD5Final</remarks>
protected virtual byte[] HashFinal()
{
byte[] digest = new byte[];
byte[] bits = new byte[];
int index, padLen; // Save number of bits
Encode(bits, , this.count, , ); // Pad out to 56 mod 64.
index = (int)((uint)(this.count[] >> ) & 0x3f);
padLen = (index < ) ? ( - index) : ( - index);
HashCore(PADDING, , padLen); // Append length (before padding)
HashCore(bits, , ); // Store state in digest
Encode(digest, , state, , ); // Zeroize sensitive information.
count[] = count[] = ;
state[] = ;
state[] = ;
state[] = ;
state[] = ; // initialize again, to be ready to use
Initialize(); return digest;
} /// <summary>
/// MD5 basic transformation. Transforms state based on 64 bytes block.
/// </summary>
/// <param name="block"></param>
/// <param name="offset"></param>
private void Transform(byte[] block, int offset)
{
uint a = state[], b = state[], c = state[], d = state[];
uint[] x = new uint[];
Decode(x, , block, offset, ); // Round 1
FF(ref a, b, c, d, x[], S11, 0xd76aa478); /* 1 */
FF(ref d, a, b, c, x[], S12, 0xe8c7b756); /* 2 */
FF(ref c, d, a, b, x[], S13, 0x242070db); /* 3 */
FF(ref b, c, d, a, x[], S14, 0xc1bdceee); /* 4 */
FF(ref a, b, c, d, x[], S11, 0xf57c0faf); /* 5 */
FF(ref d, a, b, c, x[], S12, 0x4787c62a); /* 6 */
FF(ref c, d, a, b, x[], S13, 0xa8304613); /* 7 */
FF(ref b, c, d, a, x[], S14, 0xfd469501); /* 8 */
FF(ref a, b, c, d, x[], S11, 0x698098d8); /* 9 */
FF(ref d, a, b, c, x[], S12, 0x8b44f7af); /* 10 */
FF(ref c, d, a, b, x[], S13, 0xffff5bb1); /* 11 */
FF(ref b, c, d, a, x[], S14, 0x895cd7be); /* 12 */
FF(ref a, b, c, d, x[], S11, 0x6b901122); /* 13 */
FF(ref d, a, b, c, x[], S12, 0xfd987193); /* 14 */
FF(ref c, d, a, b, x[], S13, 0xa679438e); /* 15 */
FF(ref b, c, d, a, x[], S14, 0x49b40821); /* 16 */ // Round 2
GG(ref a, b, c, d, x[], S21, 0xf61e2562); /* 17 */
GG(ref d, a, b, c, x[], S22, 0xc040b340); /* 18 */
GG(ref c, d, a, b, x[], S23, 0x265e5a51); /* 19 */
GG(ref b, c, d, a, x[], S24, 0xe9b6c7aa); /* 20 */
GG(ref a, b, c, d, x[], S21, 0xd62f105d); /* 21 */
GG(ref d, a, b, c, x[], S22, 0x2441453); /* 22 */
GG(ref c, d, a, b, x[], S23, 0xd8a1e681); /* 23 */
GG(ref b, c, d, a, x[], S24, 0xe7d3fbc8); /* 24 */
GG(ref a, b, c, d, x[], S21, 0x21e1cde6); /* 25 */
GG(ref d, a, b, c, x[], S22, 0xc33707d6); /* 26 */
GG(ref c, d, a, b, x[], S23, 0xf4d50d87); /* 27 */
GG(ref b, c, d, a, x[], S24, 0x455a14ed); /* 28 */
GG(ref a, b, c, d, x[], S21, 0xa9e3e905); /* 29 */
GG(ref d, a, b, c, x[], S22, 0xfcefa3f8); /* 30 */
GG(ref c, d, a, b, x[], S23, 0x676f02d9); /* 31 */
GG(ref b, c, d, a, x[], S24, 0x8d2a4c8a); /* 32 */ // Round 3
HH(ref a, b, c, d, x[], S31, 0xfffa3942); /* 33 */
HH(ref d, a, b, c, x[], S32, 0x8771f681); /* 34 */
HH(ref c, d, a, b, x[], S33, 0x6d9d6122); /* 35 */
HH(ref b, c, d, a, x[], S34, 0xfde5380c); /* 36 */
HH(ref a, b, c, d, x[], S31, 0xa4beea44); /* 37 */
HH(ref d, a, b, c, x[], S32, 0x4bdecfa9); /* 38 */
HH(ref c, d, a, b, x[], S33, 0xf6bb4b60); /* 39 */
HH(ref b, c, d, a, x[], S34, 0xbebfbc70); /* 40 */
HH(ref a, b, c, d, x[], S31, 0x289b7ec6); /* 41 */
HH(ref d, a, b, c, x[], S32, 0xeaa127fa); /* 42 */
HH(ref c, d, a, b, x[], S33, 0xd4ef3085); /* 43 */
HH(ref b, c, d, a, x[], S34, 0x4881d05); /* 44 */
HH(ref a, b, c, d, x[], S31, 0xd9d4d039); /* 45 */
HH(ref d, a, b, c, x[], S32, 0xe6db99e5); /* 46 */
HH(ref c, d, a, b, x[], S33, 0x1fa27cf8); /* 47 */
HH(ref b, c, d, a, x[], S34, 0xc4ac5665); /* 48 */ // Round 4
II(ref a, b, c, d, x[], S41, 0xf4292244); /* 49 */
II(ref d, a, b, c, x[], S42, 0x432aff97); /* 50 */
II(ref c, d, a, b, x[], S43, 0xab9423a7); /* 51 */
II(ref b, c, d, a, x[], S44, 0xfc93a039); /* 52 */
II(ref a, b, c, d, x[], S41, 0x655b59c3); /* 53 */
II(ref d, a, b, c, x[], S42, 0x8f0ccc92); /* 54 */
II(ref c, d, a, b, x[], S43, 0xffeff47d); /* 55 */
II(ref b, c, d, a, x[], S44, 0x85845dd1); /* 56 */
II(ref a, b, c, d, x[], S41, 0x6fa87e4f); /* 57 */
II(ref d, a, b, c, x[], S42, 0xfe2ce6e0); /* 58 */
II(ref c, d, a, b, x[], S43, 0xa3014314); /* 59 */
II(ref b, c, d, a, x[], S44, 0x4e0811a1); /* 60 */
II(ref a, b, c, d, x[], S41, 0xf7537e82); /* 61 */
II(ref d, a, b, c, x[], S42, 0xbd3af235); /* 62 */
II(ref c, d, a, b, x[], S43, 0x2ad7d2bb); /* 63 */
II(ref b, c, d, a, x[], S44, 0xeb86d391); /* 64 */ state[] += a;
state[] += b;
state[] += c;
state[] += d; // Zeroize sensitive information.
for (int i = ; i < x.Length; i++)
x[i] = ;
} /// <summary>
/// Encodes input (uint) into output (byte). Assumes len is
/// multiple of 4.
/// </summary>
/// <param name="output"></param>
/// <param name="outputOffset"></param>
/// <param name="input"></param>
/// <param name="inputOffset"></param>
/// <param name="count"></param>
private static void Encode(byte[] output, int outputOffset, uint[] input, int inputOffset, int count)
{
int i, j;
int end = outputOffset + count;
for (i = inputOffset, j = outputOffset; j < end; i++, j += )
{
output[j] = (byte)(input[i] & 0xff);
output[j + ] = (byte)((input[i] >> ) & 0xff);
output[j + ] = (byte)((input[i] >> ) & 0xff);
output[j + ] = (byte)((input[i] >> ) & 0xff);
}
} /// <summary>
/// Decodes input (byte) into output (uint). Assumes len is
/// a multiple of 4.
/// </summary>
/// <param name="output"></param>
/// <param name="outputOffset"></param>
/// <param name="input"></param>
/// <param name="inputOffset"></param>
/// <param name="count"></param>
static private void Decode(uint[] output, int outputOffset, byte[] input, int inputOffset, int count)
{
int i, j;
int end = inputOffset + count;
for (i = outputOffset, j = inputOffset; j < end; i++, j += )
output[i] = ((uint)input[j]) | (((uint)input[j + ]) << ) | (((uint)input[j + ]) << ) | (((uint)input[j + ]) <<
);
}
#endregion #region expose the same interface as the regular MD5 object protected byte[] HashValue;
protected int State;
public virtual bool CanReuseTransform
{
get
{
return true;
}
} public virtual bool CanTransformMultipleBlocks
{
get
{
return true;
}
}
public virtual byte[] Hash
{
get
{
if (this.State != )
throw new InvalidOperationException();
return (byte[])HashValue.Clone();
}
}
public virtual int HashSize
{
get
{
return HashSizeValue;
}
}
protected int HashSizeValue = ; public virtual int InputBlockSize
{
get
{
return ;
}
}
public virtual int OutputBlockSize
{
get
{
return ;
}
} public void Clear()
{
Dispose(true);
} public byte[] ComputeHash(byte[] buffer)
{
return ComputeHash(buffer, , buffer.Length);
}
public byte[] ComputeHash(byte[] buffer, int offset, int count)
{
Initialize();
HashCore(buffer, offset, count);
HashValue = HashFinal();
return (byte[])HashValue.Clone();
} public byte[] ComputeHash(System.IO.Stream inputStream)
{
Initialize();
int count = ;
byte[] buffer = new byte[];
while ( < (count = inputStream.Read(buffer, , )))
{
HashCore(buffer, , count);
}
HashValue = HashFinal();
return (byte[])HashValue.Clone();
} public int TransformBlock(
byte[] inputBuffer,
int inputOffset,
int inputCount,
byte[] outputBuffer,
int outputOffset
)
{
if (inputBuffer == null)
{
throw new ArgumentNullException("inputBuffer");
}
if (inputOffset < )
{
throw new ArgumentOutOfRangeException("inputOffset");
}
if ((inputCount < ) || (inputCount > inputBuffer.Length))
{
throw new ArgumentException("inputCount");
}
if ((inputBuffer.Length - inputCount) < inputOffset)
{
throw new ArgumentOutOfRangeException("inputOffset");
}
if (this.State == )
{
Initialize();
this.State = ;
} HashCore(inputBuffer, inputOffset, inputCount);
if ((inputBuffer != outputBuffer) || (inputOffset != outputOffset))
{
Buffer.BlockCopy(inputBuffer, inputOffset, outputBuffer, outputOffset, inputCount);
}
return inputCount;
}
public byte[] TransformFinalBlock(
byte[] inputBuffer,
int inputOffset,
int inputCount
)
{
if (inputBuffer == null)
{
throw new ArgumentNullException("inputBuffer");
}
if (inputOffset < )
{
throw new ArgumentOutOfRangeException("inputOffset");
}
if ((inputCount < ) || (inputCount > inputBuffer.Length))
{
throw new ArgumentException("inputCount");
}
if ((inputBuffer.Length - inputCount) < inputOffset)
{
throw new ArgumentOutOfRangeException("inputOffset");
}
if (this.State == )
{
Initialize();
}
HashCore(inputBuffer, inputOffset, inputCount);
HashValue = HashFinal();
byte[] buffer = new byte[inputCount];
Buffer.BlockCopy(inputBuffer, inputOffset, buffer, , inputCount);
this.State = ;
return buffer;
}
#endregion protected virtual void Dispose(bool disposing)
{
if (!disposing)
Initialize();
}
public void Dispose()
{
Dispose(true);
}
}
}

[软件开发] WP8 MD5加密方法类,登陆之类所需

Windows Phone 8 MD5的更多相关文章

  1. Windows下计算md5值

    目录 Windows下计算md5值 1.linux 下计算md5值 2.Windows下计算md5值 Windows下计算md5值 1.linux 下计算md5值 [root@master yl]# ...

  2. Windows自带MD5 SHA1 SHA256命令行工具

    感恩大佬LiuYanYGZ的文章 MyHash 检验工具http://www.zdfans.com/html/4346.html HashMyFiles Hash校验工具http://www.nirs ...

  3. Windows 系统判断MD5 值的办法

    Linux 系统的文件要传到Windows系统里面,传输过程中网络不稳定,为了判断文件是否完整传输,所以就用md5的方式判断是否同一个文件 Linux系统 [root@augusite ~]# md5 ...

  4. WINDOWS自带md5校验工具

    WINDOWS自带的工具certutil.exe,   certutil -hashfile chropp.exe MD5; 就可以了

  5. 利用python计算windows全盘文件md5值的脚本

    import hashlib import os import time import configparser import uuid def test_file_md5(file_path): t ...

  6. Windows命令计算MD5与SHA1/256值

    certutil -hashfile file MD5 certutil -hashfile file SHA1 certutil -hashfile file SHA256 示例如下:

  7. 【windows】【md5】查看文件的md5值

    certutil -hashfile filename MD5 certutil -hashfile filename SHA1 certutil -hashfile filename SHA256 ...

  8. windows查看文件MD5值的命令

    今天需要,就记录一下. certutil -hashfile filename MD5 certutil -hashfile filename SHA1 certutil -hashfile file ...

  9. windows powershell校验下载的文件MD5和SHA1值

    Windows自带MD5 SHA1 SHA256命令行工具 certutil -hashfile <文件名> <hash类型> 打开windows powershell,进入到 ...

随机推荐

  1. 一个简单而经典的RTX51 Tiny应用实例

    关于RTX51 Tiny嵌入式实时操作系统的描写叙述请參考本人的上一篇博文(RTX51 Tiny实时操作系统学习笔记-初识RTX51 Tiny). 本篇博文.我将通过一个实例代码,带大家深入了解一下R ...

  2. LB 负载均衡的层次结构(转)

    作为后端应用的开发者,我们经常开发.调试.测试完我们的应用并发布到生产环境,用户就可以直接访问到我们的应用了.但对于互联网应用,在你的应用和用户之间还隔着一层低调的或厚或薄的负载均衡层软件,它们不显山 ...

  3. Android 上实现非root的 Traceroute -- 非Root权限下移植可运行二进制文件 脚本文件

    作者 : 万境绝尘 转载请著名出处 : http://blog.csdn.net/shulianghan/article/details/36438365 演示样例代码下载 : -- CSDN : h ...

  4. android uiautomator自己主动化測试

    前提是自己电脑上配置好JDK,android和ant的环境 1.命令行下进入\Android-sdk\tools\文件夹下,执行命令:    android list    查看相应android版本 ...

  5. Delphi F11 全屏

    unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...

  6. STL源代码分析——STL算法remove删除算法

    前言 因为在前文的<STL算法剖析>中,源代码剖析许多.不方便学习,也不方便以后复习,这里把这些算法进行归类.对他们单独的源代码剖析进行解说.本文介绍的STL算法中的remove删除算法. ...

  7. hdu3501

    要我们求小于n并且不与n互素的数字的和, 那么可以转化为1->(n-1)的和减去小于n且与n互素的数字的和 首先,有gcd(n,i)=1, 那么gcd(n,n-i)=1, 这是因为如果a%s=0 ...

  8. MySQL的create table as 与 like区别(转)

    对于mysql的复制相同表结构方法,有create table as 和create table like 两种,区别是什么呢? create table t2 as select * from t1 ...

  9. 什么是 CGI,什么是 IIS,什么是VPS

    该公司来到天.我们所从事的事情在网站上.这对我来说确实是一个很大的挑战.个人一直从事Android,对于web而一个开发网站server知识的几乎为零.在这里应该说,现在我只是有一个技术人员,昨天相遇 ...

  10. Hdu 5256 系列转换

    主题链接: HDU5236 代码: #include<iostream> #include<cstdio> #include<cstring> #include&l ...