These samples are located in \Samples\Windows\Crypto.
Description
Using the Sample
Key Project Files
Technologies Demonstrated
These samples are based on the Cryptography example code available from the MSDN Library documentation. The Sample.crypto package contains four classes that can be used to digitally sign and verify data and to encrypt/decrypt data streams. These classes have been implemented as extensions of java.io.InputStream and java.io.OutputStream so that they can be easily used in conjunction with existing data storage and retrieval code. Each of the Crypto API functions that are accessed through Microsoft® J/Direct have been wrapped with a Java method that converts the typical boolean for success/failure into a com.ms.dll.Win32Exception that encapsulates the error code returned from GetLastError (in Java, this will be com.ms.dll.DllLib.getLastWin32Error. All the stream classes (Encrypt/Decrypt, Sign/Verify) have main methods that facilitate command-line usage.
The InitUser application (sample.crypto.InitUser) must be run prior to using these samples to ensure that the default cryptographic client has been set up.
EnumAlgs can be used to display the available encryption algorithms for the current system.
This sample was written for Microsoft® Windows NT® 4.0 or later.
To compile the samples
Use Nmake.exe to compile the makefile in the \Samples\Windows\Crypto directory.
To install the samples
The INITUSER sample (or equivalent) must be run prior to using these samples to create a key container for the default user.
To run the samples
Use the following command to run InitUser:
jview sample.crypto.InitUser
Use the following command to run EnumAlgs:
jview sample.crypto.EnumAlgs
The ENCRYPT sample is a console application that encrypts files. Files encrypted with this sample can be decrypted with the DECRYPT sample.
Use the following command to run ENCRYPT:
jview sample.crypto.EncryptStream <source file> <dest file> [<password>]
The <source file> argument specifies the name of the plain text file to be encrypted, and the <dest file> argument specifies the name of the cipher text file to be created. The optional <password> argument specifies a password with which to encrypt the file.
If no password is specified, a random session key is used to encrypt the file. This session key is then encrypted with the key exchange public key of the default user and stored with the encrypted file. In this case, the corresponding key exchange private key is later used to decrypt the session key. The session key is then used to decrypt the file itself.
The DECRYPT sample is a console application that decrypts files. Use the following command to run DECRYPT:
jview sample.crypto.DecryptStream <source file> <dest file> [<password>]
The <source file> argument specifies the name of the cipher text file to be decrypted, and the <dest file> argument specifies the name of the plain text file to be created. The optional <password> argument specifies a password with which to decrypt the file.
Although supplying an invalid password to DECRYPT does not usually generate an error, the file will not be properly decrypted.
Use the following command to run the SIGNFILE sample:
jview sample.crypto.SignStream <source file> <signature file> <description>
The <source file> argument specifies the name of the file to be signed, and the <signature file> argument specifies the name of the file in which to place the signature data. The <description> argument specifies a textual description of the data being signed. This can consist of empty double quotes ("") if no description is required.
Run sample.crypto.VerifyStream with the same arguments as SIGNFILE. If the contents of the source file, signature file, or description string have changed in any way since the file was originally signed, the program will fail.
Input stream that decrypts the given stream with the provided credentials.
EncryptStream.javaOutput stream that encrypts the data using the given credentials.
EnumAlgs.javaEnumerates the Crypto algorithms available on the local system.
InitUser.javaInitializes the credentials.
SignStream.javaOutput stream that signs the outgoing code with the given credentials.
VerifyStream.javaInput stream that verifies the incoming contents.
Advapi32.javaContains private @dll.import statements for the Crypto API functions used in Java. These are wrapped in Java methods that handle the Microsoft® Win32® error code and convert it into a com.ms.dll.Win32Exception.