/*
makecert.exe -n "CN=Microshaoft X509 Test - A" -sky exchange -pe -sv a.pvk a.cer
pvk2pfx.exe -pvk a.pvk -spc a.cer -pfx a.pfx -f -po 123
makecert.exe -n "CN=Microshaoft X509 Test - B" -sky exchange -pe -sv b.pvk b.cer
pvk2pfx.exe -pvk b.pvk -spc b.cer -pfx b.pfx -f -po abc
*/
namespace Test
{
using System;
using System.IO;
using System.Text;
using System.Threading;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using Microshaoft;
class Class1
{
static void Main()
{
//=======================================
UTF8Encoding e = new UTF8Encoding();
string s;
byte[] data = e.GetBytes
(
@"测试@microshaoft.com测试@microshaoft.com测试@microshaoft.com
测试@microshaoft.com测试@microshaoft.com测试@microshaoft.com");
byte[] encryptedData;
// Hybird
Console.WriteLine("Hybird(X509+3DES):=====================");
X509Certificate2 cerA = new X509Certificate2(@"C:\Users\xiyueyu\Desktop\a.cer");
X509Certificate2 pfxA = new X509Certificate2(@"C:\Users\xiyueyu\Desktop\a.pfx", "123");
X509Certificate2 cerB = new X509Certificate2(@"C:\Users\xiyueyu\Desktop\b.cer");
Secret secret = CryptographyHelper.HybridEncrypt
(
pfxA
, cerA
, cerB
, HashSignatureMode.SHA1
, false
, data
);
X509Certificate2 pfxB = new X509Certificate2(@"C:\Users\xiyueyu\Desktop\b.pfx", "abc");
data = CryptographyHelper.HybridDecrypt(pfxB, secret);
s = e.GetString(data);
Console.WriteLine("Hybird decrypted plain text:");
Console.WriteLine(s);
//100 字节以内
s = "测试@microshaoft.com测试@microshaoft.com测试@microshaoft.com测试";
data = e.GetBytes(s);
// X509
Console.WriteLine("\nX509 加解密 只能处理100字节以内的加解密:=====================");
X509Certificate2[] certs = X509CertificateHelper.LoadCertificatesFromStore
(
StoreName.My
, StoreLocation.CurrentUser
, "Microshaoft X509 Test"
, X509FindType.FindBySubjectName
);
X509Certificate2 cert = null;
if (certs != null)
{
foreach (X509Certificate2 c in certs)
{
Console.WriteLine(c.Subject);
cert = c;
}
}
X509Certificate2 cer = new X509Certificate2(@"C:\Users\xiyueyu\Desktop\a.cer");
X509Certificate2 pfx = new X509Certificate2(@"C:\Users\xiyueyu\Desktop\a.pfx", "123");
encryptedData = CryptographyHelper.X509CertificateEncrypt(cer, data, false);
encryptedData = CryptographyHelper.X509CertificateDecrypt(pfx, encryptedData, false);
s = e.GetString(encryptedData);
Console.WriteLine("X509 decrypted plain text: {0}", s);
//RSA
Console.WriteLine("\nRSA 加解密 只能处理100字节以内的加解密:=====================");
// RSA 非证书 只能处理100字节以内的加解密
s = "测试@microshaoft.com测试@microshaoft.com测试@microshaoft.com测试";
data = e.GetBytes(s);
RSACryptoServiceProvider x = new RSACryptoServiceProvider();
string privateKey = x.ToXmlString(true);
Console.WriteLine("RSA Private Key: {0}", privateKey);
string publicKey = x.ToXmlString(false);
Console.WriteLine("RSA Public Key: {0}", publicKey);
//公钥加密
encryptedData = CryptographyHelper.RSAEncrypt(publicKey, data, false);
//私钥解密
data = CryptographyHelper.RSADecrypt(privateKey, encryptedData, false);
Console.WriteLine("RSA Decrypted plaintext: {0}", e.GetString(data));
//私钥签名
byte[] signature = CryptographyHelper.RSASignSHA1(privateKey, data);
//公钥验签
Console.WriteLine(CryptographyHelper.RSAVerifySHA1(publicKey, data, signature));
//TripleDES
Console.WriteLine("\nTripleDES:===============");
string key = "000111222333444555666777888999aaabbbcccdddeeefff";//48
key = "0123456789abcdef01111111111111111111111111111110";
string iv = "0123456789abcdef";//16
iv = "0000000000000000";
data = e.GetBytes("测试@microshaoft.com");
SymmetricAlgorithm symmetricAlgorithm = new TripleDESCryptoServiceProvider();
symmetricAlgorithm.Key = CryptographyHelper.HexStringToBytesArray(key);
symmetricAlgorithm.IV = CryptographyHelper.HexStringToBytesArray(iv);
data = CryptographyHelper.SymmetricAlgorithmEncrypt
(
symmetricAlgorithm
, data
);
//"测试@microshaoft.com"
//3des: "A668632B4AA1EB8C1B25F7E66128846D28CEB416AC7B7B21"
//篡改: "B668632B4AA1EB8C1B25F7E66128846D28CEB416AC7B7B21"
data = CryptographyHelper.SymmetricAlgorithmDecrypt
(
symmetricAlgorithm,
data
);
Console.WriteLine("3DES Decrypted plaintext: {0}", e.GetString(data));
Console.WriteLine("X509 并发测试");
Console.ReadLine();
encryptorPrivateKeyPfxProvider = encryptorPrivateKeyPfx.PrivateKey as RSACryptoServiceProvider;
encryptorPublicKeyCerProvider = encryptorPublicKeyCer.PublicKey.Key as RSACryptoServiceProvider;
decryptorPublicKeyCerProvider = decryptorPublicKeyCer.PublicKey.Key as RSACryptoServiceProvider;
decryptorPrivateKeyPfxProvider = decryptorPrivateKeyPfx.PrivateKey as RSACryptoServiceProvider;
for (int i = 0; i < 1000; i++)
{
ThreadStart ts = new ThreadStart(Run);
Thread t = new Thread(ts);
t.Name = _ThreadID.ToString();
_ThreadID++;
t.Start();
//Run();
}
Console.WriteLine(Environment.Version.ToString());
}
private static volatile int _ThreadID = 0;
private static object _syncLockObject = new object();
private static X509Certificate2 encryptorPrivateKeyPfx = new X509Certificate2(@"a.pfx", "123");
private static X509Certificate2 encryptorPublicKeyCer = new X509Certificate2(@"a.cer");
private static X509Certificate2 decryptorPublicKeyCer = new X509Certificate2(@"b.cer");
private static X509Certificate2 decryptorPrivateKeyPfx = new X509Certificate2(@"b.pfx", "abc");
private static RSACryptoServiceProvider encryptorPrivateKeyPfxProvider = null;
private static RSACryptoServiceProvider encryptorPublicKeyCerProvider = null;
private static RSACryptoServiceProvider decryptorPublicKeyCerProvider = null;
private static RSACryptoServiceProvider decryptorPrivateKeyPfxProvider = null;
static void Run()
{
/// X509Certificate2 cerA = null;// = new X509Certificate2(@"C:\Users\xiyueyu\Desktop\a.cer");
/// X509Certificate2 pfxA = null;// = new X509Certificate2(@"C:\Users\xiyueyu\Desktop\a.pfx", "123");
/// X509Certificate2 cerB = null;// = new X509Certificate2(@"C:\Users\xiyueyu\Desktop\b.cer");
/// X509Certificate2 pfxB = null;// = new X509Certificate2(@"C:\Users\xiyueyu\Desktop\b.pfx", "abc");
//lock (_syncLockObject)
{
try
{
//Thread.Sleep(50);
/// cerA = new X509Certificate2(@"C:\Users\xiyueyu\Desktop\a.cer");
/// pfxA = new X509Certificate2(@"C:\Users\xiyueyu\Desktop\a.pfx", "123");
/// cerB = new X509Certificate2(@"C:\Users\xiyueyu\Desktop\b.cer");
/// pfxB = new X509Certificate2(@"C:\Users\xiyueyu\Desktop\b.pfx", "abc");
UTF8Encoding e = new UTF8Encoding();
string s;
byte[] data = e.GetBytes
(
@"并发测试@microshaoft.com并发测试@microshaoft.com并发测试@microshaoft.com
并发测试@microshaoft.com并发测试@microshaoft.com并发测试@microshaoft.com
并发测试@microshaoft.com并发测试@microshaoft.com并发测试@microshaoft.com
并发测试@microshaoft.com并发测试@microshaoft.com并发测试@microshaoft.com
并发测试@microshaoft.com并发测试@microshaoft.com并发测试@microshaoft.com
并发测试@microshaoft.com并发测试@microshaoft.com并发测试@microshaoft.com
并发测试@microshaoft.com并发测试@microshaoft.com并发测试@microshaoft.com");
//byte[] encryptedData;
// Hybird
//Console.WriteLine("Hybird(X509+3DES):=====================");
Secret secret = CryptographyHelper.HybridEncrypt
(
encryptorPrivateKeyPfxProvider
, encryptorPublicKeyCer
, decryptorPublicKeyCerProvider
, HashSignatureMode.SHA1
, false
, data
);
data = CryptographyHelper.HybridDecrypt
(
decryptorPrivateKeyPfxProvider
, encryptorPublicKeyCerProvider
, secret
);
s = e.GetString(data);
Console.WriteLine("Hybird decrypted plain text: {0}", Thread.CurrentThread.Name);
Console.WriteLine(s);
}
catch (Exception e)
{
// Thread.Sleep(1200);
Console.WriteLine("exception sleep: {0}", e.ToString());
Console.WriteLine("exception sleep: {0}", Thread.CurrentThread.Name);
// Run();
}
finally
{
/// cerA.Reset();
/// pfxA.Reset();
/// cerB.Reset();
/// cerB.Reset();
}
}
}
}
}
namespace Microshaoft
{
using System;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
public static class X509CertificateHelper
{
public static X509Certificate2[] LoadCertificatesFromStore
(
StoreName storeName
, StoreLocation storeLocation
, string findValue
, X509FindType findType
)
{
X509Certificate2[] certs = null;
X509Store store = new X509Store(storeName, storeLocation);
store.Open(OpenFlags.ReadOnly);
try
{
X509Certificate2Collection matches = store.Certificates.Find(findType, findValue, false);
certs = new X509Certificate2[matches.Count];
matches.CopyTo(certs, 0);
}
finally
{
store.Close();
}
return certs;
}
}
}
namespace Microshaoft
{
using System;
using System.IO;
using System.Text;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
public class Secret
{
public byte[] EncryptorSharedEncryptedOnceKey;
public byte[] EncryptorSharedEncryptedOnceIV;
public byte[] EncryptorHashSignature;
public byte[] EncryptorPublicKeyCerRawData;
public byte[] EncryptedData;
public HashSignatureMode SignHashMode;
public bool DoOAEPadding;
}
public enum HashSignatureMode
{
MD5
, SHA1
}
public static class CryptographyHelper
{
public static byte[] HybridDecrypt
(
X509Certificate2 decryptorPrivateKeyPfx
, Secret data
)
{
X509Certificate2 encryptorPublicKeyCer = null;
try
{
RSACryptoServiceProvider decryptorPrivateKeyPfxProvider = decryptorPrivateKeyPfx.PrivateKey as RSACryptoServiceProvider;
encryptorPublicKeyCer = new X509Certificate2(data.EncryptorPublicKeyCerRawData);
RSACryptoServiceProvider encryptorPublicKeyCerProvider = encryptorPublicKeyCer.PublicKey.Key as RSACryptoServiceProvider;
return HybridDecrypt
(
decryptorPrivateKeyPfxProvider
, encryptorPublicKeyCerProvider
, data
);
}
catch
{
return null;
}
finally
{
if (encryptorPublicKeyCer != null)
{
encryptorPublicKeyCer.Reset();
}
}
}
public static byte[] HybridDecrypt
(
RSACryptoServiceProvider decryptorPrivateKeyPfxProvider
, RSACryptoServiceProvider encryptorPublicKeyCerProvider
, Secret data
)
{
byte[] buffer = null;
HashAlgorithm hashAlgorithm;
if (data.SignHashMode == HashSignatureMode.SHA1)
{
hashAlgorithm = new SHA1CryptoServiceProvider();
}
else //(hashSignatureMode == HashSignatureMode.MD5)
{
hashAlgorithm = new MD5CryptoServiceProvider();
}
using (MemoryStream stream = new MemoryStream())
{
buffer = data.EncryptorSharedEncryptedOnceIV;
stream.Write(buffer, 0, buffer.Length);
buffer = data.EncryptorSharedEncryptedOnceKey;
stream.Write(buffer, 0, buffer.Length);
buffer = data.EncryptedData;
stream.Position = 0;
buffer = hashAlgorithm.ComputeHash(stream);
stream.Close();
}
//X509Certificate2 encryptorPublicKeyCer = new X509Certificate2(data.EncryptorPublicKeyCerRawData);
//RSACryptoServiceProvider encryptorPublicKeyCerProvider = encryptorPublicKeyCer.PublicKey.Key as RSACryptoServiceProvider;
if (encryptorPublicKeyCerProvider.VerifyHash
(
buffer
, Enum.GetName
(
data.SignHashMode.GetType()
, data.SignHashMode
)
, data.EncryptorHashSignature
)
)
{
//decryptorPrivateKeyPfxProvider = decryptorPrivateKeyPfx.PrivateKey as RSACryptoServiceProvider;
using (TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider())
{
buffer = data.EncryptorSharedEncryptedOnceIV;
buffer = decryptorPrivateKeyPfxProvider.Decrypt(buffer, data.DoOAEPadding);
des.IV = buffer;
buffer = data.EncryptorSharedEncryptedOnceKey;
buffer = decryptorPrivateKeyPfxProvider.Decrypt(buffer, data.DoOAEPadding);
des.Key = buffer;
buffer = data.EncryptedData;
buffer = des.CreateDecryptor().TransformFinalBlock(buffer, 0, buffer.Length);
}
}
else
{
buffer = null;
}
return buffer;
}
public static Secret HybridEncrypt
(
byte[] encryptorPrivateKeyPfxRawData
, byte[] encryptorPublicKeyCerRawData
, byte[] decryptorPublicKeyCerRawData
, HashSignatureMode hashSignatureMode
, bool DoOAEPadding
, byte[] data
)
{
X509Certificate2 encryptorPrivateKeyPfx = null;
X509Certificate2 encryptorPublicKeyCer = null;
X509Certificate2 decryptorPublicKeyCer = null;
try
{
encryptorPrivateKeyPfx = null;
encryptorPublicKeyCer = null;
decryptorPublicKeyCer = null;
return HybridEncrypt
(
encryptorPrivateKeyPfx
, encryptorPublicKeyCer
, decryptorPublicKeyCer
, hashSignatureMode
, DoOAEPadding
, data
);
}
catch
{
return null;
}
finally
{
if (encryptorPrivateKeyPfx != null)
{
encryptorPrivateKeyPfx.Reset();
}
if (encryptorPublicKeyCer != null)
{
encryptorPublicKeyCer.Reset();
}
if (decryptorPublicKeyCer != null)
{
decryptorPublicKeyCer.Reset();
}
}
}
public static Secret HybridEncrypt
(
string encryptorPrivateKeyPfxFileName
, string encryptorPublicKeyCerFileName
, string decryptorPublicKeyCerFileName
, HashSignatureMode hashSignatureMode
, bool DoOAEPadding
, byte[] data
)
{
X509Certificate2 encryptorPrivateKeyPfx = null;
X509Certificate2 encryptorPublicKeyCer = null;
X509Certificate2 decryptorPublicKeyCer = null;
try
{
encryptorPrivateKeyPfx = new X509Certificate2(encryptorPrivateKeyPfxFileName);
encryptorPublicKeyCer = new X509Certificate2(encryptorPublicKeyCerFileName);
decryptorPublicKeyCer = new X509Certificate2(decryptorPublicKeyCerFileName);
return HybridEncrypt
(
encryptorPrivateKeyPfx
, encryptorPublicKeyCer
, decryptorPublicKeyCer
, hashSignatureMode
, DoOAEPadding
, data
);
}
catch
{
return null;
}
finally
{
if (encryptorPrivateKeyPfx != null)
{
encryptorPrivateKeyPfx.Reset();
}
if (encryptorPublicKeyCer != null)
{
encryptorPublicKeyCer.Reset();
}
if (decryptorPublicKeyCer != null)
{
decryptorPublicKeyCer.Reset();
}
}
}
public static Secret HybridEncrypt
(
X509Certificate2 encryptorPrivateKeyPfx
, X509Certificate2 encryptorPublicKeyCer
, X509Certificate2 decryptorPublicKeyCer
, HashSignatureMode signHashMode
, bool DoOAEPadding
, byte[] data
)
{
RSACryptoServiceProvider encryptorPrivateKeyPfxProvider = encryptorPrivateKeyPfx.PrivateKey as RSACryptoServiceProvider;
RSACryptoServiceProvider decryptorPublicKeyCerProvider = decryptorPublicKeyCer.PublicKey.Key as RSACryptoServiceProvider;
return HybridEncrypt
(
encryptorPrivateKeyPfxProvider
, encryptorPublicKeyCer
, decryptorPublicKeyCerProvider
, signHashMode
, DoOAEPadding
, data
);
}
public static Secret HybridEncrypt
(
RSACryptoServiceProvider encryptorPrivateKeyPfxProvider
, X509Certificate2 encryptorPublicKeyCer
, RSACryptoServiceProvider decryptorPublicKeyCerProvider
, HashSignatureMode signHashMode
, bool DoOAEPadding
, byte[] data
)
{
Secret secret = new Secret();
using (TripleDESCryptoServiceProvider provider = new TripleDESCryptoServiceProvider())
{
provider.GenerateIV();
secret.EncryptorSharedEncryptedOnceIV = provider.IV;
provider.GenerateKey();
secret.EncryptorSharedEncryptedOnceKey = provider.Key;
secret.EncryptedData = provider.CreateEncryptor().TransformFinalBlock(data, 0, data.Length);
}
secret.EncryptorSharedEncryptedOnceIV = decryptorPublicKeyCerProvider.Encrypt(secret.EncryptorSharedEncryptedOnceIV, DoOAEPadding);
secret.EncryptorSharedEncryptedOnceKey = decryptorPublicKeyCerProvider.Encrypt(secret.EncryptorSharedEncryptedOnceKey, DoOAEPadding);
HashAlgorithm hashAlgorithm;
if (signHashMode == HashSignatureMode.SHA1)
{
hashAlgorithm = new SHA1CryptoServiceProvider();
}
else //(hashSignatureMode == HashSignatureMode.MD5)
{
hashAlgorithm = new MD5CryptoServiceProvider();
}
MemoryStream stream = new MemoryStream();
byte[] buffer = secret.EncryptorSharedEncryptedOnceIV;
stream.Write(buffer, 0, buffer.Length);
buffer = secret.EncryptorSharedEncryptedOnceKey;
stream.Write(buffer, 0, buffer.Length);
buffer = secret.EncryptedData;
stream.Position = 0;
buffer = hashAlgorithm.ComputeHash(stream);
stream.Close();
stream.Dispose();
secret.EncryptorHashSignature = encryptorPrivateKeyPfxProvider.SignHash
(
buffer
, Enum.GetName
(
signHashMode.GetType()
, signHashMode
)
);
secret.EncryptorPublicKeyCerRawData = encryptorPublicKeyCer.RawData;
secret.SignHashMode = signHashMode;
secret.DoOAEPadding = DoOAEPadding;
return secret;
}
public static string GenerateTripleDESHexStringKey()
{
TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
des.GenerateKey();
return BytesArrayToHexString(des.Key);
}
public static string GenerateTripleDESHexStringIV()
{
TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
des.GenerateIV();
return BytesArrayToHexString(des.IV);
}
public static byte[] SymmetricAlgorithmEncrypt
(
SymmetricAlgorithm algorithm
, byte[] data
)
{
return algorithm.CreateEncryptor().TransformFinalBlock(data, 0, data.Length);
}
public static byte[] SymmetricAlgorithmEncrypt
(
SymmetricAlgorithm algorithm
, string text
, Encoding e
)
{
return SymmetricAlgorithmEncrypt
(
algorithm
, e.GetBytes(text)
);
}
public static byte[] SymmetricAlgorithmDecrypt
(
SymmetricAlgorithm algorithm
, byte[] data
)
{
return algorithm.CreateDecryptor().TransformFinalBlock(data, 0, data.Length);
}
public static string SymmetricAlgorithmDecrypt
(
SymmetricAlgorithm algorithm
, byte[] data
, Encoding e //原文的encoding
)
{
return e.GetString
(
SymmetricAlgorithmDecrypt
(
algorithm
, data
)
);
}
public static byte[] ComputeHash
(
HashAlgorithm algorithm
, byte[] data
)
{
return algorithm.ComputeHash(data);
}
public static byte[] ComputeHash
(
HashAlgorithm algorithm
, string text
, Encoding e
)
{
return ComputeHash(algorithm, e.GetBytes(text));
}
public static byte[] ComputeKeyedHash
(
KeyedHashAlgorithm algorithm
, byte[] data
)
{
return ComputeHash(algorithm, data);
}
public static byte[] ComputeKeyedHash
(
KeyedHashAlgorithm algorithm
, string text
, Encoding e
)
{
return ComputeHash(algorithm, text, e);
}
public static byte[] RSASignSHA1
(
string privateKeyXml
, byte[] data
)
{
RSACryptoServiceProvider provider = new RSACryptoServiceProvider();
provider.FromXmlString(privateKeyXml);
return RSASignSHA1
(
provider
, data
);
}
public static byte[] RSASignSHA1
(
RSACryptoServiceProvider provider
, byte[] data
)
{
HashAlgorithm hashAlgorithm = new SHA1CryptoServiceProvider();
return provider.SignHash
(
ComputeHash(hashAlgorithm, data)
, "SHA1"
);
}
public static bool RSAVerifySHA1
(
string publicKeyXml
, byte[] data
, byte[] signature
)
{
RSACryptoServiceProvider provider = new RSACryptoServiceProvider();
provider.FromXmlString(publicKeyXml);
return RSAVerifySHA1
(
provider
, data
, signature
);
}
public static bool RSAVerifySHA1
(
RSACryptoServiceProvider provider
, byte[] data
, byte[] signature
)
{
HashAlgorithm hashAlgorithm = new SHA1CryptoServiceProvider();
return provider.VerifyHash
(
ComputeHash(hashAlgorithm, data)
, "SHA1"
, signature
);
}
public static byte[] RSASignMD5
(
string privateKeyXml
, byte[] data
)
{
RSACryptoServiceProvider provider = new RSACryptoServiceProvider();
provider.FromXmlString(privateKeyXml);
return RSASignMD5
(
provider
, data
);
}
public static byte[] RSASignMD5
(
RSACryptoServiceProvider provider
, byte[] data
)
{
HashAlgorithm hashAlgorithm = new MD5CryptoServiceProvider();
return provider.SignHash
(
ComputeHash(hashAlgorithm, data)
, "MD5"
);
}
public static bool RSAVerifyMD5
(
string publicKeyXml
, byte[] data
, byte[] signature
)
{
RSACryptoServiceProvider provider = new RSACryptoServiceProvider();
provider.FromXmlString(publicKeyXml);
return RSAVerifyMD5
(
provider
, data
, signature
);
}
public static bool RSAVerifyMD5
(
RSACryptoServiceProvider provider
, byte[] data
, byte[] signature
)
{
HashAlgorithm hashAlgorithm = new MD5CryptoServiceProvider();
return provider.VerifyHash
(
ComputeHash(hashAlgorithm, data)
, "MD5"
, signature
);
}
public static byte[] RSAEncrypt
(
string publicKeyXml
, byte[] data
, bool DoOAEPPadding
)
{
RSACryptoServiceProvider provider = new RSACryptoServiceProvider();
provider.FromXmlString(publicKeyXml);
return RSAEncrypt
(
provider
, data
, DoOAEPPadding
);
}
public static byte[] RSAEncrypt
(
RSACryptoServiceProvider provider
, byte[] data
, bool DoOAEPPadding
)
{
return provider.Encrypt(data, DoOAEPPadding);
}
public static byte[] RSADecrypt
(
string privateKeyXml
, byte[] data
, bool DoOAEPPadding
)
{
RSACryptoServiceProvider provider = new RSACryptoServiceProvider();
provider.FromXmlString(privateKeyXml);
return RSADecrypt
(
provider
, data
, DoOAEPPadding
);
}
public static byte[] RSADecrypt
(
RSACryptoServiceProvider provider
, byte[] data
, bool DoOAEPPadding
)
{
return provider.Decrypt(data, DoOAEPPadding);
}
public static byte[] X509CertificateEncrypt
(
X509Certificate2 publicKeyCer
, byte[] data
, bool DoOAEPadding
)
{
RSACryptoServiceProvider provider = publicKeyCer.PublicKey.Key as RSACryptoServiceProvider;
return RSAEncrypt
(
provider
, data
, DoOAEPadding
);
}
public static byte[] X509CertificateDecrypt
(
X509Certificate2 privateKeyPfx
, byte[] data
, bool DoOAEPadding
)
{
RSACryptoServiceProvider provider = privateKeyPfx.PrivateKey as RSACryptoServiceProvider;
return RSADecrypt
(
provider
, data
, DoOAEPadding
);
}
public static byte[] X509CertificateSignSHA1
(
X509Certificate2 privateKeyPfx
, byte[] data
)
{
RSACryptoServiceProvider provider = privateKeyPfx.PrivateKey as RSACryptoServiceProvider;
return RSASignSHA1
(
provider
, data
);
}
public static byte[] X509CertificateSignMD5
(
X509Certificate2 privateKeyPfx
, byte[] data
)
{
RSACryptoServiceProvider provider = privateKeyPfx.PrivateKey as RSACryptoServiceProvider;
return RSASignMD5(provider, data);
}
public static bool X509CertificateVerifySHA1
(
X509Certificate2 publicKeyCer
, byte[] data
, byte[] signature
)
{
RSACryptoServiceProvider provider = publicKeyCer.PublicKey.Key as RSACryptoServiceProvider;
return RSAVerifySHA1
(
provider
, data
, signature
);
}
public static bool X509CertificateVerifyMD5
(
X509Certificate2 publicKeyCer
, byte[] data
, byte[] signature
)
{
RSACryptoServiceProvider provider = publicKeyCer.PublicKey.Key as RSACryptoServiceProvider;
return RSAVerifyMD5
(
provider
, data
, signature
);
}
public static string BytesArrayToHexString(byte[] data)
{
return BitConverter.ToString(data).Replace("-", "");
}
public static byte[] HexStringToBytesArray(string text)
{
text = text.Replace(" ", "");
int l = text.Length;
byte[] buffer = new byte[l / 2];
for (int i = 0; i < l; i += 2)
{
buffer[i / 2] = Convert.ToByte(text.Substring(i, 2), 16);
}
return buffer;
}
}
}
|