Symmetric key encryption (Windows Runtime apps)
Symmetric key encryption, also called secret key encryption, requires that the key used for encryption also be used for decryption. You can use a SymmetricKeyAlgorithmProvider object to specify a symmetric algorithm and create or import a key. You can use static methods on the CryptographicEngine class to encrypt and decrypt data by using the algorithm and key.
Symmetric key encryption typically uses block ciphers and block cipher modes. A block cipher is a symmetric encryption function that operates on fixed size blocks. If the message you want to encrypt is longer than the block length, you must use a block cipher mode. A block cipher mode is a symmetric encryption function built by using a block cipher. It encrypts plaintext as a series of fixed size blocks. The following modes are supported for apps:
- The ECB (electronic codebook) mode encrypts each block of the message separately. This is not considered a secure encryption mode.
- The CBC (cipher block chaining) mode uses the previous ciphertext block to obfuscate the current block. You must determine what value to use for the first block. This value is called the initialization vector (IV).
- The CCM (counter with CBC-MAC) mode combines the CBC block cipher mode with a message authentication code (MAC).
- The GCM (Galois counter mode) mode combines the counter encryption mode with the Galois authentication mode.
Some modes such as CBC require that you use an initialization vector (IV) for the first ciphertext block. The following are common initialization vectors. You specify the IV when calling the Encrypt method on the CryptographicEngine class. For most cases it is important that the IV never be reused with the same key.
- Fixed uses the same IV for all messages to be encrypted. This leaks information and its use is not recommended.
- Counter increments the IV for each block.
- Random creates a pseudorandom IV. You can use the GenerateRandom method on the CryptographicBuffer class to create the IV.
- Nonce-Generated uses a unique number for each message to be encrypted. Typically, the nonce is a modified message or transaction identifier. The nonce does not have to be kept secret, but it should never be reused under the same key.
Most modes require that the length of the plaintext be an exact multiple of the block size. This usually requires that you pad the plaintext to obtain the appropriate length.
While block ciphers encrypt fixed size blocks of data, stream ciphers are symmetric encryption functions that combine plaintext bits with a pseudorandom bit stream (called a key stream) to generate the ciphertext. Some block cipher modes such as output feedback mode (OTF) and counter mode (CTR) effectively turn a block cipher into a stream cipher. Actual stream ciphers such as RC4, however, typically operate at higher speeds than block cipher modes are capable of achieving.