Class CryptoHelper
This is the Helper to encrypt and decrypt the information with Secure Exchanges
Inheritance
Inherited Members
Namespace: SecureExchangesSDK.Helpers
Assembly: SecureExchangesSDK.dll
Syntax
public static class CryptoHelper
Methods
AlterBinary(String, Byte[], Int32)
This will derive a byteArray with a Password and a number of iteration
Declaration
public static byte[] AlterBinary(string password, byte[] original, int iteration)
Parameters
Type | Name | Description |
---|---|---|
System.String | password | Password to use for derivation, longer, is better |
System.Byte[] | original | The original byte array to derive |
System.Int32 | iteration | the number of iteration you want to derive. Higher is better, but will be more slow. 1000 should be use |
Returns
Type | Description |
---|---|
System.Byte[] |
Examples
Random rnd = new Random();
var iteration = rnd.Next(1000, 1200);
byte[] alterByteArray = CryptoHelper.AlterBinary("ThatIsAVeryComplexPswMmmNotSure!!!?", binary, iteration);
AlterGuid(String, Guid)
That is use to Derive a Guid with a password
Declaration
public static Guid AlterGuid(string password, Guid original)
Parameters
Type | Name | Description |
---|---|---|
System.String | password | Password need to be use for derivation |
System.Guid | original | Original Guid need to be derived |
Returns
Type | Description |
---|---|
System.Guid |
Examples
Guid newGuid = CryptoHelper.AlterGuid(password, MyGuid);
ClearRSAKeyStore(String)
Declaration
public static bool ClearRSAKeyStore(string keyStoreName)
Parameters
Type | Name | Description |
---|---|---|
System.String | keyStoreName |
Returns
Type | Description |
---|---|
System.Boolean |
ConcatByteArray(Byte[][])
Use to merge multiple bytearray togheter
Declaration
public static byte[] ConcatByteArray(params byte[][] arrays)
Parameters
Type | Name | Description |
---|---|---|
System.Byte[][] | arrays | Return a single bytes arrays |
Returns
Type | Description |
---|---|
System.Byte[] |
Examples
byte[] singleByteArray = CryptoHelper.ConcatByteArray(byte[], byte[],byte[]);
CreateNewPrivateCertificate(Int32, Boolean)
Export a certificate with the private Key
Declaration
public static string CreateNewPrivateCertificate(int keySize, bool exportPrivateKey = false)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | keySize | The key site |
System.Boolean | exportPrivateKey | Did the CER string will contains the certificate |
Returns
Type | Description |
---|---|
System.String |
CreateNewPrivateKey(Int32)
Create a new private RSA key xml format
Declaration
public static string CreateNewPrivateKey(int keySize)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | keySize | Key size need to be use, 256, 512, 1024, 2048, 4096, 8192 |
Returns
Type | Description |
---|---|
System.String | Return the xml private key |
CreateRSAKeyStore(String, Int32)
Declaration
public static string CreateRSAKeyStore(string keyStoreName, int keySize = 2048)
Parameters
Type | Name | Description |
---|---|---|
System.String | keyStoreName | |
System.Int32 | keySize |
Returns
Type | Description |
---|---|
System.String |
DecryptBinaryFromBytes(Byte[], Byte[], Byte[], PaddingMode)
This method is use to decrypt binary Rijndael
Declaration
public static byte[] DecryptBinaryFromBytes(byte[] cipher, byte[] Key, byte[] IV, PaddingMode paddingMode = PaddingMode.PKCS7)
Parameters
Type | Name | Description |
---|---|---|
System.Byte[] | cipher | The binary to decrypt, canot be null |
System.Byte[] | Key | The key use to decrypt the binary |
System.Byte[] | IV | The iv use to decrypt the binary |
System.Security.Cryptography.PaddingMode | paddingMode | Padding mode use to decrypt. The default is PKCS7 |
Returns
Type | Description |
---|---|
System.Byte[] | The decrypted array |
Examples
byte[] unEncryptedByteArray = CryptoHelper.DecryptBinaryFromBytes(Convert.FromBase64String(Base64BinaryArray), Key, IV);
DecryptFile(String, String, Byte[], Byte[])
Decrypt file, destination will be override.
Declaration
public static void DecryptFile(string sourceFilename, string destinationFilename, byte[] Key, byte[] IV)
Parameters
Type | Name | Description |
---|---|---|
System.String | sourceFilename | |
System.String | destinationFilename | |
System.Byte[] | Key | |
System.Byte[] | IV |
DecryptRSAContent(String, Byte[], Boolean)
Decrypt RSA crypted data with private key, and return a byte array
Declaration
public static byte[] DecryptRSAContent(string privateKey, byte[] cryptedData, bool fOAEP)
Parameters
Type | Name | Description |
---|---|---|
System.String | privateKey | The xml private key need to be use to uncrypt the crypteddata |
System.Byte[] | cryptedData | Byte array of encrypted data |
System.Boolean | fOAEP | true to perform direct System.Security.Cryptography.RSA encryption using OAEP padding (only available on a computer runing XP or later); otherwise, false to use PCKS#1.5 padding |
Returns
Type | Description |
---|---|
System.Byte[] | The byte array uncrypted of the byte array |
DecryptRSAContentToString(String, Byte[], Boolean)
Decrypt RSA crypted data with private key, and return a string
Declaration
public static string DecryptRSAContentToString(string privateKey, byte[] cryptedData, bool fOAEP)
Parameters
Type | Name | Description |
---|---|---|
System.String | privateKey | The xml private key need to be use to uncrypt the crypteddata |
System.Byte[] | cryptedData | Byte array of encrypted data |
System.Boolean | fOAEP | true to perform direct System.Security.Cryptography.RSA encryption using OAEP padding (only available on a computer runing XP or later); otherwise, false to use PCKS#1.5 padding |
Returns
Type | Description |
---|---|
System.String | The string uncrypted of the byte array |
Examples
string decryptedString = CryptoHelper.DecryptRSAContentToString(privateKey, stringByteArray, true);
DecryptStringFromBase64(String, Byte[], Byte[])
Decrypt base 64 string
Declaration
public static string DecryptStringFromBase64(string b64, byte[] Key, byte[] IV)
Parameters
Type | Name | Description |
---|---|---|
System.String | b64 | The encrypted string base64 |
System.Byte[] | Key | The key need to be use for decryption |
System.Byte[] | IV | The iv need to be use for decryption |
Returns
Type | Description |
---|---|
System.String |
Examples
string decryptedString = DecryptStringFromBytes("SSBuZWVkIHRvIGJlIGRlY3J5cHRlZD8gWW91IGFyZSByaWdodCwgdGhhdCB3YXMgbm90IHJlYWxseSBlbmNyeXB0ZWQhISA6LSk=", Key, IV);
DecryptStringFromBase64(String, String, String)
Declaration
public static string DecryptStringFromBase64(string b64, string Base64Key, string Base64IV)
Parameters
Type | Name | Description |
---|---|---|
System.String | b64 | |
System.String | Base64Key | |
System.String | Base64IV |
Returns
Type | Description |
---|---|
System.String |
DecryptStringFromBytes(Byte[], Byte[], Byte[])
Decrypt binaryText
Declaration
public static string DecryptStringFromBytes(byte[] cipherText, byte[] Key, byte[] IV)
Parameters
Type | Name | Description |
---|---|---|
System.Byte[] | cipherText | The encrypted binary data |
System.Byte[] | Key | The key need to be used for decryption |
System.Byte[] | IV | The iv need to be used for decryption |
Returns
Type | Description |
---|---|
System.String | return a string unencrypted of a string encrypted |
Examples
DecryptStringFromBytes(Convert.FromBase64String(b64), Key, IV);
EncryptBinary(Byte[], Byte[], Byte[], PaddingMode)
Use to encrypt with Rijndael a byte array
Declaration
public static byte[] EncryptBinary(byte[] binary, byte[] Key, byte[] IV, PaddingMode paddingMode = PaddingMode.PKCS7)
Parameters
Type | Name | Description |
---|---|---|
System.Byte[] | binary | Binary need to be encrypted |
System.Byte[] | Key | The key of the encryption |
System.Byte[] | IV | The iv of the encryption |
System.Security.Cryptography.PaddingMode | paddingMode |
Returns
Type | Description |
---|---|
System.Byte[] | Return the crypted bytes array |
Examples
byte[] cryptedBinary = CryptoHelper.EncryptBinary(Binary, Key, IV);
EncryptFile(String, String, Byte[], Byte[])
Use to encrypt file destination will be override
Declaration
public static void EncryptFile(string sourceFilename, string destinationFilename, byte[] Key, byte[] IV)
Parameters
Type | Name | Description |
---|---|---|
System.String | sourceFilename | |
System.String | destinationFilename | |
System.Byte[] | Key | |
System.Byte[] | IV |
EncryptRSAContent(String, Byte[], Boolean)
Encrypt a byte array with a public key
Declaration
public static byte[] EncryptRSAContent(string publicKey, byte[] content, bool fOAEP)
Parameters
Type | Name | Description |
---|---|---|
System.String | publicKey | The xml public key need to be use |
System.Byte[] | content | The string need to be encrypted |
System.Boolean | fOAEP | true to perform direct System.Security.Cryptography.RSA encryption using OAEP padding (only available on a computer runing XP or later); otherwise, false to use PCKS#1.5 padding |
Returns
Type | Description |
---|---|
System.Byte[] | Byte array of the byte array, encrypted with the public key |
Examples
byte[] cryptedString = EncryptRSAContent(publicKey, contentBinary, true);
EncryptRSAContent(String, String)
Encrypt RSA content with xml public key
Declaration
public static byte[] EncryptRSAContent(string publicKey, string content)
Parameters
Type | Name | Description |
---|---|---|
System.String | publicKey | Public key |
System.String | content | The string need to be encrypted |
Returns
Type | Description |
---|---|
System.Byte[] | Encrypted string |
Examples
byte[] encryptedData = EncryptRSAContent(publicKey, content);
EncryptRSAContent(String, String, Boolean)
Encrypt a string with a public key
Declaration
public static byte[] EncryptRSAContent(string publicKey, string content, bool fOAEP)
Parameters
Type | Name | Description |
---|---|---|
System.String | publicKey | The xml public key need to be use |
System.String | content | The string need to be encrypted System.Text.Encoding.Default |
System.Boolean | fOAEP | true to perform direct System.Security.Cryptography.RSA encryption using OAEP padding (only available on a computer runing XP or later); otherwise, false to use PCKS#1.5 padding |
Returns
Type | Description |
---|---|
System.Byte[] | Byte array of the string, encrypted with the public key |
Examples
byte[] cryptedString = EncryptRSAContent(publicKey, contentString, true);
EncryptStringToB64(String, Byte[], Byte[])
Use to encrypt a string to UTF8 base 64
Declaration
public static string EncryptStringToB64(string plainText, byte[] Key, byte[] IV)
Parameters
Type | Name | Description |
---|---|---|
System.String | plainText | Text to encrypt |
System.Byte[] | Key | The key use to encrypt |
System.Byte[] | IV | The Iv use to encrypt |
Returns
Type | Description |
---|---|
System.String | The base64 string |
Examples
string cryptedString = CryptoHelper.EncryptStringToB64("I need encryption for this string", KEY, IV);
EncryptStringToBytes(String, Byte[], Byte[])
Use to encrypt plainText UTF8 to byte array
Declaration
public static byte[] EncryptStringToBytes(string plainText, byte[] Key, byte[] IV)
Parameters
Type | Name | Description |
---|---|---|
System.String | plainText | |
System.Byte[] | Key | |
System.Byte[] | IV |
Returns
Type | Description |
---|---|
System.Byte[] | A byte array encrypted. The plainText must be in UTF-8 |
Examples
byte[] encrypted = CryptoHelper.EncryptStringToBytes(plainText, Key, IV);
EnncryptRSAContentToBase64String(String, String)
Encrypt with public key a string. Use RSA crypto provider
Declaration
public static string EnncryptRSAContentToBase64String(string publicKey, string content)
Parameters
Type | Name | Description |
---|---|---|
System.String | publicKey | The public XML public key need to be used |
System.String | content | The content to encrypt |
Returns
Type | Description |
---|---|
System.String | Base64 encrypted string |
Examples
string encryptedString = CryptoHelper.EnncryptRSAContentToBase64String(PublicKey, Content);
FillSecureRandomByteArray(Byte[])
Fill a array with random strong value
Declaration
public static void FillSecureRandomByteArray(byte[] data)
Parameters
Type | Name | Description |
---|---|---|
System.Byte[] | data | Array to fill |
GenerateAESKeys()
Generate a new AES keys
Declaration
public static Keys GenerateAESKeys()
Returns
Type | Description |
---|---|
Keys |
GenerateBase64KeyAndIV(out String, out String)
Generate 256 bits base 64 key and base 64 iv key with RijndaelManaged.
Declaration
public static void GenerateBase64KeyAndIV(out string b64Key, out string b64iv)
Parameters
Type | Name | Description |
---|---|---|
System.String | b64Key | |
System.String | b64iv |
GenerateSecureRandomByteArray(Int32)
That method will return a array fill with SecureRandomValue
Declaration
public static byte[] GenerateSecureRandomByteArray(int length)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | length |
Returns
Type | Description |
---|---|
System.Byte[] |
GetKeyFromSHA512(Byte[])
From a SHA512 return a 32 bit keys
Declaration
public static byte[] GetKeyFromSHA512(byte[] hash)
Parameters
Type | Name | Description |
---|---|---|
System.Byte[] | hash |
Returns
Type | Description |
---|---|
System.Byte[] |
GetMD5Bytes(String)
Declaration
public static byte[] GetMD5Bytes(string plainText)
Parameters
Type | Name | Description |
---|---|---|
System.String | plainText |
Returns
Type | Description |
---|---|
System.Byte[] |
GetMD5HashOfBytes(Byte[])
Return a MD5 Hexa string of the hash
Declaration
public static string GetMD5HashOfBytes(byte[] b)
Parameters
Type | Name | Description |
---|---|---|
System.Byte[] | b | Source binary that need to get the Hash |
Returns
Type | Description |
---|---|
System.String | MD5 128 bit hexa hash |
Examples
string fileHash = CryptoHelper.GetMD5HashOfBytes(Binary);
GetMD5HashOfFile(String)
Return a MD5 hash of a file
Declaration
public static string GetMD5HashOfFile(string filePath)
Parameters
Type | Name | Description |
---|---|---|
System.String | filePath | Path to the file |
Returns
Type | Description |
---|---|
System.String | HEXA MD5 string file |
Examples
string hash = CryptoHelper.GetMD5HashOfFile(myfile.FileName);
GetMD5HashOfStream(Stream)
Get the Hexa MD5 string of a stream
Declaration
public static string GetMD5HashOfStream(Stream stream)
Parameters
Type | Name | Description |
---|---|---|
System.IO.Stream | stream |
Returns
Type | Description |
---|---|
System.String |
Examples
using (var stream = new BufferedStream(File.OpenRead(filePath), 3145728))
{
return GetMD5HashOfStream(stream);
}
GetMD5HashOfString(String)
Return the MD5 hash of a string
Declaration
public static string GetMD5HashOfString(string stringToHash)
Parameters
Type | Name | Description |
---|---|---|
System.String | stringToHash | UTF-8 string |
Returns
Type | Description |
---|---|
System.String | HEXA string of the Hash |
Examples
string hash = CryptoHelper.GetMD5HashOfString(content);
GetPublicKeyFromPrivateKey(String)
Return the public RSA key of a private key. the public key will be in xml format
Declaration
public static string GetPublicKeyFromPrivateKey(string privateKey)
Parameters
Type | Name | Description |
---|---|---|
System.String | privateKey | XML private key to extract the public key |
Returns
Type | Description |
---|---|
System.String | The xml public key of the private key |
GetPublicKeyFromPrivateXmlOrPEM(String)
Declaration
public static RSAKeys GetPublicKeyFromPrivateXmlOrPEM(string privateKey)
Parameters
Type | Name | Description |
---|---|---|
System.String | privateKey |
Returns
Type | Description |
---|---|
RSAKeys |
GetPublicKeySize(String)
Get the key size of a public key
Declaration
public static int GetPublicKeySize(string publicKey)
Parameters
Type | Name | Description |
---|---|---|
System.String | publicKey |
Returns
Type | Description |
---|---|
System.Int32 |
GetSHA256Bytes(Byte[])
REturn the hash of a SHA256 of bytes
Declaration
public static byte[] GetSHA256Bytes(byte[] bytes)
Parameters
Type | Name | Description |
---|---|---|
System.Byte[] | bytes |
Returns
Type | Description |
---|---|
System.Byte[] |
GetSHA256Bytes(String)
Get the SHA 256 of UTF-8 string
Declaration
public static byte[] GetSHA256Bytes(string plainText)
Parameters
Type | Name | Description |
---|---|---|
System.String | plainText |
Returns
Type | Description |
---|---|
System.Byte[] |
GetSHA256HashOfBytes(Byte[])
Get the HEX hash of bytes
Declaration
public static string GetSHA256HashOfBytes(byte[] b)
Parameters
Type | Name | Description |
---|---|---|
System.Byte[] | b |
Returns
Type | Description |
---|---|
System.String |
GetSHA256HashOfString(String)
Get the HEX hash of a string utf8
Declaration
public static string GetSHA256HashOfString(string b)
Parameters
Type | Name | Description |
---|---|---|
System.String | b |
Returns
Type | Description |
---|---|
System.String |
GetSHA256OfFile(String)
Get the HEX hash of file
Declaration
public static string GetSHA256OfFile(string filePath)
Parameters
Type | Name | Description |
---|---|---|
System.String | filePath |
Returns
Type | Description |
---|---|
System.String |
GetSHA256OfStream(Stream)
Get the HEX hash of stream
Declaration
public static string GetSHA256OfStream(Stream stream)
Parameters
Type | Name | Description |
---|---|---|
System.IO.Stream | stream |
Returns
Type | Description |
---|---|
System.String |
GetSHA512Bytes(Byte[])
Return the hash of a SHA 512 of bytes
Declaration
public static byte[] GetSHA512Bytes(byte[] bytes)
Parameters
Type | Name | Description |
---|---|---|
System.Byte[] | bytes |
Returns
Type | Description |
---|---|
System.Byte[] |
GetSHA512Bytes(String)
Get SHA 512 of UTF-8 string
Declaration
public static byte[] GetSHA512Bytes(string plainText)
Parameters
Type | Name | Description |
---|---|---|
System.String | plainText |
Returns
Type | Description |
---|---|
System.Byte[] |
GetSHA512HashOfBytes(Byte[])
Get the HEX hash of bytes
Declaration
public static string GetSHA512HashOfBytes(byte[] b)
Parameters
Type | Name | Description |
---|---|---|
System.Byte[] | b |
Returns
Type | Description |
---|---|
System.String |
GetSHA512HashOfString(String)
Get the HEX hash of a string utf8
Declaration
public static string GetSHA512HashOfString(string b)
Parameters
Type | Name | Description |
---|---|---|
System.String | b |
Returns
Type | Description |
---|---|
System.String |
GetSHA512OfFile(String)
Get the HEX hash of file
Declaration
public static string GetSHA512OfFile(string filePath)
Parameters
Type | Name | Description |
---|---|---|
System.String | filePath |
Returns
Type | Description |
---|---|
System.String |
GetSHA512OfStream(Stream)
Get the HEX hash of stream
Declaration
public static string GetSHA512OfStream(Stream stream)
Parameters
Type | Name | Description |
---|---|---|
System.IO.Stream | stream |
Returns
Type | Description |
---|---|
System.String |
GetStrongKeyStoreNameFromAssemblyGuid()
Declaration
public static string GetStrongKeyStoreNameFromAssemblyGuid()
Returns
Type | Description |
---|---|
System.String |
KeyAlreadyExistInMachineKeys(String)
Declaration
public static bool KeyAlreadyExistInMachineKeys(string strongName)
Parameters
Type | Name | Description |
---|---|---|
System.String | strongName |
Returns
Type | Description |
---|---|
System.Boolean |
RSADencryptWhitKeyContainerFromMachineKeys(Byte[], String)
Declaration
public static byte[] RSADencryptWhitKeyContainerFromMachineKeys(byte[] encrBytes, string containerName)
Parameters
Type | Name | Description |
---|---|---|
System.Byte[] | encrBytes | |
System.String | containerName |
Returns
Type | Description |
---|---|
System.Byte[] |
RSADencryptWhitKeyContainerFromMachineKeysToString(Byte[], String)
Declaration
public static string RSADencryptWhitKeyContainerFromMachineKeysToString(byte[] encrBytes, string containerName)
Parameters
Type | Name | Description |
---|---|---|
System.Byte[] | encrBytes | |
System.String | containerName |
Returns
Type | Description |
---|---|
System.String |
RSAEncryptWhitKeyContainerMachineKeyStore(String, String)
Declaration
public static byte[] RSAEncryptWhitKeyContainerMachineKeyStore(string message, string keyContainerName)
Parameters
Type | Name | Description |
---|---|---|
System.String | message | |
System.String | keyContainerName |
Returns
Type | Description |
---|---|
System.Byte[] |
RSAEncryptWhitKeyContainerMachineKeyStoreToBase64(String, String)
Declaration
public static string RSAEncryptWhitKeyContainerMachineKeyStoreToBase64(string message, string keyContainerName)
Parameters
Type | Name | Description |
---|---|---|
System.String | message | |
System.String | keyContainerName |
Returns
Type | Description |
---|---|
System.String |
SignDataWithPrivateKey(String, Byte[], String)
Get the signature of a message signed with a private key
Declaration
public static byte[] SignDataWithPrivateKey(string privateKey, byte[] cryptedData, string hashAlgo = "SHA512")
Parameters
Type | Name | Description |
---|---|---|
System.String | privateKey | |
System.Byte[] | cryptedData | |
System.String | hashAlgo |
Returns
Type | Description |
---|---|
System.Byte[] |
SignDataWithPrivateKey(String, String, String)
return a base64 signature of the hash
Declaration
public static string SignDataWithPrivateKey(string privateKey, string clearText, string hashAlgo = "SHA512")
Parameters
Type | Name | Description |
---|---|---|
System.String | privateKey | |
System.String | clearText | |
System.String | hashAlgo |
Returns
Type | Description |
---|---|
System.String |
TransformXmlKeyToPEM(String)
That will transform a xml key to certificate
Declaration
public static string TransformXmlKeyToPEM(string xmlKey)
Parameters
Type | Name | Description |
---|---|---|
System.String | xmlKey | If we get the private key, the PEM will contain the private key if not only the public key |
Returns
Type | Description |
---|---|
System.String |
VerifySignature(String, Byte[], Byte[], String)
Vérifie la signature d'un échange clé publique privé
Declaration
public static bool VerifySignature(string xmlPublicKey, byte[] signature, byte[] clearHashData, string hashAlgo = "SHA512")
Parameters
Type | Name | Description |
---|---|---|
System.String | xmlPublicKey | La clé publique associer à la clé privé |
System.Byte[] | signature | La signature en bytes |
System.Byte[] | clearHashData | Le hash des données signers en bytes |
System.String | hashAlgo | l'alrorithme utiliser |
Returns
Type | Description |
---|---|
System.Boolean |
VerifySignature(String, String, String, String)
Vérifie la signature d'un échange clé publique privé
Declaration
public static bool VerifySignature(string xmlPublicKey, string base64Signature, string clearBase64HashData, string hashAlgo = "SHA512")
Parameters
Type | Name | Description |
---|---|---|
System.String | xmlPublicKey | La clé publique associer à la clé privé |
System.String | base64Signature | La signature fait à partir de la clé privé en base 64 |
System.String | clearBase64HashData | Le hash du message qui à été signer en base 64 |
System.String | hashAlgo | l'algoritme utiliser |
Returns
Type | Description |
---|---|
System.Boolean |