Thursday 10 October 2019

Decrypt Files using System.Security.Cryptography in SSIS package


We are getting the encrypted file (Rijndael Algorithm). We need to decrypt these files is load it into the database.


We have an encrypted file that is not a human-readable file.


Here I will decrypt this file using System.Security.Cryptography (Rijndael Algorithm).
I am taking script task. We need to write the below code (in this demo I am decrypting the file which is encrypted by above example post https://bageshkumarbagi-msbi.blogspot.com/2019/10/encrypt-files-using-systemsecuritycrypt.html )
We need to include below namespaces
  • using System.Security.Cryptography;
  • using System.IO;

Below is the C# code
// Open filestream for encrypted source file
            FileStream fsIn = new FileStream(@"J:\SSIS1\Source\sales_10062019__ENCRYPTED.csv", FileMode.Open, FileAccess.Read);
            // Open filestream for decrypted file
            FileStream fsOut = new FileStream(@"J:\SSIS1\Source\sales_10062019_DECRYPTED.csv", FileMode.OpenOrCreate, FileAccess.Write);
            string encryptionKey = "Bagesh"; //Encryption key
            // Create Key and IV from the password with salt technique
            PasswordDeriveBytes pdb = new PasswordDeriveBytes(encryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
            // Create a symmetric algorithm with Rijndael
            Rijndael alg = Rijndael.Create();
            // Set Key and IV
            alg.Key = pdb.GetBytes(32);
            alg.IV = pdb.GetBytes(16);
            CryptoStream cs = new CryptoStream(fsOut, alg.CreateDecryptor(), CryptoStreamMode.Write);
            int bufferLen = 4096;
            byte[] buffer = new byte[bufferLen];
            int bytesRead;
            do
            {   // read a chunk of data from the input file
                bytesRead = fsIn.Read(buffer, 0, bufferLen);
                // Decrypt it
                cs.Write(buffer, 0, bytesRead);
            } while (bytesRead != 0);
            cs.Close();
            fsIn.Close();
Compile and save and close this window.
Note: for the encryption and decryption, we need to use the same encryption Key
 Now I am running this package.
  
Executed successfully.
  
Now we can read this file.

4 comments:

  1. Great post i must say and thanks for the information.
    Cyber security Online Training

    ReplyDelete
  2. Different forms of a bitcoin wallet. There are several different forms of a bitcoin wallet and each of them are used as per their requirements. bitcoin mixer

    ReplyDelete
  3. This is very educational content and written well for a change. It's nice to see that some people still understand how to write a quality post.! wasabi wallet

    ReplyDelete

If you have any doubt, please let me know.

Popular Posts