C# Class Westwind.Utilities.Encryption

Class that provides a number of encryption utilities
显示文件 Open project: RickStrahl/WestwindToolkit

Public Properties

Property Type Description
EncryptionKey string

Public Methods

Method Description
BinHexToBinary ( string hex ) : byte[]

Turns a BinHex string that contains raw byte values into a byte array

BinaryToBinHex ( byte data ) : string

Converts a byte array into a BinHex string. Example: 01552233 where the numbers are packed byte values.

ComputeHash ( byte byteData, string hashAlgorithm, byte saltBytes, bool useBinHex = false ) : string

Generates a hash for the given plain text value and returns a base64-encoded result. Before the hash is computed, a random salt is generated and appended to the plain text. This salt is stored at the end of the hash value, so it can be used later for hash verification.

ComputeHash ( string plainText, string hashAlgorithm, byte saltBytes, bool useBinHex = false ) : string

Generates a hash for the given plain text value and returns a base64-encoded result. Before the hash is computed, a random salt is generated and appended to the plain text. This salt is stored at the end of the hash value, so it can be used later for hash verification.

ComputeHash ( string plainText, string hashAlgorithm, string salt, bool useBinHex = false ) : string

Generates a hash for the given plain text value and returns a base64-encoded result. Before the hash is computed, a random salt is generated and appended to the plain text. This salt is stored at the end of the hash value, so it can be used later for hash verification.

DecryptBytes ( byte decryptBuffer, byte encryptionKey ) : byte[]

Decrypts a byte buffer with a byte based encryption key

DecryptBytes ( byte decryptBuffer, string encryptionKey ) : byte[]

Decrypts a Byte array from DES with an Encryption Key.

DecryptBytes ( string decryptString, string encryptionKey, bool useBinHex = false ) : byte[]

Decrypts a string using DES encryption and a pass key that was used for encryption and returns a byte buffer.

DecryptString ( string decryptString, byte encryptionKey, bool useBinHex = false ) : string

Decrypts a string using DES encryption and a pass key that was used for encryption. Class wwEncrypt

DecryptString ( string decryptString, string encryptionKey, bool useBinHex = false ) : string

Decrypts a string using DES encryption and a pass key that was used for encryption. Class wwEncrypt

EncryptBytes ( byte inputBytes, byte encryptionKey ) : byte[]

Encrypts a byte buffer with a byte encryption key

EncryptBytes ( byte inputBytes, string encryptionKey ) : byte[]

Encodes a stream of bytes using DES encryption with a pass key. Lowest level method that handles all work.

EncryptBytes ( string inputString, string encryptionKey ) : byte[]

Encrypts a string into bytes using DES encryption with a Passkey.

EncryptString ( string inputString, byte encryptionKey, bool useBinHex = false ) : string

Encrypts a string using Triple DES encryption with a two way encryption key.String is returned as Base64 or BinHex encoded value rather than binary.

EncryptString ( string inputString, string encryptionKey, bool useBinHex = false ) : string

Encrypts a string using Triple DES encryption with a two way encryption key.String is returned as Base64 encoded value rather than binary.

GZipFile ( string Filename, string OutputFile ) : bool

Encodes one file to another file that is gzip compressed. File is overwritten if it exists and not locked.

GZipMemory ( byte buffer ) : byte[]

GZip encodes a memory buffer to a compressed memory buffer

GZipMemory ( string Input ) : byte[]

Encodes a string to a gzip compressed memory buffer

GZipMemory ( string Filename, bool IsFile ) : byte[]

Encodes a file to a gzip memory buffer

GetChecksumFromBytes ( byte fileData, string mode ) : string

Create a SHA256 or MD5 checksum from a bunch of bytes

GetChecksumFromFile ( string file, string mode, bool useBinHex = false ) : string

Creates an SHA256 or MD5 checksum of a file

ProtectBytes ( byte encryptBytes, byte key, DataProtectionScope scope = DataProtectionScope.LocalMachine ) : byte[]

Encrypt bytes using the Data Protection API on Windows. This API uses internal keys to encrypt data which is valid for decryption only on the same machine. This is an idea storage mechanism for application registraions, service passwords and other semi-transient data that is specific to the software used on the current machine

DO NOT USE FOR DATA THAT WILL CROSS MACHINE BOUNDARIES

ProtectBytes ( byte encryptBytes, string key, DataProtectionScope scope = DataProtectionScope.LocalMachine ) : byte[]

Encrypt bytes using the Data Protection API on Windows. This API uses internal keys to encrypt data which is valid for decryption only on the same machine. This is an idea storage mechanism for application registraions, service passwords and other semi-transient data that is specific to the software used on the current machine

DO NOT USE FOR DATA THAT WILL CROSS MACHINE BOUNDARIES

ProtectString ( string encryptString, byte key, DataProtectionScope scope = DataProtectionScope.LocalMachine, bool useBinHex = false ) : string

Encrypt bytes using the Data Protection API on Windows. This API uses internal keys to encrypt data which is valid for decryption only on the same machine. This is an idea storage mechanism for application registraions, service passwords and other semi-transient data that is specific to the software used on the current machine

DO NOT USE FOR DATA THAT WILL CROSS MACHINE BOUNDARIES

ProtectString ( string encryptString, string key, DataProtectionScope scope = DataProtectionScope.LocalMachine, bool useBinHex = false ) : string

Encrypt bytes using the Data Protection API on Windows. This API uses internal keys to encrypt data which is valid for decryption only on the same machine. This is an idea storage mechanism for application registraions, service passwords and other semi-transient data that is specific to the software used on the current machine

DO NOT USE FOR DATA THAT WILL CROSS MACHINE BOUNDARIES

UnprotectBytes ( byte encryptBytes, byte key, DataProtectionScope scope = DataProtectionScope.LocalMachine ) : byte[]

Decrypts bytes using the Data Protection API on Windows. This API uses internal keys to encrypt data which is valid for decryption only on the same machine. This is an idea storage mechanism for application registraions, service passwords and other semi-transient data that is specific to the software used on the current machine

DO NOT USE FOR DATA THAT WILL CROSS MACHINE BOUNDARIES

UnprotectBytes ( byte encryptBytes, string key, DataProtectionScope scope = DataProtectionScope.LocalMachine ) : byte[]

Encrypt bytes using the Data Protection API on Windows. This API uses internal keys to encrypt data which is valid for decryption only on the same machine. This is an idea storage mechanism for application registraions, service passwords and other semi-transient data that is specific to the software used on the current machine

DO NOT USE FOR DATA THAT WILL CROSS MACHINE BOUNDARIES

UnprotectString ( string encryptString, byte key, DataProtectionScope scope = DataProtectionScope.LocalMachine, bool useBinHex = false ) : string

Encrypt bytes using the Data Protection API on Windows. This API uses internal keys to encrypt data which is valid for decryption only on the same machine. This is an idea storage mechanism for application registraions, service passwords and other semi-transient data that is specific to the software used on the current machine

DO NOT USE FOR DATA THAT WILL CROSS MACHINE BOUNDARIES

UnprotectString ( string encryptString, string key, DataProtectionScope scope = DataProtectionScope.LocalMachine, bool useBinHex = false ) : string

Encrypt bytes using the Data Protection API on Windows. This API uses internal keys to encrypt data which is valid for decryption only on the same machine. This is an idea storage mechanism for application registraions, service passwords and other semi-transient data that is specific to the software used on the current machine

DO NOT USE FOR DATA THAT WILL CROSS MACHINE BOUNDARIES

Private Methods

Method Description
ParseHexChar ( char c ) : int

Method Details

BinHexToBinary() public static method

Turns a BinHex string that contains raw byte values into a byte array
public static BinHexToBinary ( string hex ) : byte[]
hex string BinHex string (011a031f) just two byte hex digits strung together)
return byte[]

BinaryToBinHex() public static method

Converts a byte array into a BinHex string. Example: 01552233 where the numbers are packed byte values.
public static BinaryToBinHex ( byte data ) : string
data byte Raw data to send
return string

ComputeHash() public static method

Generates a hash for the given plain text value and returns a base64-encoded result. Before the hash is computed, a random salt is generated and appended to the plain text. This salt is stored at the end of the hash value, so it can be used later for hash verification.
public static ComputeHash ( byte byteData, string hashAlgorithm, byte saltBytes, bool useBinHex = false ) : string
byteData byte /// Plaintext value to be hashed. ///
hashAlgorithm string /// Name of the hash algorithm. Allowed values are: "MD5", "SHA1", /// "SHA256", "SHA384", "SHA512", "HMACMD5", "HMACSHA1", "HMACSHA256", /// "HMACSHA512" (if any other value is specified MD5 will be used). /// /// HMAC algorithms uses Hash-based Message Authentication Code. /// The HMAC process mixes a secret key with the message data, hashes /// the result with the hash function, mixes that hash value with /// the secret key again, and then applies the hash function /// a second time. HMAC hashes are fixed lenght and generally /// much longer than non-HMAC hashes of the same type. /// /// https://msdn.microsoft.com/en-us/library/system.security.cryptography.hmacsha256(v=vs.110).aspx /// /// This value is case-insensitive. ///
saltBytes byte /// Optional but recommended salt bytes to apply to the hash. If not passed the /// raw encoding is used. If salt is nullthe raw algorithm is used (useful for /// file hashes etc.) HMAC versions REQUIRE that salt is passed. ///
useBinHex bool if true returns the data as BinHex byte pair string. Otherwise Base64 is returned.
return string

ComputeHash() public static method

Generates a hash for the given plain text value and returns a base64-encoded result. Before the hash is computed, a random salt is generated and appended to the plain text. This salt is stored at the end of the hash value, so it can be used later for hash verification.
public static ComputeHash ( string plainText, string hashAlgorithm, byte saltBytes, bool useBinHex = false ) : string
plainText string /// Plaintext value to be hashed. ///
hashAlgorithm string /// Name of the hash algorithm. Allowed values are: "MD5", "SHA1", /// "SHA256", "SHA384", "SHA512", "HMACMD5", "HMACSHA1", "HMACSHA256", /// "HMACSHA512" (if any other value is specified MD5 will be used). /// /// HMAC algorithms uses Hash-based Message Authentication Code. /// The HMAC process mixes a secret key with the message data, hashes /// the result with the hash function, mixes that hash value with /// the secret key again, and then applies the hash function /// a second time. HMAC hashes are fixed lenght and generally /// much longer than non-HMAC hashes of the same type. /// /// https://msdn.microsoft.com/en-us/library/system.security.cryptography.hmacsha256(v=vs.110).aspx /// /// This value is case-insensitive. ///
saltBytes byte
useBinHex bool if true returns the data as BinHex byte pair string. Otherwise Base64 is returned.
return string

ComputeHash() public static method

Generates a hash for the given plain text value and returns a base64-encoded result. Before the hash is computed, a random salt is generated and appended to the plain text. This salt is stored at the end of the hash value, so it can be used later for hash verification.
public static ComputeHash ( string plainText, string hashAlgorithm, string salt, bool useBinHex = false ) : string
plainText string /// Plaintext value to be hashed. ///
hashAlgorithm string /// Name of the hash algorithm. Allowed values are: "MD5", "SHA1", /// "SHA256", "SHA384", "SHA512", "HMACMD5", "HMACSHA1", "HMACSHA256", /// "HMACSHA512" (if any other value is specified MD5 will be used). /// /// HMAC algorithms uses Hash-based Message Authentication Code. /// The HMAC process mixes a secret key with the message data, hashes /// the result with the hash function, mixes that hash value with /// the secret key again, and then applies the hash function /// a second time. HMAC hashes are fixed lenght and generally /// much longer than non-HMAC hashes of the same type. /// /// https://msdn.microsoft.com/en-us/library/system.security.cryptography.hmacsha256(v=vs.110).aspx /// /// This value is case-insensitive. ///
salt string /// Optional but recommended salt string to apply to the hash. If not passed the /// raw encoding is used. If salt is nullthe raw algorithm is used (useful for /// file hashes etc.) HMAC versions REQUIRE that salt is passed. ///
useBinHex bool if true returns the data as BinHex byte pair string. Otherwise Base64 is returned.
return string

DecryptBytes() public static method

Decrypts a byte buffer with a byte based encryption key
public static DecryptBytes ( byte decryptBuffer, byte encryptionKey ) : byte[]
decryptBuffer byte
encryptionKey byte
return byte[]

DecryptBytes() public static method

Decrypts a Byte array from DES with an Encryption Key.
public static DecryptBytes ( byte decryptBuffer, string encryptionKey ) : byte[]
decryptBuffer byte
encryptionKey string
return byte[]

DecryptBytes() public static method

Decrypts a string using DES encryption and a pass key that was used for encryption and returns a byte buffer.
public static DecryptBytes ( string decryptString, string encryptionKey, bool useBinHex = false ) : byte[]
decryptString string
encryptionKey string
useBinHex bool Returns data in useBinHex format (12afb1c3f1). Otherwise base64 is returned.
return byte[]

DecryptString() public static method

Decrypts a string using DES encryption and a pass key that was used for encryption. Class wwEncrypt
public static DecryptString ( string decryptString, byte encryptionKey, bool useBinHex = false ) : string
decryptString string
encryptionKey byte
useBinHex bool Returns data in useBinHex format (12afb1c3f1). Otherwise base64 is returned
return string

DecryptString() public static method

Decrypts a string using DES encryption and a pass key that was used for encryption. Class wwEncrypt
public static DecryptString ( string decryptString, string encryptionKey, bool useBinHex = false ) : string
decryptString string
encryptionKey string
useBinHex bool Returns data in useBinHex format (12afb1c3f1). Otherwise base64 is returned.
return string

EncryptBytes() public static method

Encrypts a byte buffer with a byte encryption key
public static EncryptBytes ( byte inputBytes, byte encryptionKey ) : byte[]
inputBytes byte
encryptionKey byte
return byte[]

EncryptBytes() public static method

Encodes a stream of bytes using DES encryption with a pass key. Lowest level method that handles all work.
public static EncryptBytes ( byte inputBytes, string encryptionKey ) : byte[]
inputBytes byte
encryptionKey string
return byte[]

EncryptBytes() public static method

Encrypts a string into bytes using DES encryption with a Passkey.
public static EncryptBytes ( string inputString, string encryptionKey ) : byte[]
inputString string
encryptionKey string
return byte[]

EncryptString() public static method

Encrypts a string using Triple DES encryption with a two way encryption key.String is returned as Base64 or BinHex encoded value rather than binary.
public static EncryptString ( string inputString, byte encryptionKey, bool useBinHex = false ) : string
inputString string
encryptionKey byte
useBinHex bool if true returns bin hex rather than base64
return string

EncryptString() public static method

Encrypts a string using Triple DES encryption with a two way encryption key.String is returned as Base64 encoded value rather than binary.
public static EncryptString ( string inputString, string encryptionKey, bool useBinHex = false ) : string
inputString string
encryptionKey string
useBinHex bool
return string

GZipFile() public static method

Encodes one file to another file that is gzip compressed. File is overwritten if it exists and not locked.
public static GZipFile ( string Filename, string OutputFile ) : bool
Filename string
OutputFile string
return bool

GZipMemory() public static method

GZip encodes a memory buffer to a compressed memory buffer
public static GZipMemory ( byte buffer ) : byte[]
buffer byte
return byte[]

GZipMemory() public static method

Encodes a string to a gzip compressed memory buffer
public static GZipMemory ( string Input ) : byte[]
Input string
return byte[]

GZipMemory() public static method

Encodes a file to a gzip memory buffer
public static GZipMemory ( string Filename, bool IsFile ) : byte[]
Filename string
IsFile bool
return byte[]

GetChecksumFromBytes() public static method

Create a SHA256 or MD5 checksum from a bunch of bytes
public static GetChecksumFromBytes ( byte fileData, string mode ) : string
fileData byte
mode string SHA256,SHA512,MD5
return string

GetChecksumFromFile() public static method

Creates an SHA256 or MD5 checksum of a file
public static GetChecksumFromFile ( string file, string mode, bool useBinHex = false ) : string
file string
mode string SHA256,SHA512,MD5
useBinHex bool
return string

ProtectBytes() public static method

Encrypt bytes using the Data Protection API on Windows. This API uses internal keys to encrypt data which is valid for decryption only on the same machine. This is an idea storage mechanism for application registraions, service passwords and other semi-transient data that is specific to the software used on the current machine
DO NOT USE FOR DATA THAT WILL CROSS MACHINE BOUNDARIES
public static ProtectBytes ( byte encryptBytes, byte key, DataProtectionScope scope = DataProtectionScope.LocalMachine ) : byte[]
encryptBytes byte
key byte
scope DataProtectionScope
return byte[]

ProtectBytes() public static method

Encrypt bytes using the Data Protection API on Windows. This API uses internal keys to encrypt data which is valid for decryption only on the same machine. This is an idea storage mechanism for application registraions, service passwords and other semi-transient data that is specific to the software used on the current machine
DO NOT USE FOR DATA THAT WILL CROSS MACHINE BOUNDARIES
public static ProtectBytes ( byte encryptBytes, string key, DataProtectionScope scope = DataProtectionScope.LocalMachine ) : byte[]
encryptBytes byte
key string
scope DataProtectionScope
return byte[]

ProtectString() public static method

Encrypt bytes using the Data Protection API on Windows. This API uses internal keys to encrypt data which is valid for decryption only on the same machine. This is an idea storage mechanism for application registraions, service passwords and other semi-transient data that is specific to the software used on the current machine
DO NOT USE FOR DATA THAT WILL CROSS MACHINE BOUNDARIES
public static ProtectString ( string encryptString, byte key, DataProtectionScope scope = DataProtectionScope.LocalMachine, bool useBinHex = false ) : string
encryptString string
key byte
scope DataProtectionScope
useBinHex bool returns bin hex data when set (010A0D10AF)
return string

ProtectString() public static method

Encrypt bytes using the Data Protection API on Windows. This API uses internal keys to encrypt data which is valid for decryption only on the same machine. This is an idea storage mechanism for application registraions, service passwords and other semi-transient data that is specific to the software used on the current machine
DO NOT USE FOR DATA THAT WILL CROSS MACHINE BOUNDARIES
public static ProtectString ( string encryptString, string key, DataProtectionScope scope = DataProtectionScope.LocalMachine, bool useBinHex = false ) : string
encryptString string
key string
scope DataProtectionScope
useBinHex bool returns bin hex data when set (010A0D10AF)
return string

UnprotectBytes() public static method

Decrypts bytes using the Data Protection API on Windows. This API uses internal keys to encrypt data which is valid for decryption only on the same machine. This is an idea storage mechanism for application registraions, service passwords and other semi-transient data that is specific to the software used on the current machine
DO NOT USE FOR DATA THAT WILL CROSS MACHINE BOUNDARIES
public static UnprotectBytes ( byte encryptBytes, byte key, DataProtectionScope scope = DataProtectionScope.LocalMachine ) : byte[]
encryptBytes byte
key byte
scope DataProtectionScope
return byte[]

UnprotectBytes() public static method

Encrypt bytes using the Data Protection API on Windows. This API uses internal keys to encrypt data which is valid for decryption only on the same machine. This is an idea storage mechanism for application registraions, service passwords and other semi-transient data that is specific to the software used on the current machine
DO NOT USE FOR DATA THAT WILL CROSS MACHINE BOUNDARIES
public static UnprotectBytes ( byte encryptBytes, string key, DataProtectionScope scope = DataProtectionScope.LocalMachine ) : byte[]
encryptBytes byte
key string
scope DataProtectionScope
return byte[]

UnprotectString() public static method

Encrypt bytes using the Data Protection API on Windows. This API uses internal keys to encrypt data which is valid for decryption only on the same machine. This is an idea storage mechanism for application registraions, service passwords and other semi-transient data that is specific to the software used on the current machine
DO NOT USE FOR DATA THAT WILL CROSS MACHINE BOUNDARIES
public static UnprotectString ( string encryptString, byte key, DataProtectionScope scope = DataProtectionScope.LocalMachine, bool useBinHex = false ) : string
encryptString string
key byte
scope DataProtectionScope
useBinHex bool returns bin hex data when set (010A0D10AF)
return string

UnprotectString() public static method

Encrypt bytes using the Data Protection API on Windows. This API uses internal keys to encrypt data which is valid for decryption only on the same machine. This is an idea storage mechanism for application registraions, service passwords and other semi-transient data that is specific to the software used on the current machine
DO NOT USE FOR DATA THAT WILL CROSS MACHINE BOUNDARIES
public static UnprotectString ( string encryptString, string key, DataProtectionScope scope = DataProtectionScope.LocalMachine, bool useBinHex = false ) : string
encryptString string
key string
scope DataProtectionScope
useBinHex bool returns bin hex data when set (010A0D10AF)
return string

Property Details

EncryptionKey public_oe static_oe property

Replace this value with some unique key of your own Best set this in your App start up in a Static constructor
public static string EncryptionKey
return string