📄 crypto.cs
字号:
//==========================================================================================
//
// OpenNETCF.Windows.Forms.Crypto
// Copyright (c) 2003, OpenNETCF.org
//
// This library is free software; you can redistribute it and/or modify it under
// the terms of the OpenNETCF.org Shared Source License.
//
// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the OpenNETCF.org Shared Source License
// for more details.
//
// You should have received a copy of the OpenNETCF.org Shared Source License
// along with this library; if not, email licensing@opennetcf.org to request a copy.
//
// If you wish to contact the OpenNETCF Advisory Board to discuss licensing, please
// email licensing@opennetcf.org.
//
// For general enquiries, email enquiries@opennetcf.org or visit our website at:
// http://www.opennetcf.org
//
// !!! A HUGE thank-you goes out to Casey Chesnut for supplying this class library !!!
// !!! You can contact Casey at http://www.brains-n-brawn.com !!!
//
//==========================================================================================
using System;
using System.Text;
using System.Runtime.InteropServices;
namespace OpenNETCF.Security.Cryptography.NativeMethods
{
[CLSCompliant(false)]
public class Crypto
{
public const string coredll = "coredll.dll";
public const string advapi32 = "advapi32.dll";
public const string crypt32 = "crypt32.dll";
///<summary>
///This function fills a buffer with random bytes. You can use this function when
///Cryptography Services features are not available on your platform.
///</summary>
/// <remarks>
/// worked on the SmartPhone
/// </remarks>
public static bool CeGenRandom(int dwLen, byte[] pbBuffer)
{
if(System.Environment.OSVersion.Platform == PlatformID.WinCE)
return CeGenRandomCe(dwLen, pbBuffer);
else
{
Random r = new Random(EnvironmentEx.TickCount);
r.NextBytes(pbBuffer);
return true;
}
}
//BOOL CeGenRandom(DWORD dwLen, BYTE* pbBuffer);
[DllImport(coredll, EntryPoint="CeGenRandom", SetLastError=true)]
private static extern bool CeGenRandomCe(int dwLen, byte[] pbBuffer);
///<summary>
///This function acquires a handle to the key container specified by the pszContainer
///parameter.
///</summary>
/// <remarks>
/// does not work on smartPhone, MissingMethodException
/// </remarks>
public static bool CPAcquireContext(out IntPtr phProv, StringBuilder pszContainer, uint dwFlags, byte[] pVTable)
{
if(System.Environment.OSVersion.Platform == PlatformID.WinCE)
return CPAcquireContextCe(out phProv, pszContainer, dwFlags, pVTable);
else
return CPAcquireContextXp(out phProv, pszContainer, dwFlags, pVTable);
}
//BOOL CPAcquireContext(HCRYPTPROV* phProv, WCHAR* pszContainer, DWORD dwFlags, PVTableProvStruc pVTable);
[DllImport(coredll, EntryPoint="CPAcquireContext", SetLastError=true)]
private static extern bool CPAcquireContextCe(out IntPtr phProv, StringBuilder pszContainer, uint dwFlags, byte[] pVTable);
[DllImport(advapi32, EntryPoint="CPAcquireContext", SetLastError=true)]
private static extern bool CPAcquireContextXp(out IntPtr phProv, StringBuilder pszContainer, uint dwFlags, byte[] pVTable);
///<summary>
///This function acquires a handle to a specific key container within a particular
///cryptographic service provider (CSP). This handle can be used to make calls to the
///selected CSP.
///</summary>
/// <remarks>
/// raCrypto, mca / works on smartPhone
/// </remarks>
public static bool CryptAcquireContext(out IntPtr hProv, string pszContainer, string pszProvider, uint dwProvType, uint dwFlags)
{
if(System.Environment.OSVersion.Platform == PlatformID.WinCE)
return CryptAcquireContextCe(out hProv, pszContainer, pszProvider, dwProvType, dwFlags);
else
return CryptAcquireContextXp(out hProv, pszContainer, pszProvider, dwProvType, dwFlags);
}
//1 0 0000ABCC CPAcquireContext
//BOOL WINAPI CryptAcquireContext(HCRYPTPROV* phProv, LPCTSTR pszContainer, LPCTSTR pszProvider, DWORD dwProvType, DWORD dwFlags);
[DllImport(coredll, EntryPoint="CryptAcquireContext", SetLastError=true)]
private static extern bool CryptAcquireContextCe(out IntPtr hProv, string pszContainer, string pszProvider, uint dwProvType, uint dwFlags);
[DllImport(advapi32, EntryPoint="CryptAcquireContext", SetLastError=true)]
private static extern bool CryptAcquireContextXp(out IntPtr hProv, string pszContainer, string pszProvider, uint dwProvType, uint dwFlags);
///<summary>
///This function adds one to the reference count of an HCRYPTPROV handle.
///</summary>
/// <remarks>
/// did not work on smartPhone, dont need it
/// </remarks>
public static bool CryptContextAddRef(IntPtr hProv, ref uint pdwReserved, uint dwFlags)
{
if(System.Environment.OSVersion.Platform == PlatformID.WinCE)
return CryptContextAddRefCe(hProv, ref pdwReserved, dwFlags);
else
return CryptContextAddRefXp(hProv, ref pdwReserved, dwFlags);
}
//BOOL WINAPI CryptContextAddRef(HCRYPTPROV hProv, DWORD* pdwReserved, DWORD dwFlags);
[DllImport(coredll, EntryPoint="CryptContextAddRef", SetLastError=true)]
private static extern bool CryptContextAddRefCe(IntPtr hProv, ref uint pdwReserved, uint dwFlags);
[DllImport(advapi32, EntryPoint="CryptContextAddRef", SetLastError=true)]
private static extern bool CryptContextAddRefXp(IntPtr hProv, ref uint pdwReserved, uint dwFlags);
///<summary>
///This function initiates the hashing of a stream of data. It creates and returns to
///the calling application a handle to a cryptographic service provider (CSP) hash
///object. This handle is used in subsequent calls to the CryptHashData function and
///CryptHashSessionKey function to hash streams of data and session keys.
///</summary>
/// <remarks>
/// raCrypto, mca / worked on smartPhone
/// </remarks>
public static bool CryptCreateHash(IntPtr hProv, uint Algid, IntPtr hKey, uint dwFlags, out IntPtr phHash)
{
if(System.Environment.OSVersion.Platform == PlatformID.WinCE)
return CryptCreateHashCe(hProv, Algid, hKey, dwFlags, out phHash);
else
return CryptCreateHashXp(hProv, Algid, hKey, dwFlags, out phHash);
}
//2 1 00004E78 CPCreateHash
//BOOL WINAPI CryptCreateHash(HCRYPTPROV hProv, ALG_ID Algid, HCRYPTKEY hKey, DWORD dwFlags, HCRYPTHASH* phHash);
[DllImport(coredll, EntryPoint="CryptCreateHash", SetLastError=true)]
private static extern bool CryptCreateHashCe(IntPtr hProv, uint Algid, IntPtr hKey, uint dwFlags, out IntPtr phHash);
[DllImport(advapi32, EntryPoint="CryptCreateHash", SetLastError=true)]
private static extern bool CryptCreateHashXp(IntPtr hProv, uint Algid, IntPtr hKey, uint dwFlags, out IntPtr phHash);
///<summary>
///This function decrypts data that was previously encrypted with the CryptEncrypt
///function.
///</summary>
/// <remarks>
/// raCrypto, mca / works on smartPhone
/// </remarks>
public static bool CryptDecrypt(IntPtr hKey, IntPtr hHash, bool Final, uint dwFlags, byte[] pbData, ref uint pdwDataLen)
{
if(System.Environment.OSVersion.Platform == PlatformID.WinCE)
return CryptDecryptCe(hKey, hHash, Final, dwFlags, pbData, ref pdwDataLen);
else
return CryptDecryptXp(hKey, hHash, Final, dwFlags, pbData, ref pdwDataLen);
}
//3 2 00004CC4 CPDecrypt
//BOOL CRYPTFUNC CryptDecrypt(HCRYPTKEY hKey, HCRYPTHASH hHash, BOOL Final, DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen);
[DllImport(coredll, EntryPoint="CryptDecrypt", SetLastError=true)]
private static extern bool CryptDecryptCe(IntPtr hKey, IntPtr hHash, bool Final, uint dwFlags, byte[] pbData, ref uint pdwDataLen);
[DllImport(advapi32, EntryPoint="CryptDecrypt", SetLastError=true)]
private static extern bool CryptDecryptXp(IntPtr hKey, IntPtr hHash, bool Final, uint dwFlags, byte[] pbData, ref uint pdwDataLen);
///<summary>
///This function generates cryptographic session keys derived from base data. This
///function guarantees that all keys generated from the same base data are identical,
///provided the same cryptographic service provider (CSP) and algorithms are used.
///The base data can be a password or any other user data.
///This function is the same as the CryptGenKey function, except that the generated
///session keys are derived from base data instead of being random. The CryptDeriveKey
///function can only generate session keys and cannot be used to generate
///public/private key pairs.
///A handle to the session key is returned in the phKey parameter. This handle can
///then be used with any CryptoAPI functions that require key handles.
///</summary>
/// <remarks>
/// raCrypto, mca / works on smartPhone
/// </remarks>
public static bool CryptDeriveKey(IntPtr hProv, uint Algid, IntPtr hBaseData, uint dwFlags, out IntPtr phKey)
{
if(System.Environment.OSVersion.Platform == PlatformID.WinCE)
return CryptDeriveKeyCe(hProv, Algid, hBaseData, dwFlags, out phKey);
else
return CryptDeriveKeyXp(hProv, Algid, hBaseData, dwFlags, out phKey);
}
//4 3 000066D0 CPDeriveKey
//BOOL CRYPTFUNC CryptDeriveKey(HCRYPTPROV hProv, ALG_ID Algid, HCRYPTHASH hBaseData, DWORD dwFlags, HCRYPTKEY *phKey);
[DllImport(coredll, EntryPoint="CryptDeriveKey", SetLastError=true)]
private static extern bool CryptDeriveKeyCe(IntPtr hProv, uint Algid, IntPtr hBaseData, uint dwFlags, out IntPtr phKey);
[DllImport(advapi32, EntryPoint="CryptDeriveKey", SetLastError=true)]
private static extern bool CryptDeriveKeyXp(IntPtr hProv, uint Algid, IntPtr hBaseData, uint dwFlags, out IntPtr phKey);
///<summary>
///This function destroys the hash object referenced by the hHash parameter. Once a
///hash object has been destroyed, it can no longer be used and its handle is useless
///from then on.
///All hash objects should be destroyed with the CryptDestroyHash function when the
///application is finished with them.
///</summary>
/// <remarks>
/// raCrypto / worked on smartPhone
/// </remarks>
public static bool CryptDestroyHash(IntPtr hHash)
{
if(System.Environment.OSVersion.Platform == PlatformID.WinCE)
return CryptDestroyHashCe(hHash);
else
return CryptDestroyHashXp(hHash);
}
//5 4 00005A90 CPDestroyHash
//BOOL CRYPTFUNC CryptDestroyHash(HCRYPTHASH hHash);
[DllImport(coredll, EntryPoint="CryptDestroyHash", SetLastError=true)]
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -