51. Encrypt and decrypt strings

Before you can use the .NET Framework's cryptography tools to encrypt and decrypt strings (or files, arrays of bytes, or just about anything else), you need to perform some setup. The following method creates an ICryptoTransform object that you can use to encrypt or decrypt data:

// Prepare a cryptographic transformation for this password// and SymmetricAlgorithm.private static ICryptoTransform MakeCryptoTransform(    string password, bool doEncryption, SymmetricAlgorithm cryptoProvider){    // Find a valid key size for this provider.    int numKeyBits = FindKeySize(cryptoProvider);    Console.WriteLine($"Key size: {numKeyBits} bits");    // Get the block size for this provider. int blockSizeBits = cryptoProvider.BlockSize; ...

Get Improving your C# Skills now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.