April 9, 2010

Saving an RSA key pair to a file

Filed under: RSA, private key — Tags: , , — admin @ 7:02 am

Question:

I’m having trouble finding a good/complete VB.Net Chilkat example of how to generate an RSA key container with a key pair (private and public) and save that key container to a file.

Answer:

I think I can clarify.  With 2 points:

1) In actuality, an RSA private key also contains the public-part of the key.  It contains the all of the key parts:  modulus, exponent, D, P, Q, DP, DQ, and InverseQ.  The public key is really just a sub-set of the private key and is composed of the modulus and exponent.  Therefore, to save the key pair, you really only need to save the private key.

2) The “key container” in a file would be an encrypted or unencrypted PEM (text) or DER (binary) file.  Either can be PCKS8 format or “RSA” format.  You would use the Chilkat.PrivateKey file to save the private key to PEM or DER (see the reference docs).  You may export the private key from the Chilkat.Rsa class to an XML string, and then load it into the Chilkat.PrivateKey class, and then save it to a file in whichever format you desire, using encryption or not.

April 5, 2010

Delete Files from a Zip Archive

Filed under: zip — Tags: — admin @ 7:10 am

Question:

“I’ve been trying out the ZIP package that you guys have and for the most part it’s great. I do however have a question about folder manipulation. What I would like to be able to do is individually remove files from the zip and if the directory is empty I would like to remove the directory from the zip file. Is there any way to remove directories from the zip as I can’t seem to find a way to do this? Also, is there any way to move/append files into a directory or create a directory in the zip?”

Answer:

The files contained within a .zip archive may or may not include a relative or absolute path.  For example, it is possible to have a .zip with several “text.txt” files:

  1. subdir1/test.txt
  2. test.txt
  3. subdir2/subdirA/test.txt

To delete one or more files from a .zip, iterate over the entries from 0 to zipObject.NumEntries-1.  For each ZipEntry object, your code can examine the entryObject.FileName property (which may also include a subdirectory path) to determine if it should be deleted.  If so, then call zipObject.DeleteEntry(entryObject) to remove the entry from the zipObject’s entries.  Once all entries have been deleted, call WriteZip or WriteZipAndClose to re-write the .zip with the deleted entries removed.

When iterating from 0 to zipObject.NumEntries, you will iterate over both file and directory entries.  The entryObject.IsDirectory property indicates whether the entry is a directory entry or not.  Removing directory entries is the same as for files.  (Note: Removing a directory entry does not remove all the files within that directory, it simply removes the explicit directory entry within the .zip)

To create a new explicit directory entry within a .zip, call zipObject.AppendNewDir.  (Note: All methods that Append files/dirs do not affect the existing .zip until WriteZip or WriteZipAndClose is called.)

To “move” files into a sub-directory within the .zip, simply update the zipEntry.FileName property to include the new subdirectory.  For example, change the zipEntry.FileName property from “test.txt” to “subdir1.test.txt”, then rewrite the .zip.  (Don’t re-write the .zip after each zipEntry object is modified.  Make all modifications (appends, deletes, zipEntry.FileName changes, etc.) and the rewrite with a single call to WriteZip/WriteZipAndClose.