Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Iam using Renci.SshNet dll for uploading file to sftp server.

xlsx file is perfectly uploading . before uploading a file i am encrypting a file and uplading to specified path, it works perfect.

Now when iam trying to decrypt the file directly in client side(SFTP Path), it refers local path, not referring client path, even though i am specifying SFTP path.

kindly provide me solution.

NOTE: My aim is to encrypt file in my local and decrypt sample file in client side

Below is main function for Encrypt, upload, Decrypt


using (SftpClient client = new SftpClient(sftpHost, sftpPort, sftpUserName, sftpPassword))
       {
         EncryptFile(@"D:\ren.xlsx", @"D:\ren_encrypt.xlsx"); //Encryption process
         upload(client, @"\Upload", @"D:\ren_encrypt.xlsx"); // Uploading process
         client.Connect();
         if (client.Exists(@"\Upload\ren_encrypt.xlsx"))//Checking file exist in sftp path
           {
            DecryptFile(@"\Upload\ren_encrypt.xlsx", @"\Upload\ren_decrypt.xlsx");
            client.Disconnect();
           }
        }


Below code is Encryption code:

private void EncryptFile(string inputFile, string outputFile)
        {

            try
            {
                string password = @"myKey123"; //   Key Here
                UnicodeEncoding UE = new UnicodeEncoding();
                byte[] key = UE.GetBytes(password);
                string cryptFile = outputFile;
                FileStream fsCrypt = new FileStream(cryptFile, FileMode.Create);
                RijndaelManaged RMCrypto = new RijndaelManaged();
                CryptoStream cs = new CryptoStream(fsCrypt, RMCrypto.CreateEncryptor(key, key), CryptoStreamMode.Write);
                FileStream fsIn = new FileStream(inputFile, FileMode.Open);
                int data;
                while ((data = fsIn.ReadByte()) != -1)
                    cs.WriteByte((byte)data);
                fsIn.Close();
                cs.Close();
                fsCrypt.Close();
            }
            catch
            {
                MessageBox.Show("Encryption failed!", "Error");
            }
        }


Below code is upload code:

public void upload(SftpClient client, string destinationpath, string sourcefile)
       {
           try
           {
               client.Connect();
               client.ChangeDirectory(destinationpath);
               using (FileStream fs = new FileStream(sourcefile, FileMode.Open))
               {
                   client.BufferSize = 4 * 1024;
                   if (client.Exists(Path.GetFileName(sourcefile)))
                   {
                       client.DeleteFile(Path.GetFileName(sourcefile));
                   }
                   client.UploadFile(fs, Path.GetFileName(sourcefile));
                   client.Disconnect();
               }

           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message);
           }
       }


Below code is for Decrypt, Here come our failure process

 private void DecryptFile(string inputFile, string outputFile)
        {

            try
            {
                string scheck = Path.GetFileName(inputFile);
                     string password = @"myKey123"; //   Key Here
                UnicodeEncoding UE = new UnicodeEncoding();
                byte[] key = UE.GetBytes(password);
                FileStream fsCrypt = new FileStream(inputFile, FileMode.Open);

// Here in inputfile parameter what iam passing is "\Upload\ren_encrypt.xlsx" (SFTP Path) but, it throws an error as "Could not find a part of the path 'D:\Upload\ren_encrypt.xlsx'."  I couldnt understand why it is searching in my local drive instead of SFTP server which i have provided correct sftp path

                RijndaelManaged RMCrypto = new RijndaelManaged();
                CryptoStream cs = new CryptoStream(fsCrypt, RMCrypto.CreateDecryptor(key, key), CryptoStreamMode.Read);
                FileStream fsOut = new FileStream(outputFile, FileMode.Create);
                int data;
                while ((data = cs.ReadByte()) != -1)
                    fsOut.WriteByte((byte)data);
                fsOut.Close();
                cs.Close();
                fsCrypt.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }


kindly solve my issue.

What I have tried:

Below code is for Decrypt, Here come our failure process

 private void DecryptFile(string inputFile, string outputFile)
        {

            try
            {
                string scheck = Path.GetFileName(inputFile);
                     string password = @"myKey123"; //   Key Here
                UnicodeEncoding UE = new UnicodeEncoding();
                byte[] key = UE.GetBytes(password);
                FileStream fsCrypt = new FileStream(inputFile, FileMode.Open);

// Here in inputfile parameter what iam passing is "\Upload\ren_encrypt.xlsx" (SFTP Path) but, it throws an error as "Could not find a part of the path 'D:\Upload\ren_encrypt.xlsx'."  I couldnt understand why it is searching in my local drive instead of SFTP server which i have provided correct sftp path

                RijndaelManaged RMCrypto = new RijndaelManaged();
                CryptoStream cs = new CryptoStream(fsCrypt, RMCrypto.CreateDecryptor(key, key), CryptoStreamMode.Read);
                FileStream fsOut = new FileStream(outputFile, FileMode.Create);
                int data;
                while ((data = cs.ReadByte()) != -1)
                    fsOut.WriteByte((byte)data);
                fsOut.Close();
                cs.Close();
                fsCrypt.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
Posted
Updated 17-Oct-17 2:43am
v4
Comments
Richard Deeming 17-Oct-17 11:16am    
Your decryption function makes no reference to the SFTP client, and it's highly unlikely that the SFTP server will let you modify files directly on the server.

But why are you even trying to do this? By using SFTP, the protocol ensures that the file will be encrypted in transit without any effort on your part. What benefit do you see in adding this extra layer of encryption?
j snooze 17-Oct-17 17:20pm    
Agree with Richard here, what is wrong with SFTP encryption? Technically there is nothing wrong with encrypting and uploading, but to decrypt it from your side you would have to bring the bytes back down to you. You could give the client a decryption app so they can decrypt, but otherwise, your encryption would be useless because you'd have to download the bytes to unecrypt and reupload the unencrypted bytes which renders the initial encryption useless.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900