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");
upload(client, @"\Upload", @"D:\ren_encrypt.xlsx");
client.Connect();
if (client.Exists(@"\Upload\ren_encrypt.xlsx"))
{
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";
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";
UnicodeEncoding UE = new UnicodeEncoding();
byte[] key = UE.GetBytes(password);
FileStream fsCrypt = new FileStream(inputFile, FileMode.Open);
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";
UnicodeEncoding UE = new UnicodeEncoding();
byte[] key = UE.GetBytes(password);
FileStream fsCrypt = new FileStream(inputFile, FileMode.Open);
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);
}