Question:
Hello,
I am using the following code in one of our application to upload files with client side encryption.
crypt.SetEncodedIV(xxx, "ascii");
crypt.SetEncodedKey(xxx, "ascii");
crypt.CryptAlgorithm = "aes";
crypt.CipherMode = "cbc";
crypt.KeyLength = 256;
crypt.PaddingScheme = 0;
FileStream fsIn = File.OpenRead(localPath);
string handle;
handle = sftp.OpenFile(remotePath, "writeOnly", "createTruncate");
if (handle == string.Empty)
{
Console.WriteLine("File not created!");
return;
}
crypt.FirstChunk = true;
crypt.LastChunk = false;
byte[] encryptedChunk;
byte[] b = new byte[524288];
int n;
while ((n = fsIn.Read(b, 0, b.Length)) > 0)
{
if (n < b.Length)
{
byte[] tmp = new byte[n];
int i = 0;
for (i = 0; i < n; i++) tmp[i] = b[i];
encryptedChunk = crypt.EncryptBytes(tmp);
}
else
{
encryptedChunk = crypt.EncryptBytes(b);
}
if (!sftp.WriteFileBytes(handle, encryptedChunk))
{
Console.WriteLine(sftp.LastErrorText);
}
crypt.FirstChunk = false;
}
fsIn.Close();
crypt.LastChunk = true;
byte[] empty = { };
encryptedChunk = crypt.EncryptBytes(empty);
//ENCRYPTED CHUNK IS ALWAYS 0!
if (encryptedChunk.Length > 0)
{
if (!sftp.WriteFileBytes(handle, encryptedChunk))
{
Console.WriteLine(sftp.LastErrorText);
}
}
success = sftp.CloseHandle(handle);
This code worked fine with the previous versions but when using the latest, the lenght of the encryptedChunk is always 0. I did not receive any error message, so I can't find where the problem is. What could be the problem? We have no idea!
Thank you, dss
Thanks dss, I'll have a look as soon as possible..
I tested it, and everything works. Any method that returns a byte array, such as EncryptBytes, will return an empty byte array on failure. Rather than check for failure this way (because it may be possible that an empty byte array is a valid successful return), you can check the object's LastMethodSuccess property. I suspect it failed for some reason -- maybe the component was not previously successfully unlocked? You can check the crypt.LastErrorText after the call to EncryptBytes to see what happened.
Thank you for your reply! Yes, I know that that an empty array can be valid. But I discovered, that the array is always empty with the 9.5.0.55 version. I tested the code with 3 files, with the totally same code only changed 9.5.0.54 to 9.5.0.55. I attached the LastErrorText outputs. As you can see, there are differences in totalNumBytesSent in sftp responses.
When I try to download and decrypt the files, the files uploaded with .54 are good (same md5 checksum) but the files uploaded with .55 are smaller, the last chunk seems to be not uploaded.
This is the modificated source with the logging:
encryptedChunk = crypt.EncryptBytes(empty);
Console.WriteLine(crypt.LastErrorText);
if (encryptedChunk.Length > 0)
{
if (!sftp.WriteFileBytes(handle, encryptedChunk))
{
Console.WriteLine(sftp.LastErrorText);
}
}
Console.WriteLine(sftp.LastErrorText);
The results of 9.5.0.54:
ChilkatLog:
EncryptBytes:
DllDate: Oct 29 2015
ChilkatVersion: 9.5.0.54
UnlockPrefix: UNLOCK-CODE
Username: User
Architecture: Little Endian; 32-bit
Language: .NET 4.5
VerboseLogging: 0
numBytes: 0
Success.
--EncryptBytes
--ChilkatLog
ChilkatLog:
WriteFileBytes:
DllDate: Oct 29 2015
ChilkatVersion: 9.5.0.54
UnlockPrefix: UNLOCK-CODE
Username: User
Architecture: Little Endian; 32-bit
Language: .NET 4.5
VerboseLogging: 0
SshVersion: SSH-2.0-mod_sftp
SftpVersion: 3
handle: 37363534303137653930663731363537
numBytes: 16
offset: 1518048
writeDataSource:
End of data stream...
------------------------------------------------
getWriteStatusReplies:
sshReadMessage:
mType: CHANNEL_DATA
--sshReadMessage
--getWriteStatusReplies
totalNumBytesSent: 16
--writeDataSource
Success.
--WriteFileBytes
--ChilkatLog
ChilkatLog:
EncryptBytes:
DllDate: Oct 29 2015
ChilkatVersion: 9.5.0.54
UnlockPrefix: UNLOCK-CODE
Username: User
Architecture: Little Endian; 32-bit
Language: .NET 4.5
VerboseLogging: 0
numBytes: 0
Success.
--EncryptBytes
--ChilkatLog
ChilkatLog:
WriteFileBytes:
DllDate: Oct 29 2015
ChilkatVersion: 9.5.0.54
UnlockPrefix: UNLOCK-CODE
Username: User
Architecture: Little Endian; 32-bit
Language: .NET 4.5
VerboseLogging: 0
SshVersion: SSH-2.0-mod_sftp
SftpVersion: 3
handle: 39306363396564343965663463663565
numBytes: 373376
offset: 1048576
writeDataSource:
getWriteStatusReplies:
sshReadMessage:
mType: CHANNEL_DATA
--sshReadMessage
sshReadMessage:
mType: CHANNEL_DATA
--sshReadMessage
sshReadMessage:
mType: CHANNEL_DATA
--sshReadMessage
sshReadMessage:
mType: CHANNEL_DATA
--sshReadMessage
sshReadMessage:
mType: CHANNEL_DATA
--sshReadMessage
sshReadMessage:
mType: CHANNEL_DATA
--sshReadMessage
sshReadMessage:
mType: CHANNEL_DATA
--sshReadMessage
sshReadMessage:
mType: CHANNEL_DATA
--sshReadMessage
sshReadMessage:
mType: CHANNEL_DATA
--sshReadMessage
sshReadMessage:
mType: CHANNEL_DATA
--sshReadMessage
--getWriteStatusReplies
End of data stream...
------------------------------------------------
getWriteStatusReplies:
sshReadMessage:
mType: CHANNEL_DATA
--sshReadMessage
sshReadMessage:
mType: CHANNEL_DATA
--sshReadMessage
--getWriteStatusReplies
totalNumBytesSent: 373376
--writeDataSource
Success.
--WriteFileBytes
--ChilkatLog
ChilkatLog:
EncryptBytes:
DllDate: Oct 29 2015
ChilkatVersion: 9.5.0.54
UnlockPrefix: UNLOCK-CODE
Username: User
Architecture: Little Endian; 32-bit
Language: .NET 4.5
VerboseLogging: 0
numBytes: 0
Success.
--EncryptBytes
--ChilkatLog
ChilkatLog:
WriteFileBytes:
DllDate: Oct 29 2015
ChilkatVersion: 9.5.0.54
UnlockPrefix: UNLOCK-CODE
Username: User
Architecture: Little Endian; 32-bit
Language: .NET 4.5
VerboseLogging: 0
SshVersion: SSH-2.0-mod_sftp
SftpVersion: 3
handle: 66353464376162646530333334306335
numBytes: 16
offset: 1637520
writeDataSource:
End of data stream...
------------------------------------------------
getWriteStatusReplies:
sshReadMessage:
mType: CHANNEL_DATA
--sshReadMessage
--getWriteStatusReplies
totalNumBytesSent: 16
--writeDataSource
Success.
--WriteFileBytes
--ChilkatLog
The results of 9.5.0.55:
ChilkatLog:
EncryptBytes:
DllDate: Dec 29 2015
ChilkatVersion: 9.5.0.55
UnlockPrefix: UNLOCK-CODE
Username: User
Architecture: Little Endian; 32-bit
Language: .NET 4.5
VerboseLogging: 0
numBytes: 0
Success.
--EncryptBytes
--ChilkatLog
ChilkatLog:
WriteFileBytes:
DllDate: Dec 29 2015
ChilkatVersion: 9.5.0.55
UnlockPrefix: UNLOCK-CODE
Username: User
Architecture: Little Endian; 32-bit
Language: .NET 4.5
VerboseLogging: 0
SshVersion: SSH-2.0-mod_sftp
SftpVersion: 3
handle: 62373466666639623465633433316435
numBytes: 469472
offset: 1048576
writeDataSource:
End of data stream...
------------------------------------------------
totalNumBytesSent: 469472
--writeDataSource
Success.
--WriteFileBytes
--ChilkatLog
ChilkatLog:
EncryptBytes:
DllDate: Dec 29 2015
ChilkatVersion: 9.5.0.55
UnlockPrefix: UNLOCK-CODE
Username: User
Architecture: Little Endian; 32-bit
Language: .NET 4.5
VerboseLogging: 0
numBytes: 0
Success.
--EncryptBytes
--ChilkatLog
ChilkatLog:
WriteFileBytes:
DllDate: Dec 29 2015
ChilkatVersion: 9.5.0.55
UnlockPrefix: UNLOCK-CODE
Username: User
Architecture: Little Endian; 32-bit
Language: .NET 4.5
VerboseLogging: 0
SshVersion: SSH-2.0-mod_sftp
SftpVersion: 3
handle: 39316565646566373137353061633763
numBytes: 373376
offset: 1048576
writeDataSource:
End of data stream...
------------------------------------------------
totalNumBytesSent: 373376
--writeDataSource
Success.
--WriteFileBytes
--ChilkatLog
ChilkatLog:
EncryptBytes:
DllDate: Dec 29 2015
ChilkatVersion: 9.5.0.55
UnlockPrefix: UNLOCK-CODE
Username: User
Architecture: Little Endian; 32-bit
Language: .NET 4.5
VerboseLogging: 0
numBytes: 0
Success.
--EncryptBytes
--ChilkatLog
ChilkatLog:
WriteFileBytes:
DllDate: Dec 29 2015
ChilkatVersion: 9.5.0.55
UnlockPrefix: UNLOCK-CODE
Username: User
Architecture: Little Endian; 32-bit
Language: .NET 4.5
VerboseLogging: 0
SshVersion: SSH-2.0-mod_sftp
SftpVersion: 3
handle: 66656531313134633031613064643264
numBytes: 64656
offset: 1572864
writeDataSource:
End of data stream...
------------------------------------------------
totalNumBytesSent: 64656
--writeDataSource
Success.
--WriteFileBytes
--ChilkatLog