1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Crypto
{
using System;
using System.Diagnostics;
using System.Security.Cryptography;
using System.Text;
public class Crypto
{
//---------------------------------------
//RSA
static public void GenerateKeys(ref string __publicPrivateKeyXML, ref string __publicOnlyKeyXML, ref string __sMessErr, int _ikeySize=1024)
{
try
{
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(_ikeySize);
//Pair of public and private key as XML string. Do not share this to other party
__publicPrivateKeyXML = RSA.ToXmlString(true);
//Private key in xml file, this string should be share to other parties
__publicOnlyKeyXML = RSA.ToXmlString(false);
}
catch (CryptographicException e)
{
__sMessErr = e.ToString();
}
}
static public string RSAEncrypt(string _sDataToEncrypt, string _sPublicKeyXML, ref string __sMessErr, int _ikeySize=1024, bool DoOAEPPadding=true)
{
try
{
byte[] dataToEncrypt = Encoding.UTF8.GetBytes(_sDataToEncrypt);
byte[] encryptedData;
//Create a new instance of RSACryptoServiceProvider.
using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(_ikeySize))
{
RSA.FromXmlString(_sPublicKeyXML);
//Encrypt the passed byte array and specify OAEP padding. OAEP padding is only available on Microsoft Windows XP or later.
encryptedData = RSA.Encrypt(dataToEncrypt, DoOAEPPadding);
}
return Convert.ToBase64String(encryptedData);
}
catch (CryptographicException e)
{
__sMessErr = e.ToString();
return null;
}
}
static public string RSADecrypt(string _sDataToDecryptBase64,string _sPrivateKeyXML, ref string __sMessErr, int _ikeySize=1024, bool DoOAEPPadding=true)
{
try
{
byte[] dataToDecrypt = Convert.FromBase64String(_sDataToDecryptBase64);
byte[] decryptedBytes;
string decryptedData;
//Create a new instance of RSACryptoServiceProvider.
using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(_ikeySize))
{
RSA.FromXmlString(_sPrivateKeyXML);
//Decrypt the passed byte array and specify OAEP padding. OAEP padding is only available on Microsoft Windows XP or later.
decryptedBytes = RSA.Decrypt(dataToDecrypt, DoOAEPPadding);
decryptedData = Encoding.UTF8.GetString(decryptedBytes);
}
return decryptedData;
}
catch (CryptographicException e)
{
__sMessErr = e.ToString();
return null;
}
}
//---------------------------------------
//Haschage
public static string GetMD5(string text)
{
return GetMD5(text, Encoding.Default);
}
public static string GetMD5(string text, Encoding enc)
{
return GetHash<MD5CryptoServiceProvider>(text, enc);
}
public static string GetSHA1(string text)
{
return GetSHA1(text, Encoding.Default);
}
public static string GetSHA1(string text, Encoding enc)
{
return GetHash<SHA1CryptoServiceProvider>(text, enc);
}
public static string GetSHA384(string text)
{
return GetSHA384(text, Encoding.Default);
}
public static string GetSHA384(string text, Encoding enc)
{
return GetHash<SHA384CryptoServiceProvider>(text, enc);
}
public static string GetSHA512(string text)
{
return GetSHA512(text, Encoding.Default);
}
public static string GetSHA512(string text, Encoding enc)
{
return GetHash<SHA512CryptoServiceProvider>(text, enc);
}
public static string GetSHA256(string text)
{
return GetSHA256(text, Encoding.Default);
}
public static string GetSHA256(string text, Encoding enc)
{
return GetHash<SHA256CryptoServiceProvider>(text, enc);
}
public static string GetHash<TP>(string text, Encoding enc)
where TP : HashAlgorithm, new()
{
var buffer = enc.GetBytes(text);
var provider = new TP();
return BitConverter.ToString(provider.ComputeHash(buffer)).Replace("-", "");
}
//---------------------------------------
//Encodage
public static Encoding GetEncoding(int codePage)
{
return Encoding.GetEncoding(codePage);
}
public static string UTF8Encode(string text)
{
return UTF8Encode(text,Encoding.Default);
}
public static string UTF8Encode(string text, Encoding enc)
{
byte[] textUTF8 = Encoding.Convert(enc,Encoding.UTF8,enc.GetBytes(text));
return enc.GetString(textUTF8);
}
public static string UnicodeEncode(string text)
{
return UnicodeEncode(text, Encoding.Default);
}
public static string UnicodeEncode(string text, Encoding enc)
{
byte[] textUnicode = Encoding.Convert(enc, Encoding.Unicode, enc.GetBytes(text));
return enc.GetString(textUnicode);
}
}
} |
Partager