简体   繁体   中英

Unable to decrypt PGP encrypted file into Stream from Azure Blob storage using C# scripting within script task of SSIS

I am trying to read the PGP encrypted file from Azure Blob Storage using SSIS package - Script Task- C# code into Stream, decrypt the file data into string and load to Azure SQL MI server. But I am not able to decrypt the file from Azure blob storage. When if I am reading the PGP encrypted file from Azure blob storage and downloading to the local folder, then from local folder I am able to decrypt the file and store the decrypted data into string.

Below the Main program - ScriptMain.cs

  public void Main()
            {
                try
                {
                    #region variables
                     string azureBlob = Dts.Variables["User::vAzureBlobContainer"].Value.ToString();
                    string azureStorageAccessKey = Dts.Variables["User::vAzureStorageAccessKey"].Value.ToString();
                    string azureStorageAccount = Dts.Variables["User::vAzureStorageAccount"].Value.ToString();
    
                  
                    StorageCredentials credentials = new StorageCredentials(azureStorageAccount, azureStorageAccessKey);
    
                    checkPoints = "1. Storage Login";
                    string inputFileName = @"FileInput.csv.pgp";
                    string keyFileName = @"C:\\PrivateKey\\keyfile.asc";
                    string passwd = @"z6yrFkE2NwzX";
                    
                    char[] password1 = passwd.ToCharArray();
                    string output;
                    output= PGPDecrypt.Program.DecryptFile(inputFileName, keyFileName, password1, credentials, azureBlob);
    
                 string[] lines = output.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
    
                       int count = 0;
    
                       foreach(string line in lines)
                       {
                           count++;
    
                       }
    
                       int cnt;
                       cnt = count;
                }
                catch (Exception ex)
                {
                    Dts.Events.FireError(0, "ERROR", checkPoints + ",\n" + ex.Message, null, 0);
                    Dts.TaskResult = (int)ScriptResults.Failure;
                    //throw;
                }
            }

Note: All required libraries added to ScriptMain.cs

Decrypt.cs

As mentioned by the OP that the issue has been resolved.

(Posting the findings as an answer so that if someone faced the similar issue in future, he would get the solution on this thread. This will be helpful for other community contributors.)

The issue has been resolved after setting the stream position to 0 right after writing in stream (mystream.position = 0) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM