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. 抢车位中的排名bug(比較使用了无符号数)

    昨天把这个发在了qzone,想来还是怪怪的,还是转过来不吧,纯当发现了一个虫子,玩笑一下.只是csdn如今不能贴图,挺郁闷的,原文在http://user.qzone.qq.com/110907073 ...

  2. 如何让HTML在手机上实现直接拨打电话以及发送短信?

    拨打电话的HTML实现方式: <a href="tel:134289210xx″>拨打电话</a> 上面是比较常用的方式,但是有可能在某些场景下是支持不太好,可以试用 ...

  3. Multitasking Apps may only use background services for their intended purposes

    2.16 Details Your app declares support for audio in the UIBackgroundModes key in your Info.plist, bu ...

  4. [置顶] Codeforces Round #197 (Div. 2)(完全)

    http://codeforces.com/contest/339/ 这场正是水题大放送,在家晚上限制,赛后做了虚拟比赛 A,B 乱搞水题 C 我是贪心过的,枚举一下第一个拿的,然后选使差值最小的那个 ...

  5. 在Eclipse在使用JUnit4单元测试(0基础知识)

    自这篇文章: http://www.devx.com/Java/Article/31983/0/page/1 我们在编写大型程序的时候,须要写成千上万个方法或函数.这些函数的功能可能非常强大,但我们在 ...

  6. wireshark教程

    Wireshark世界上最流行的网络分析工具. 这个强大的工具能够捕捉网络中的数据,并为用户提供关于网络和上层协议的各种信息.与非常多其它网络工具一样.Wireshark也使用pcap network ...

  7. 在struts2中訪问servletAPI

    在struts2中訪问servletAPI,通俗点也就是使用servlet中的两个对象request对象和response对象. 前几天看到一个CRM项目的源代码,里面使用request对象和resp ...

  8. [TWRP 2.8.4] for 小米2S/2SC 支持中英文切换

    其中这个 twrp 2.8.4 在18号的下午已经编译好了. 经历了2个小时的代码修改,再经过后期的调试,中英文双语版本的twrp出炉了. 下面上几张图: 图片1:为英文界面 图片2: 图片3:中文界 ...

  9. [Erlang危机](5.1.0)VM检测概述

    原创文章.转载请注明出处:server非业余研究http://blog.csdn.net/erlib 作者Sunface 把执行指标储存在VM的内存中.能够指定是全局的还是app所特有的. • vms ...

  10. 打开 chm 帮助文件显示空白及解决方法

    有个很奇葩的解决方法:把 chm 文件用压缩软件压缩,然后用压缩软打开此压缩包,直接双击压缩包里面的 chm 文件 这虽然解决了问题,但是这不科学…… 分析:直接打开压缩包里面的文件,压缩包的文件是临 ...