1 / 5

Encryption with Generated K eys.

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"

Download Presentation

Encryption with Generated K eys.

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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); }

  3. 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; }

  4. 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.

  5. 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());

More Related