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. 一个带动画的页面底部的TabBar的实现

    有时有这样一个需求,页面底部有几个图标能够点击,假设一个screenWidth显示不下这些图标,则这一列图标最后一个是more,点击more,能够通过动画展示两列图标 这样来增加layout中: &l ...

  2. 集成学习---bagging and boosting

    作为集成学习的二个方法,其实bagging和boosting的实现比较容易理解,但是理论证明比较费力.下面首先介绍这两种方法. 所谓的集成学习,就是用多重或多个弱分类器结合为一个强分类器,从而达到提升 ...

  3. Java4Android之BlockingQueue

    在研究Smack的源码的时候,我对它的连接Connection以及派生类XMPPConnection的关注是最多的,由于一个即时通信程序,它的网络模块必是它的核心. 而我非常在乎它是怎样实现的. 在收 ...

  4. friend keyword 对于模板 并不只不过友元!!!

    friend是C++中封装的漏网之鱼. C++中的friend同意其它的类或者是函数訪问本类的不论什么成员.甚至是private成员,仅仅要该类声明其为友元. 但是,在有些情况下,并非同意外界訪问类的 ...

  5. Vector Clock理解

    背景近期在重读"Dynamo: Amazon's Highly Available Key-value Store"(经典好文,推荐!).文章4.4 中聊到了Data Versio ...

  6. 由于空间,注定的结果——第五届山东省ACM编程比赛总结

    应该是,这是一个很失败的结果.目前省赛玩具.作为志愿者说,,铁匠是一个很丢人的事. 作为队长.全然没有想到会是这种一次旅程.尽管由于去baidu的实习和各种offer的申请,对acm抱着能水就水绝不深 ...

  7. 远程连接到vultr vps的mysql服务器

    实验环境 vultr centos 6.7 x64 1. 首先要打开远程 vps的3306端口用于 mysql的连接 修改/etc/sysconfig/iptables 文件,添加3306端口的支持 ...

  8. 为Linux用ISO制作U盘启动及基本原理

    制作成功后的基本最简文件夹文件图 一.系统的基本引导流程: 首先系统要引导isolinux.bin可执行程序,此程序是移动介质上引导用的,isolinux.bin执行成功后会载入其配置文件syslin ...

  9. 【SICP归纳】2 高阶函数和数据抽象

    上一篇博文相应的是书中的第一章的一二两节,我们已经大致的有了一种构造的感觉不是么. 书中展示了非常多有趣的句法(syntax). 如今我们要让思想进一步的抽象.写这篇博客的时候并未学完整本书.更不敢说 ...

  10. Python编程预约参观北京行动纲要

    通过Python程序来模拟一个统一平台预约参观北京,包含验证码识别.登陆.据医院.时间.有关主管部门号等查询. 此程序仅供学习使用,请勿用于其他用途. 1.验证码图片 def getCodePic() ...