Can not read encrypted files

70 views
Skip to first unread message

Ranga Fernando

unread,
Dec 6, 2011, 2:56:17 AM12/6/11
to didisof...@googlegroups.com
Hi,
I'm using didisoft's registered PGP libs in one of our projects.
I do the encryption of part of the content using pgp.encryptStream() ( contents are separated by ',' ) and finally the whole file encrypted using pgp.encryptFile().
The above implementation done in JAVA and is running on Glassfish application server on a windows box.
once the file being encrypted the it it will be uploaded to a different location.

The encryption of the file and the content decrypted above is done using .NET client.
So far the encryption and the decryption worked without any issues.

However, we have moved our application in to a linux box. The file content and file itself encryption part is now happening on a linux box and being uploaded to a ftp location.
I tried download the file and try to decrypt the file using my .NET client.
The decryption in .NET no longer working.

I investigate further on this issue and realized that I still can decrypt old files I created in windows box without any issues.
However the problem is with the file created on the linux box.
When I use pgp.DecryptFile() to decrypt the file in .NET. The content of the file are look as follows ( content separated by ',') .
Which is different to the decrypted files created on windows box.
The bold content is the encrypted file content, which I need to decrypt it.

When I try to read the rows of the file through a streamreadder and split the content by ',' in order to decrypted content which is being encrypted.
The program can not correctly split the content, the split array contains only 2 elements which is  "000000021001" and "".
The program failing to read the other contents.

000000021001,


hQEMA1c4dmEpO7e7AQf/SFZAOcZUesu/OjSJZrH4S7/aGO+IkE93nYqq6A6Um+Jf
lgVjj7EXLM4bqyNjb2rCugzgLaLtAB8DSoQZ5og4zUq+XcitRwcBveTDE+zASHHe
tDmUyeySol0dKUUNX2c4Qb/T5jRVfHlruoAEJShG357tf0tW56KAdTjoBessy976
khOhjo54/nPOYbPIK3zZKXDP38zTdo+mdCeGC+tBMc/KCUO6Draio3vs7z7UWSzY
eExGBR4dPSDkyithP8pfh0BFIQlRfInyGXfK0ttXxqrl2Z5o7aDSb1HQMV2v/n7Z
VlMjeNvRN/pB974uY1R/cGQlPM6tMwjqO0jrvjPLudJDAeHtKe6y8vP7XOfG4dA3
kt3IlGTjApU+v/9JR83VoveOaw6QNE1o5DqXB6YrnzAMunuuy/iosR+Aq8KTAr9v
e7G7/g==
=Sh7W

,5.0,EUR,2011-12-06 07:47:41,2012-12-31 00:00:00,131,21

Could you please help me figuring out what causes this unusual behavior and how to fix this?
Please do let me know should you require clarifying anything with regard to this issue.

Does the Operating systems really effect this behavior ? Please advice.

Thx & BR
Ranga

didisoft

unread,
Dec 6, 2011, 3:48:07 AM12/6/11
to DidiSoft Forum
Hi Ranga,

Will it be possible for you to send me:
1) a test key pair (public and private key with the private key
password)
2) a file encrypted with the above keys on the Windows machine
3) the same source file encrypted with the above keys on the Linux
machine

4) (Oprionally) the code snippets that you use to encrypt the data and
decrypt.

Best Regards,
Peter Kalef
DidiSoft Ltd.

Best Regards,
Peter Kalef
DidiSoft Ltd.

Ranga Fernando

unread,
Dec 6, 2011, 4:13:14 AM12/6/11
to didisof...@googlegroups.com
Hi Peter,

1- Keys
I used 2 pub keys file; one to encrypt the file content and other one to encrypt the file (You can find them in the pub.rar zip)
pub.rar
pub_hrn_key.pkr - to encrypt the part of the content of the file.
pub_file_key.pkr - to encrypt the file itself.

and respective pvt key files.
pvt.rar
pvt_hrn_key.skr - to decrypt the part of the content of the file.
pvt_file_key.skr - to decrypt the file itself.


2-3. Windows.ord and Linux.ord - Note that the content of the 2 files are not equal. windows.ord were created before moving the code base to Linux. Linux.ord were created after moving to linux. But I can decrypt the Windows.ord without any issues.


4.
Encryption happen in JAVA
Code snip that I use to encrypt the part of the file content
private String encryptHrn(String content) {
        String encHrn = null;
        String pathTopublicKey = getPathToHrnKeyFile();
        try {
            PGPLib pgp = new PGPLib();
            InputStream keyFile = new FileInputStream(new File(pathTopublicKey));
            ByteArrayInputStream data = new ByteArrayInputStream(content.getBytes());
            ByteArrayOutputStream encData = new ByteArrayOutputStream();
            pgp.encryptStream(data, "lol.temp", keyFile, encData, true, true);
            encHrn = encData.toString();
            encHrn=encHrn.replace("-----BEGIN PGP MESSAGE-----", "");
            encHrn=encHrn.replace("Version: BCPG v1.45", "");
            encHrn=encHrn.replace("-----END PGP MESSAGE-----", "");
            encHrn=encHrn.replace("\r\n", "");
        } catch (FileNotFoundException fnex) {
            fnex.printStackTrace();
        } catch (PGPException pgpex) {
            pgpex.printStackTrace();
        } catch (IOException ioex) {
            ioex.printStackTrace();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return encHrn;
    }


File encryption

 public void encryptFile(String sourceFile, String encFile) {
        String pathTopublicKey = getPathToFileKeyFile();
        String sFile = instanceRoot + File.separator + sourceFile;
        String dFile = instanceRoot + File.separator + encFile;
        try {
            PGPLib pgp = new PGPLib();
            InputStream fs = new FileInputStream(new File(pathTopublicKey));
            pgp.encryptFile(sFile, fs, dFile, true, true);
        } catch (PGPException pgpex) {
            pgpex.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

Decryption happen in .NET

Decrypt file and content main flow

DecryptFile(sourceFilePath, destFileName, tempFolderPath);
            FileStream finalFile =  fm.CreateFile(destinationPath);
            //Open the temp file to decrypt HRN
            StreamReader sr = new StreamReader(tempFolderPath + "\\" + destFileName);
            StreamWriter sw = new StreamWriter(finalFile);
            using (sr)
            {
                while (!sr.EndOfStream)
                {
                    content = new StringBuilder();
                    string[] rs = sr.ReadLine().Split(',');
                    string encHrn = rs[1];
                    string hrn = DecryptHN(encHrn);
                    content.Append(rs[0] + "," + hrn + "," + rs[2] + "," + rs[3] + "," + rs[4] + "," + rs[5] + "," + rs[6] + "," + rs[7]);
                    fm.AppendFile(sw, content.ToString());

                }
                sw.Close();
                sw.Dispose();
            }
            fm.DeleteFile(tempFolderPath + "\\" + destFileName);
        }


File decryption
Note that all the paths are correctly set to files without any issues.

pvtPassword = "E4T@%^&*";

public String DecryptFile(string sourceFile, string destinationFileName, string destinationFolder)
        {
            string originalFileName = string.Empty;
            string destinationPath = destinationFolder + "\\" + destinationFileName;
            try
            {
                pgp = new PGPLib();
                originalFileName = pgp.DecryptFile(sourceFile, ApplicationPath.KeyFilePath, pvtPassword, destinationPath);
            }
            catch (Exception ex)
            {
                throw new PGPException(ex.Message, ex);
            }
            return originalFileName;
        }

Decrypt file content
public string DecryptHN(string hn)
        {
            string localHn = string.Empty;
            try
            {
                pgp = new PGPLib();
                fm = new FileManager();
                localHn = pgp.DecryptString(hn, fm.GetHrnKeyFile(), pvtPassword);
            }
            catch (Exception ex)
            {
                throw new PGPException(ex.Message, ex);
            }
            return localHn;
        }

I Also attached a sample file at the state of as soon as it been decrypted ( before decrypt the content) [After_decrypt_the_file.ord]

Please do let me know where is the problem lies relating to this issue.

Thanks in advance for your kind co-operation.

Thx & BR,
Ranga
pub.rar
pvt.rar
WINDOWS.ord
LINUX.ord
After_decrypt_the_file.ord

didisoft

unread,
Dec 6, 2011, 5:53:30 AM12/6/11
to DidiSoft Forum
Hi Ranga,

We have investigated the code that you have provided and have found
some small things that have to be corrected.

1)
In the Java code in the method: private String encryptHrn(String
content)
on the line: encHrn=encHrn.replace("\r\n", "");
\r\n - is the line eding on Windows systems, on Linux the line
termination character is \n
For an Operating system independant code please use
System.getProperty("line.separator")

2)
Please ensure that when you convert the Strings into byte arrays you
use consistent character encoding:
for example in the method: private String encryptHrn(String content)
on the line: ByteArrayInputStream data = new
ByteArrayInputStream(content.getBytes());
- this will convert the string into byte array using the Operating
system default character encoding.
but at the .NET side you are using the PGPLib.DecryptString method
that expects that the encrypted string is encoded in UTF-8.
It will be safe if in the Java code you specify explicitly the
character encoding like:
data = new ByteArrayInputStream(content.getBytes("UTF-8"));

Please correct the above notices and drop me a line if there is any
improvement.

Ranga Fernando

unread,
Dec 6, 2011, 6:42:48 AM12/6/11
to didisof...@googlegroups.com
Hi Peter,
First of all, I must thank you for your rapid and quick feedback with regard to the issues.
This is wonderful and I'm really appreciate the support you provided.!

Second of all ! All your suggestions got worked !
I changed the places as you suggested and re-deploy the application. Its working fine now !!

Thanks for the great support !!!

Thx & BR,
Ranga

didisoft

unread,
Dec 6, 2011, 11:54:57 AM12/6/11
to DidiSoft Forum
No problem Ranga,

Please let me know if I can be of any other help in the future.

Kind Regards,
Peter Kalef
DidiSoft Ltd.

Reply all
Reply to author
Forward
0 new messages