50 likes | 173 Views
Encryption with Generated K eys. Encryption: Symmetric - The same that is used to encrypt the data is also used to decrypt the data. They are all "BLOCK CIPHERS" Take unencrypted data and break it into blocks of all the same size. Each block is encrypted. "CIPHER BLOCK CHAINING"
E N D
Encryption with Generated Keys. • Encryption: • Symmetric - • The same that is used to encrypt the data is also used to decrypt the data. • They are all "BLOCK CIPHERS" • Take unencrypted data and break it into blocks of all the same size. • Each block is encrypted. • "CIPHER BLOCK CHAINING" • Each Block is combined with the previous blocks encryption. • "INITIALIZATION VECTOR" (IV) • Data starting point • Must be stored like the key. • Use symmetric when the same application encrypts and decrypts the data. • .NET algorithms (System.Security.Cryptography, SymmetricAlgorithm class) • Data Encryption Standard (DES) • Triple Data Encryption Algorithm (3DES/TDEA) • RC2 • Rijndael/Advanced Encryption Standard (AES) • aesManaged classes
Encryption with Generated Keys. • Encryption: • Symmetric cont'd- • Generating Keys • RNGCryptoServiceProvider class (hashing) • Minimum and maximum key sizes (LegalKeySizes property) • DES: 64 - 64 bits • 3DES: 128 - 192 bits • RC2: 40 - 128 bits • AES: 128 - 256 bits • 128 (standard for SSL) is usually sufficient • Initialization Vectors • Size = to block size (BlockSize property) protected void FillOutDetailFields(SymmetricAlgorithmsymmetricAlgorithm) { this.keySize.Text = symmetricAlgorithm.KeySize.ToString(); this.blockSize.Text = symmetricAlgorithm.BlockSize.ToString(); this.key.Text = Convert.ToBase64String(symmetricAlgorithm.Key); this.initializationVector.Text = Convert.ToBase64String(symmetricAlgorithm.IV); }
Encryption with Generated Keys. • Encryption: • Symmetric cont'd- • Generating Keys protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { SymmetricAlgorithmsymmetricAlgorithm = this.CreateSymmetricAlgorithm(this.algorithm.Text); this.FillOutDetailFields(symmetricAlgorithm); } } static byte[] GenerateRandomBytes(int length) { byte[] key = new byte[length]; RNGCryptoServiceProvider provider = new RNGCryptoServiceProvider(); provider.GetBytes(key); return key; }
Encryption with Generated Keys. • Encryption: • 1. Choose an Algorithm • SymmetricAlgorithmsymmetricAlgorithm = this.CreateSymmetricAlgorithm(this.algorithm.Text); • 2. Create or retrieve key. • symmetricAlgorithm.Key = Convert.FromBase64String(this.key.Text); • 3. Generate the IV. • symmetricAlgorithm.IV = Convert.FromBase64String(this.initializationVector.Text); • 4. Convert the clear text data to an array of bytes. • 5. Encrypt the clear text byte array. • ICryptoTransformencryptor = symmetricAlgorithm.CreateEncryptor(symmetricAlgorithm.Key, symmetricAlgorithm.IV); • // Create the streams used for encryption. • MemoryStreammemoryStream = new MemoryStream(); • using (CryptoStreamcryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write)) • { • byte[] plainTextAsBytes = new UTF8Encoding(false).GetBytes(this.plainText.Text); • cryptoStream.Write(plainTextAsBytes, 0, plainTextAsBytes.Length); • } • symmetricAlgorithm.Clear(); • byte[] encryptedData = memoryStream.ToArray(); • 6. Store the encryption data and the IV. • this.encryptedValue.Text = Convert.ToBase64String(encryptedData); • 7. If the key is new store it.
Encryption with Generated Keys. Decryption: 1. Choose the same algorithm that was used to encrypt the data. SymmetricAlgorithmsymmetricAlgorithm = this.CreateSymmetricAlgorithm(this.algorithm.Text); 2. Retrieve the key that was used. symmetricAlgorithm.Key = Convert.FromBase64String(this.key.Text); 3. Retrieve the IV that was used. symmetricAlgorithm.IV = Convert.FromBase64String(this.initializationVector.Text); 4. Retrieve the encrypted data. 5. Decrypt the data. ICryptoTransformdecryptor = symmetricAlgorithm.CreateDecryptor(symmetricAlgorithm.Key, symmetricAlgorithm.IV); // Create the streams used for encryption. MemoryStreammemoryStream = new MemoryStream(); using (CryptoStreamcryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Write)) { byte[] encryptedBytes = Convert.FromBase64String(this.encryptedValue.Text); cryptoStream.Write(encryptedBytes, 0, encryptedBytes.Length); } symmetricAlgorithm.Clear(); 6. Convert the data back to its original form. this.plainText.Text = new UTF8Encoding(false).GetString(memoryStream.ToArray());