在.net Flamework完全版中對System.Security.Cryptography名字空間下的加密類支援得很好。但在精簡版中卻沒有提供這個名字空間中的相應的類。在用.net寫Pocket PC程式的時候用的密碼編譯演算法的時候確實比較麻煩。一般有兩種方法來解決這個問題。OpenNetCF(www.openetcf.org)提供了System.Security.Cryptography名字空間下的各個類的類比,但它的提供缺乏靈活性:比如在對稱式加密的時候,使用者無法設定Padding, CipherMode等屬性。而且它的提供方式和完全版的.net下的類的介面不一致,這樣給使用者造成困惑。另一種方法就是自己動手對cryptoAPI進行自己封裝。
最近出於工作需要對MD5CryptoServiceProvider進行了實現,這個類的介面和完整版下面的介面完全一致。
Implementation of the "System.Security.Cryptography.MD5CryptoServiceProvider" class.
public sealed class MD5CryptoServiceProvider : MD5
{
public MD5CryptoServiceProvider()
{
Initialize();
m_Disposed = false;
}
public override void Initialize()
{
if (m_Disposed)
throw new ObjectDisposedException(this.GetType().FullName);
if (m_Hash != IntPtr.Zero)
{
Crypto.CryptDestroyHash(m_Hash);
}
m_Prov = Crypto.AcquireContext(ProvType.RSA_FULL);
bool retVal=Crypto.CryptCreateHash(m_Prov, (uint)CalgHash.MD5, IntPtr.Zero, 0, out m_Hash);
}
protected override void HashCore(byte[] array, int ibStart, int cbSize)
{
if (m_Disposed)