Implementation of MD5CryptoServiceProvider in the. NET Compact flamework

Source: Internet
Author: User
Tags bool compact dot net md5
The encryption classes under the System.Security.Cryptography namespace are well supported in the. NET Flamework full edition. However, the corresponding class in the namespace is not provided in the compact version. The encryption algorithm used when writing Pocket PC programs in. NET is really troublesome. There are generally two ways to solve this problem. OpenNETCF (www.openetcf.org) provides simulations of various classes under the System.Security.Cryptography namespace, but it provides a lack of flexibility: for example, when symmetric encryption, users cannot set padding, Ciphermode and other attributes. And it's provided in a way that is inconsistent with the interfaces of classes under the full version of. NET, creating confusion for users. Another way is to do their own encapsulation of CryptoAPI.

MD5CryptoServiceProvider has recently been implemented for work, and the interface of this class is exactly the same as the interface below the full version.

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)



throw new ObjectDisposedException (this. GetType (). FullName);



byte[] copy = (byte[]) array. Clone ();



Array.copy (Array, Ibstart, Copy, 0, cbsize);



BOOL Retval=false;



Retval=crypto.crypthashdata (m_hash, copy, copy.) Length, 0);



}



protected override byte[] Hashfinal ()



{



if (m_disposed)



throw new ObjectDisposedException (this. GetType (). FullName);



byte [] data = new Byte[0];



UINT Datalen = 0;



UINT flags = 0;



Size



BOOL RetVal = Crypto.cryptgethashparam (M_hash, (UINT) hashparam.hashval, data, ref datalen, flags);



if (234 = = Marshal.GetLastWin32Error ())//more_data = 234,



{



Data



data = new Byte[datalen];



RetVal = Crypto.cryptgethashparam (M_hash, (UINT) hashparam.hashval, data, ref datalen, flags);



}



return data;



}



protected override void Dispose (bool disposing)



{



if (!m_disposed)



{



if (M_hash!= IntPtr.Zero)



{



BOOL Retval=crypto.cryptdestroyhash (M_hash);



M_hash = IntPtr.Zero;



}



if (M_prov!=intptr.zero)



{



Crypto.cryptreleasecontext (M_prov, 0);



M_prov=intptr.zero;



}



Try



{



Gc. SuppressFinalize (this);



}



Catch {}



M_disposed = true;



}



}



~md5cryptoserviceprovider ()



{



Clear ();



}



Private INTPTR M_hash=intptr.zero;



private bool m_disposed;



Private INTPTR M_prov=intptr.zero;



}







Public abstract class Md5:hashalgorithm



{



constructor.



Protected MD5 ()



{



Hashsizevalue = 128;



}







Create a new instance of the "MD5" class.



Public new static MD5 Create ()



{



Return (MD5) (cryptoconfig.createfromname



(Cryptoconfig.md5default, null));



}



Public new static MD5 Create (String algname)



{



Return (MD5) (Cryptoconfig.createfromname (algname, null));



}







}; Class MD5







P/invoke the Cryotoapi



public class Crypto



{



[DllImport ("Coredll.dll", entrypoint= "CryptAcquireContext")]



public static extern bool CryptAcquireContext (out IntPtr Hprov, String Pszcontainer, String pszprovider, uint Dwprovtype, UINT dwflags);







[DllImport ("Coredll.dll", entrypoint= "Cryptcreatehash")]



public static extern bool Cryptcreatehash (IntPtr Hprov, uint algid, IntPtr hkey, uint dwflags, out IntPtr Phhash);







[DllImport ("Coredll.dll", entrypoint= "Cryptdestroyhash")]



public static extern bool Cryptdestroyhash (IntPtr Hhash);







[DllImport ("Coredll.dll", entrypoint= "Crypthashdata")]



public static extern bool Crypthashdata (IntPtr Hhash, byte[] pbdata, int dwdatalen, uint dwflags);







[DllImport ("Coredll.dll", entrypoint= "Cryptgethashparam", Setlasterror=true)]



public static extern bool Cryptgethashparam (IntPtr Hhash, uint dwparam, byte[] pbdata, ref uint Pdwdatalen, uint dwflags);







[DllImport ("Coredll.dll", entrypoint= "CryptReleaseContext")]



public static extern bool CryptReleaseContext (IntPtr Hprov, uint dwflags);







public static INTPTR Acquirecontext ()



{



Return Acquirecontext ("Md5container", Provname.ms_enhanced_prov, Provtype.rsa_full, Contextflag.none);



}







public static INTPTR Acquirecontext (String container)



{



return Acquirecontext (Container, Provname.ms_enhanced_prov, Provtype.rsa_full, Contextflag.none);



}







public static IntPtr Acquirecontext (Provtype provtype)



{



return acquirecontext (NULL, NULL, Provtype, contextflag.none);



}







public static IntPtr Acquirecontext (String provname, Provtype provtype)



{



return Acquirecontext (null, Provname, Provtype, Contextflag.none);



}







public static IntPtr Acquirecontext (String provname, Provtype provtype, Contextflag Conflag)



{



return Acquirecontext (null, Provname, Provtype, Conflag);



}







public static IntPtr Acquirecontext (String conName, String provname, Provtype provtype)



{



Return Acquirecontext (ConName, Provname, Provtype, Contextflag.none);



}







public static IntPtr Acquirecontext (String conName, String provname, Provtype provtype, Contextflag Conflag)



{



INTPTR Hprov;



BOOL RetVal = Crypto.cryptacquirecontext (out Hprov, ConName, Provname, (UINT) Provtype, (UINT) conflag);



if (!retval)//try Creating a new key container



{



RetVal = Crypto.cryptacquirecontext (out Hprov, ConName, Provname, (UINT) Provtype, (UINT) contextflag.newkeyset);



}



if (Hprov = = IntPtr.Zero)



throw new Exception ("System.Security.Cryptography");



return Hprov;



}



}


Code Downloads on CodeProject: http://www.codeproject.com/useritems/MD5CryptoServiceProvider.asp

Reference:

Dot NET Compact Framework Kick Start 2003

www.opennetcf.org

Http://www.koders.com/csharp/fidC21861B5F1B717EC1FDEC006DBD0B8226B92D878.aspx








Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.