Table of Contents

Class CryptoHelper

Espace de noms
SecureExchangesSDK.Helpers
Assembly
SecureExchangesSDK.NetCore.dll

This is the Helper to encrypt and decrypt the information with Secure Exchanges

public static class CryptoHelper
Héritage
CryptoHelper
Membres hérités

Methods

AlterBinary(string, byte[], int, int)

This will derive a byteArray with a Password and a number of iteration

public static byte[] AlterBinary(string password, byte[] original, int iteration, int maskLength = 16)

Paramètres

password string

Password to use for derivation, longer, is better

original byte[]

The original byte array to derive

iteration int

the number of iteration you want to derive. Higher is better, but will be more slow. 1000 should be use

maskLength int

Retour

byte[]

Exemples

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

public static Guid AlterGuid(string password, Guid original)

Paramètres

password string

Password need to be use for derivation

original Guid

Original Guid need to be derived

Retour

Guid

Exemples

Guid newGuid =  CryptoHelper.AlterGuid(password, MyGuid);

BytesXor(byte[], byte[])

Performs XOR operation between two byte arrays of equal length.

public static byte[] BytesXor(byte[] mask, byte[] source)

Paramètres

mask byte[]

The mask byte array to XOR with the source.

source byte[]

The source byte array to be XORed.

Retour

byte[]

A new byte array containing the XOR result.

Exemples

byte[] mask = new byte[] { 0x0F, 0xF0, 0xAA };
byte[] source = new byte[] { 0x55, 0x55, 0x55 };
byte[] result = CryptoHelper.BytesXor(mask, source);
// result = { 0x5A, 0xA5, 0xFF }

Exceptions

Exception

Thrown when the arrays have different lengths.

ClearRSAKeyStore(string)

public static bool ClearRSAKeyStore(string keyStoreName)

Paramètres

keyStoreName string

Retour

bool

ConcatByteArray(params byte[][])

Use to merge multiple bytearray togheter

public static byte[] ConcatByteArray(params byte[][] arrays)

Paramètres

arrays byte[][]

Return a single bytes arrays

Retour

byte[]

Exemples

byte[] singleByteArray =  CryptoHelper.ConcatByteArray(byte[], byte[],byte[]);

CreateNewPrivateCertificate(int, bool)

Export a certificate with the private Key

public static string CreateNewPrivateCertificate(int keySize, bool exportPrivateKey = false)

Paramètres

keySize int

The key site

exportPrivateKey bool

Did the CER string will contains the certificate

Retour

string

CreateNewPrivateKey(int)

Create a new private RSA key xml format

public static string CreateNewPrivateKey(int keySize)

Paramètres

keySize int

Key size need to be use, 256, 512, 1024, 2048, 4096, 8192

Retour

string

Return the xml private key

CreateRSAKeyStore(string, int)

Create an RSA key stored in the file system (cross-platform alternative to CspParameters MachineKeyStore). On Windows, falls back to CspParameters. On other platforms, uses file-based key storage.

public static string CreateRSAKeyStore(string keyStoreName, int keySize = 2048)

Paramètres

keyStoreName string

The key store name

keySize int

The key size

Retour

string

The key container name

DecryptBinaryFromBytes(byte[], byte[], byte[], PaddingMode)

This method is use to decrypt binary AES

public static byte[] DecryptBinaryFromBytes(byte[] cipher, byte[] Key, byte[] IV, PaddingMode paddingMode = PaddingMode.PKCS7)

Paramètres

cipher byte[]

The binary to decrypt, canot be null

Key byte[]

The key use to decrypt the binary

IV byte[]

The iv use to decrypt the binary

paddingMode PaddingMode

Padding mode use to decrypt. The default is PKCS7

Retour

byte[]

The decrypted array

Exemples

byte[] unEncryptedByteArray =  CryptoHelper.DecryptBinaryFromBytes(Convert.FromBase64String(Base64BinaryArray), Key, IV);

DecryptFile(string, string, byte[], byte[])

Decrypt file, destination will be override.

public static void DecryptFile(string sourceFilename, string destinationFilename, byte[] Key, byte[] IV)

Paramètres

sourceFilename string
destinationFilename string
Key byte[]
IV byte[]

DecryptRSAContent(string, byte[], bool)

Decrypt RSA crypted data with private key, and return a byte array

public static byte[] DecryptRSAContent(string privateKey, byte[] cryptedData, bool fOAEP)

Paramètres

privateKey string

The xml private key need to be use to uncrypt the crypteddata

cryptedData byte[]

Byte array of encrypted data

fOAEP bool

true to perform direct System.Security.Cryptography.RSA encryption using OAEP padding; otherwise, false to use PCKS#1.5 padding

Retour

byte[]

The byte array uncrypted of the byte array

DecryptRSAContentToString(string, byte[], bool)

Decrypt RSA crypted data with private key, and return a string

public static string DecryptRSAContentToString(string privateKey, byte[] cryptedData, bool fOAEP)

Paramètres

privateKey string

The xml private key need to be use to uncrypt the crypteddata

cryptedData byte[]

Byte array of encrypted data

fOAEP bool

true to perform direct System.Security.Cryptography.RSA encryption using OAEP padding; otherwise, false to use PCKS#1.5 padding

Retour

string

The string uncrypted of the byte array

Exemples

string decryptedString = CryptoHelper.DecryptRSAContentToString(privateKey, stringByteArray, true);

DecryptSecfFile(string, byte[], byte[])

Decrypt file, destination will be override.

public static bool DecryptSecfFile(string sourceFilename, byte[] Key, byte[] IV)

Paramètres

sourceFilename string
Key byte[]
IV byte[]

Retour

bool

DecryptSecfFile(string, string, byte[], byte[])

Decrypt file, destination will be override.

public static bool DecryptSecfFile(string sourceFilename, string destinationDirectory, byte[] Key, byte[] IV)

Paramètres

sourceFilename string
destinationDirectory string
Key byte[]
IV byte[]

Retour

bool

DecryptStream(Stream, Stream, byte[], byte[], PaddingMode)

Decrypt Binaray with stream

public static void DecryptStream(Stream inputStream, Stream outputStream, byte[] Key, byte[] IV, PaddingMode paddingMode = PaddingMode.PKCS7)

Paramètres

inputStream Stream

The caller must close the input stream after use

outputStream Stream

The caller must close the output stream after use

Key byte[]

The key

IV byte[]

The IV

paddingMode PaddingMode

The padding mode

Exceptions

ArgumentNullException

DecryptStringFromBase64(string, byte[], byte[])

Decrypt base 64 string

public static string DecryptStringFromBase64(string b64, byte[] Key, byte[] IV)

Paramètres

b64 string

The encrypted string base64

Key byte[]

The key need to be use for decryption

IV byte[]

The iv need to be use for decryption

Retour

string

Exemples

string decryptedString =  DecryptStringFromBytes("SSBuZWVkIHRvIGJlIGRlY3J5cHRlZD8gWW91IGFyZSByaWdodCwgdGhhdCB3YXMgbm90IHJlYWxseSBlbmNyeXB0ZWQhISA6LSk=", Key, IV);

DecryptStringFromBase64(string, string, string)

Decrypts a Base64 encoded encrypted string using Base64 encoded key and IV.

public static string DecryptStringFromBase64(string b64, string Base64Key, string Base64IV)

Paramètres

b64 string

The Base64 encoded encrypted string.

Base64Key string

The Base64 encoded AES key.

Base64IV string

The Base64 encoded AES initialization vector.

Retour

string

The decrypted plain text string.

Exemples

string decrypted = CryptoHelper.DecryptStringFromBase64(encryptedB64, base64Key, base64IV);

DecryptStringFromBytes(byte[], byte[], byte[])

Decrypt binaryText

public static string DecryptStringFromBytes(byte[] cipherText, byte[] Key, byte[] IV)

Paramètres

cipherText byte[]

The encrypted binary data

Key byte[]

The key need to be used for decryption

IV byte[]

The iv need to be used for decryption

Retour

string

return a string unencrypted of a string encrypted

Exemples

DecryptStringFromBytes(Convert.FromBase64String(b64), Key, IV);

EncryptBinary(byte[], byte[], byte[], PaddingMode)

Use to encrypt with AES a byte array

public static byte[] EncryptBinary(byte[] binary, byte[] Key, byte[] IV, PaddingMode paddingMode = PaddingMode.PKCS7)

Paramètres

binary byte[]

Binary need to be encrypted

Key byte[]

The key of the encryption

IV byte[]

The iv of the encryption

paddingMode PaddingMode

Retour

byte[]

Return the crypted bytes array

Exemples

byte[] cryptedBinary = CryptoHelper.EncryptBinary(Binary, Key, IV);

EncryptFile(string, string, byte[], byte[])

Use to encrypt file destination will be override

public static void EncryptFile(string sourceFilename, string destinationFilename, byte[] Key, byte[] IV)

Paramètres

sourceFilename string
destinationFilename string
Key byte[]
IV byte[]

EncryptRSAContent(string, byte[], bool)

Encrypt a byte array with a public key

public static byte[] EncryptRSAContent(string publicKey, byte[] content, bool fOAEP)

Paramètres

publicKey string

The xml public key need to be use

content byte[]

The string need to be encrypted

fOAEP bool

true to perform direct System.Security.Cryptography.RSA encryption using OAEP padding; otherwise, false to use PCKS#1.5 padding

Retour

byte[]

Byte array of the byte array, encrypted with the public key

Exemples

byte[] cryptedString = EncryptRSAContent(publicKey, contentBinary, true);

EncryptRSAContent(string, string)

Encrypt RSA content with xml public key

public static byte[] EncryptRSAContent(string publicKey, string content)

Paramètres

publicKey string

Public key

content string

The string need to be encrypted

Retour

byte[]

Encrypted string

Exemples

byte[] encryptedData = EncryptRSAContent(publicKey, content);

EncryptRSAContent(string, string, bool)

Encrypt a string with a public key

public static byte[] EncryptRSAContent(string publicKey, string content, bool fOAEP)

Paramètres

publicKey string

The xml public key need to be use

content string

The string need to be encrypted System.Text.Encoding.Default

fOAEP bool

true to perform direct System.Security.Cryptography.RSA encryption using OAEP padding; otherwise, false to use PCKS#1.5 padding

Retour

byte[]

Byte array of the string, encrypted with the public key

Exemples

byte[] cryptedString = EncryptRSAContent(publicKey, contentString, true);

EncryptSecfFile(string, byte[], byte[])

Use to encrypt file destination will be override

public static bool EncryptSecfFile(string sourceFilename, byte[] Key, byte[] IV)

Paramètres

sourceFilename string
Key byte[]
IV byte[]

Retour

bool

EncryptSecfFile(string, string, byte[], byte[])

Use to encrypt file destination will be override

public static bool EncryptSecfFile(string sourceFilename, string destinationDirectory, byte[] Key, byte[] IV)

Paramètres

sourceFilename string
destinationDirectory string
Key byte[]
IV byte[]

Retour

bool

EncryptStream(Stream, Stream, byte[], byte[], PaddingMode)

Encrypt binary by using stream to encrypt large file

public static void EncryptStream(Stream inputStream, Stream outputStream, byte[] Key, byte[] IV, PaddingMode paddingMode = PaddingMode.PKCS7)

Paramètres

inputStream Stream

The input stream

outputStream Stream

The outpout stream. ATTENTION THE CALLER MUST CLOSE THE STREAM

Key byte[]

The key

IV byte[]

The IV

paddingMode PaddingMode

The padding mode

Exceptions

ArgumentNullException

EncryptStringToB64(string, byte[], byte[])

Use to encrypt a string to UTF8 base 64

public static string EncryptStringToB64(string plainText, byte[] Key, byte[] IV)

Paramètres

plainText string

Text to encrypt

Key byte[]

The key use to encrypt

IV byte[]

The Iv use to encrypt

Retour

string

The base64 string

Exemples

string cryptedString = CryptoHelper.EncryptStringToB64("I need encryption for this string", KEY, IV);

EncryptStringToBytes(string, byte[], byte[])

Use to encrypt plainText UTF8 to byte array

public static byte[] EncryptStringToBytes(string plainText, byte[] Key, byte[] IV)

Paramètres

plainText string
Key byte[]
IV byte[]

Retour

byte[]

A byte array encrypted. The plainText must be in UTF-8

Exemples

byte[] encrypted = CryptoHelper.EncryptStringToBytes(plainText, Key, IV);

EnncryptRSAContentToBase64String(string, string)

Encrypt with public key a string. Use RSA crypto provider

public static string EnncryptRSAContentToBase64String(string publicKey, string content)

Paramètres

publicKey string

The public XML public key need to be used

content string

The content to encrypt

Retour

string

Base64 encrypted string

Exemples

string encryptedString =  CryptoHelper.EnncryptRSAContentToBase64String(PublicKey, Content);

FillSecureRandomByteArray(byte[])

Fill a array with random strong value

public static void FillSecureRandomByteArray(byte[] data)

Paramètres

data byte[]

Array to fill

GenerateAESKeys()

Generate a new AES keys

public static Keys GenerateAESKeys()

Retour

Keys

GenerateBase64KeyAndIV(out string, out string)

Generate 256 bits base 64 key and base 64 iv key with Aes.

public static void GenerateBase64KeyAndIV(out string b64Key, out string b64iv)

Paramètres

b64Key string
b64iv string

GenerateSecureRandomByteArray(int)

That method will return a array fill with SecureRandomValue

public static byte[] GenerateSecureRandomByteArray(int length)

Paramètres

length int

Retour

byte[]

GetKeyFromSHA512(byte[])

From a SHA512 return a 32 bit keys

public static byte[] GetKeyFromSHA512(byte[] hash)

Paramètres

hash byte[]

Retour

byte[]

GetMD5Bytes(string)

Computes the MD5 hash of a UTF-8 encoded string and returns it as a byte array.

public static byte[] GetMD5Bytes(string plainText)

Paramètres

plainText string

The UTF-8 string to hash.

Retour

byte[]

The MD5 hash as a byte array, or null if the input is null or empty.

Exemples

byte[] hashBytes = CryptoHelper.GetMD5Bytes("Hello World");

GetMD5HashOfBytes(byte[])

Return a MD5 Hexa string of the hash

public static string GetMD5HashOfBytes(byte[] b)

Paramètres

b byte[]

Source binary that need to get the Hash

Retour

string

MD5 128 bit hexa hash

Exemples

string fileHash = CryptoHelper.GetMD5HashOfBytes(Binary);

GetMD5HashOfFile(string)

Return a MD5 hash of a file

public static string GetMD5HashOfFile(string filePath)

Paramètres

filePath string

Path to the file

Retour

string

HEXA MD5 string file

Exemples

string hash = CryptoHelper.GetMD5HashOfFile(myfile.FileName);

GetMD5HashOfStream(Stream)

Get the Hexa MD5 string of a stream

public static string GetMD5HashOfStream(Stream stream)

Paramètres

stream Stream

Retour

string

Exemples

using (var stream = new BufferedStream(File.OpenRead(filePath), 3145728))
   {
     return GetMD5HashOfStream(stream);
 }

GetMD5HashOfString(string)

Return the MD5 hash of a string

public static string GetMD5HashOfString(string stringToHash)

Paramètres

stringToHash string

UTF-8 string

Retour

string

HEXA string of the Hash

Exemples

string hash = CryptoHelper.GetMD5HashOfString(content);

GetPublicKeyFromPrivateKey(string)

Return the public RSA key of a private key. the public key will be in xml format

public static string GetPublicKeyFromPrivateKey(string privateKey)

Paramètres

privateKey string

XML private key to extract the public key

Retour

string

The xml public key of the private key

GetPublicKeyFromPrivateXmlOrPEM(string)

public static RSAKeys GetPublicKeyFromPrivateXmlOrPEM(string privateKey)

Paramètres

privateKey string

Retour

RSAKeys

GetPublicKeySize(string)

Get the key size of a public key

public static int GetPublicKeySize(string publicKey)

Paramètres

publicKey string

Retour

int

GetSHA256Bytes(byte[])

REturn the hash of a SHA256 of bytes

public static byte[] GetSHA256Bytes(byte[] bytes)

Paramètres

bytes byte[]

Retour

byte[]

GetSHA256Bytes(string)

Get the SHA 256 of UTF-8 string

public static byte[] GetSHA256Bytes(string plainText)

Paramètres

plainText string

Retour

byte[]

GetSHA256HashOfBytes(byte[])

Get the HEX hash of bytes

public static string GetSHA256HashOfBytes(byte[] b)

Paramètres

b byte[]

Retour

string

GetSHA256HashOfString(string)

Get the HEX hash of a string utf8

public static string GetSHA256HashOfString(string b)

Paramètres

b string

Retour

string

GetSHA256OfFile(string)

Get the HEX hash of file

public static string GetSHA256OfFile(string filePath)

Paramètres

filePath string

Retour

string

GetSHA256OfStream(Stream)

Get the HEX hash of stream

public static string GetSHA256OfStream(Stream stream)

Paramètres

stream Stream

Retour

string

GetSHA512Bytes(byte[])

Return the hash of a SHA 512 of bytes

public static byte[] GetSHA512Bytes(byte[] bytes)

Paramètres

bytes byte[]

Retour

byte[]

GetSHA512Bytes(string)

Get SHA 512 of UTF-8 string

public static byte[] GetSHA512Bytes(string plainText)

Paramètres

plainText string

Retour

byte[]

GetSHA512HashOfBytes(byte[])

Get the HEX hash of bytes

public static string GetSHA512HashOfBytes(byte[] b)

Paramètres

b byte[]

Retour

string

GetSHA512HashOfString(string)

Get the HEX hash of a string utf8

public static string GetSHA512HashOfString(string b)

Paramètres

b string

Retour

string

GetSHA512OfFile(string, bool)

Get the HEX hash of file

public static string GetSHA512OfFile(string filePath, bool fileOverOneGigabitHashIsSplit = true)

Paramètres

filePath string

The file path of a file

fileOverOneGigabitHashIsSplit bool

If the file is over that 1G, then the hash calculation is optimised with combinaison of 100Mb hash

Retour

string

GetSHA512OfStream(Stream)

Get the HEX hash of stream

public static string GetSHA512OfStream(Stream stream)

Paramètres

stream Stream

Retour

string

GetStrongKeyStoreNameFromAssemblyGuid()

public static string GetStrongKeyStoreNameFromAssemblyGuid()

Retour

string

HexToByteArray(string)

public static byte[] HexToByteArray(string hex)

Paramètres

hex string

Retour

byte[]

KeyAlreadyExistInMachineKeys(string)

public static bool KeyAlreadyExistInMachineKeys(string strongName)

Paramètres

strongName string

Retour

bool

RSADencryptWhitKeyContainerFromMachineKeys(byte[], string)

public static byte[] RSADencryptWhitKeyContainerFromMachineKeys(byte[] encrBytes, string containerName)

Paramètres

encrBytes byte[]
containerName string

Retour

byte[]

RSADencryptWhitKeyContainerFromMachineKeysToString(byte[], string)

public static string RSADencryptWhitKeyContainerFromMachineKeysToString(byte[] encrBytes, string containerName)

Paramètres

encrBytes byte[]
containerName string

Retour

string

RSAEncryptWhitKeyContainerMachineKeyStore(string, string)

public static byte[] RSAEncryptWhitKeyContainerMachineKeyStore(string message, string keyContainerName)

Paramètres

message string
keyContainerName string

Retour

byte[]

RSAEncryptWhitKeyContainerMachineKeyStoreToBase64(string, string)

public static string RSAEncryptWhitKeyContainerMachineKeyStoreToBase64(string message, string keyContainerName)

Paramètres

message string
keyContainerName string

Retour

string

SignDataWithPrivateKey(string, byte[])

Get the signature of a message signed with a private key. Always uses SHA512.

public static byte[] SignDataWithPrivateKey(string privateKey, byte[] cryptedData)

Paramètres

privateKey string
cryptedData byte[]

Retour

byte[]

SignDataWithPrivateKey(string, string)

return a base64 signature of the hash

public static string SignDataWithPrivateKey(string privateKey, string clearText)

Paramètres

privateKey string
clearText string

Retour

string

TransformXmlKeyToPEM(string)

That will transform a xml key to certificate

public static string TransformXmlKeyToPEM(string xmlKey)

Paramètres

xmlKey string

If we get the private key, the PEM will contain the private key if not only the public key

Retour

string

VerifySignature(string, byte[], byte[], string)

Vérifie la signature d'un échange clé publique privé

public static bool VerifySignature(string xmlPublicKey, byte[] signature, byte[] clearHashData, string hashAlgo = "SHA512")

Paramètres

xmlPublicKey string

La clé publique associer à la clé privé

signature byte[]

La signature en bytes

clearHashData byte[]

Le hash des données signers en bytes

hashAlgo string

l'alrorithme utiliser

Retour

bool

VerifySignature(string, string, string, string)

Vérifie la signature d'un échange clé publique privé

public static bool VerifySignature(string xmlPublicKey, string base64Signature, string clearBase64HashData, string hashAlgo = "SHA512")

Paramètres

xmlPublicKey string

La clé publique associer à la clé privé

base64Signature string

La signature fait à partir de la clé privé en base 64

clearBase64HashData string

Le hash du message qui à été signer en base 64

hashAlgo string

l'algoritme utiliser

Retour

bool