C# Class GSF.Security.Cryptography.Cipher

Provides general use cryptographic functions.
This class exists to simplify usage of basic cryptography functionality.
Exibir arquivo Open project: GridProtectionAlliance/gsf

Public Methods

Method Description
Decrypt ( this source, byte key, byte iv, CipherStrength strength ) : MemoryStream

Returns a stream of decrypted data for the given parameters.

This returns a memory stream of the decrypted results, if the incoming stream is very large this will consume a large amount of memory. In this case use the overload that takes the destination stream as a parameter instead.

Decrypt ( this source, byte key, byte iv, CipherStrength strength ) : byte[]

Returns a binary array of decrypted data for the given parameters.

Decrypt ( this source, int startIndex, int length, byte key, byte iv, CipherStrength strength ) : byte[]

Returns a binary array of decrypted data for the given parameters.

Decrypt ( this source, int startIndex, int length, string password, CipherStrength strength ) : byte[]

Returns a binary array of decrypted data for the given parameters.

Decrypt ( this source, string password, CipherStrength strength ) : byte[]

Returns a binary array of decrypted data for the given parameters.

Decrypt ( this source, string password, CipherStrength strength ) : string

Returns a decrypted string from a Base64 encoded string of binary encrypted data from the given parameters.

Decrypt ( this source, Stream destination, byte key, byte iv, CipherStrength strength, Action progressHandler ) : void

Decrypts input stream onto output stream for the given parameters.

DecryptFile ( string sourceFileName, string destinationFileName, string password, CipherStrength strength, Action progressHandler ) : void

Creates a decrypted file from source file data.

Encrypt ( this source, byte key, byte iv, CipherStrength strength ) : MemoryStream

Returns a stream of encrypted data for the given parameters.

This returns a memory stream of the encrypted results, if the incoming stream is very large this will consume a large amount of memory. In this case use the overload that takes the destination stream as a parameter instead.

Encrypt ( this source, byte key, byte iv, CipherStrength strength ) : byte[]

Returns a binary array of encrypted data for the given parameters.

Encrypt ( this source, int startIndex, int length, byte key, byte iv, CipherStrength strength ) : byte[]

Returns a binary array of encrypted data for the given parameters.

Encrypt ( this source, int startIndex, int length, string password, CipherStrength strength ) : byte[]

Returns a binary array of encrypted data for the given parameters.

Encrypt ( this source, string password, CipherStrength strength ) : byte[]

Returns a binary array of encrypted data for the given parameters.

Encrypt ( this source, string password, CipherStrength strength ) : string

Returns a Base64 encoded string of the returned binary array of the encrypted data, generated with the given parameters.

Encrypt ( this source, Stream destination, byte key, byte iv, CipherStrength strength, Action progressHandler ) : void

Encrypts input stream onto output stream for the given parameters.

EncryptFile ( string sourceFileName, string destinationFileName, string password, CipherStrength strength, Action progressHandler ) : void

Creates an encrypted file from source file data.

ExportKeyIV ( string password, int keySize ) : string

Exports a key and initialization vector from the local system key cache.

This method is used to manually export a key to be installed on another computer.

FlushCache ( int millisecondsTimeout = Timeout.Infinite ) : void

Blocks current thread and waits for any pending save of local system key cache to complete.

This method only needs to be used if crypto cache changes could be pending during application shutdown (i.e., executing ciphers with new keys that have not been saved, using existing keys does not queue crypto cache updates) to ensure keys are flushed before exit.

For most applications it is expected that this method would be rarely needed. However, possible usage scenarios would include:
Writing an application that establishes crypto keys where application lifetime would be very short (i.e., run, create keys, exit). Creating new crypto keys during application shutdown (i.e., performing ciphers with non-existing keys at shutdown).

GetPasswordHash ( string password, int categoryID ) : string

Gets the Base64 encoded SHA-256 hash of given user password.

The optional categoryID will be appended to the password to allow the same password to be used in different contexts and return different results, when useful.

ImportKeyIV ( string password, int keySize, string keyIVText ) : void

Imports a key and initialization vector into the local system key cache.

This method is used to manually import a key created on another computer.

KeyIVExists ( string password, int keySize ) : bool

Determines if a key and initialization vector exists for the given password in the local system key cache.

ReloadCache ( ) : void

Manually loads keys into the local system key cache.

Private Methods

Method Description
Cipher ( ) : System

Static constructor for the Cipher class.

Method Details

Decrypt() public static method

Returns a stream of decrypted data for the given parameters.
This returns a memory stream of the decrypted results, if the incoming stream is very large this will consume a large amount of memory. In this case use the overload that takes the destination stream as a parameter instead.
public static Decrypt ( this source, byte key, byte iv, CipherStrength strength ) : MemoryStream
source this Source stream that contains data to decrypt.
key byte Encryption key to use to decrypt stream.
iv byte Initialization vector to use to decrypt stream.
strength CipherStrength Cryptographic strength to use when decrypting stream.
return System.IO.MemoryStream

Decrypt() public static method

Returns a binary array of decrypted data for the given parameters.
public static Decrypt ( this source, byte key, byte iv, CipherStrength strength ) : byte[]
source this Binary array of data to decrypt.
key byte Encryption key to use to decrypt data.
iv byte Initialization vector to use to decrypt data.
strength CipherStrength Cryptographic strength to use when decrypting data.
return byte[]

Decrypt() public static method

Returns a binary array of decrypted data for the given parameters.
public static Decrypt ( this source, int startIndex, int length, byte key, byte iv, CipherStrength strength ) : byte[]
source this Binary array of data to decrypt.
startIndex int Offset into buffer.
length int Number of bytes in buffer to decrypt starting from offset.
key byte Encryption key to use to decrypt data.
iv byte Initialization vector to use to decrypt data.
strength CipherStrength Cryptographic strength to use when decrypting data.
return byte[]

Decrypt() public static method

Returns a binary array of decrypted data for the given parameters.
public static Decrypt ( this source, int startIndex, int length, string password, CipherStrength strength ) : byte[]
source this Binary array of data to decrypt.
startIndex int Offset into buffer.
length int Number of bytes in buffer to decrypt starting from offset.
password string User password used for key lookup.
strength CipherStrength Cryptographic strength to use when decrypting data.
return byte[]

Decrypt() public static method

Returns a binary array of decrypted data for the given parameters.
public static Decrypt ( this source, string password, CipherStrength strength ) : byte[]
source this Binary array of data to decrypt.
password string User password used for key lookup.
strength CipherStrength Cryptographic strength to use when decrypting data.
return byte[]

Decrypt() public static method

Returns a decrypted string from a Base64 encoded string of binary encrypted data from the given parameters.
public static Decrypt ( this source, string password, CipherStrength strength ) : string
source this Source string to decrypt.
password string User password used for key lookup.
strength CipherStrength Cryptographic strength to use when decrypting string.
return string

Decrypt() public static method

Decrypts input stream onto output stream for the given parameters.
public static Decrypt ( this source, Stream destination, byte key, byte iv, CipherStrength strength, Action progressHandler ) : void
source this Source stream that contains data to decrypt.
destination Stream Destination stream used to hold decrypted data.
key byte Encryption key to use to decrypt stream.
iv byte Initialization vector to use to decrypt stream.
strength CipherStrength Cryptographic strength to use when decrypting stream.
progressHandler Action Optional delegate to handle progress updates for decrypting large streams.
return void

DecryptFile() public static method

Creates a decrypted file from source file data.
public static DecryptFile ( string sourceFileName, string destinationFileName, string password, CipherStrength strength, Action progressHandler ) : void
sourceFileName string Source file name.
destinationFileName string Destination file name.
password string User password used for key lookup.
strength CipherStrength Cryptographic strength to use when decrypting file.
progressHandler Action Optional delegate to handle progress updates for decrypting large files.
return void

Encrypt() public static method

Returns a stream of encrypted data for the given parameters.
This returns a memory stream of the encrypted results, if the incoming stream is very large this will consume a large amount of memory. In this case use the overload that takes the destination stream as a parameter instead.
public static Encrypt ( this source, byte key, byte iv, CipherStrength strength ) : MemoryStream
source this Source stream that contains data to encrypt.
key byte Encryption key to use to encrypt stream.
iv byte Initialization vector to use to encrypt stream.
strength CipherStrength Cryptographic strength to use when encrypting stream.
return System.IO.MemoryStream

Encrypt() public static method

Returns a binary array of encrypted data for the given parameters.
public static Encrypt ( this source, byte key, byte iv, CipherStrength strength ) : byte[]
source this Binary array of data to encrypt.
key byte Encryption key to use to encrypt data.
iv byte Initialization vector to use to encrypt data.
strength CipherStrength Cryptographic strength to use when encrypting data.
return byte[]

Encrypt() public static method

Returns a binary array of encrypted data for the given parameters.
public static Encrypt ( this source, int startIndex, int length, byte key, byte iv, CipherStrength strength ) : byte[]
source this Binary array of data to encrypt.
startIndex int Offset into buffer.
length int Number of bytes in buffer to encrypt starting from offset.
key byte Encryption key to use to encrypt data.
iv byte Initialization vector to use to encrypt data.
strength CipherStrength Cryptographic strength to use when encrypting data.
return byte[]

Encrypt() public static method

Returns a binary array of encrypted data for the given parameters.
public static Encrypt ( this source, int startIndex, int length, string password, CipherStrength strength ) : byte[]
source this Binary array of data to encrypt.
startIndex int Offset into buffer.
length int Number of bytes in buffer to encrypt starting from offset.
password string User password used for key lookup.
strength CipherStrength Cryptographic strength to use when encrypting data.
return byte[]

Encrypt() public static method

Returns a binary array of encrypted data for the given parameters.
public static Encrypt ( this source, string password, CipherStrength strength ) : byte[]
source this Binary array of data to encrypt.
password string User password used for key lookup.
strength CipherStrength Cryptographic strength to use when encrypting data.
return byte[]

Encrypt() public static method

Returns a Base64 encoded string of the returned binary array of the encrypted data, generated with the given parameters.
public static Encrypt ( this source, string password, CipherStrength strength ) : string
source this Source string to encrypt.
password string User password used for key lookup.
strength CipherStrength Cryptographic strength to use when encrypting string.
return string

Encrypt() public static method

Encrypts input stream onto output stream for the given parameters.
public static Encrypt ( this source, Stream destination, byte key, byte iv, CipherStrength strength, Action progressHandler ) : void
source this Source stream that contains data to encrypt.
destination Stream Destination stream used to hold encrypted data.
key byte Encryption key to use to encrypt stream.
iv byte Initialization vector to use to encrypt stream.
strength CipherStrength Cryptographic strength to use when encrypting stream.
progressHandler Action Optional delegate to handle progress updates for encrypting large streams.
return void

EncryptFile() public static method

Creates an encrypted file from source file data.
public static EncryptFile ( string sourceFileName, string destinationFileName, string password, CipherStrength strength, Action progressHandler ) : void
sourceFileName string Source file name.
destinationFileName string Destination file name.
password string User password used for key lookup.
strength CipherStrength Cryptographic strength to use when encrypting file.
progressHandler Action Optional delegate to handle progress updates for encrypting large files.
return void

ExportKeyIV() public static method

Exports a key and initialization vector from the local system key cache.
This method is used to manually export a key to be installed on another computer.
public static ExportKeyIV ( string password, int keySize ) : string
password string User password used for key lookup.
keySize int Specifies the desired key size.
return string

FlushCache() public static method

Blocks current thread and waits for any pending save of local system key cache to complete.

This method only needs to be used if crypto cache changes could be pending during application shutdown (i.e., executing ciphers with new keys that have not been saved, using existing keys does not queue crypto cache updates) to ensure keys are flushed before exit.

For most applications it is expected that this method would be rarely needed. However, possible usage scenarios would include:
Writing an application that establishes crypto keys where application lifetime would be very short (i.e., run, create keys, exit). Creating new crypto keys during application shutdown (i.e., performing ciphers with non-existing keys at shutdown).

public static FlushCache ( int millisecondsTimeout = Timeout.Infinite ) : void
millisecondsTimeout int The number of milliseconds to wait, or (-1) to wait indefinitely.
return void

GetPasswordHash() public static method

Gets the Base64 encoded SHA-256 hash of given user password.
The optional categoryID will be appended to the password to allow the same password to be used in different contexts and return different results, when useful.
public static GetPasswordHash ( string password, int categoryID ) : string
password string User password to get hash for.
categoryID int Specifies the desired category ID.
return string

ImportKeyIV() public static method

Imports a key and initialization vector into the local system key cache.
This method is used to manually import a key created on another computer.
public static ImportKeyIV ( string password, int keySize, string keyIVText ) : void
password string User password used for key lookups.
keySize int Specifies the desired key size.
keyIVText string Text based key and initialization vector to import into local key cache.
return void

KeyIVExists() public static method

Determines if a key and initialization vector exists for the given password in the local system key cache.
public static KeyIVExists ( string password, int keySize ) : bool
password string User password used for key lookups.
keySize int Specifies the desired key size.
return bool

ReloadCache() public static method

Manually loads keys into the local system key cache.
public static ReloadCache ( ) : void
return void