Java Create Signature / Chilkat Verify Interoperability

The following Java code produces a digital signature that can be verified using Chilkat RSA.  Links to the Chilkat signature verification examples follow this code.  The Java signature creation code does not use Chilkat to produce the digital signature.  It also demonstrates how to save a generated key (public and private) to DER files that can be used with Chilkat RSA.

import java.io.*;
import java.security.*;

// Chilkat is only used for Base64 and Hex encoding.
import com.chilkatsoft.*;

public class SignTest {

  static {
    try {
        System.loadLibrary("chilkat");
    } catch (UnsatisfiedLinkError e) {
      System.err.println("Native code library failed to load.\n" + e);
      System.exit(1);
    }
  }

  public static void main(String argv[])
  {
  	  try
  	  {
		KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
		System.out.println("Generated RSA Key!");
		generator.initialize(1024);
		KeyPair keyPair = generator.generateKeyPair();

		PrivateKey privKey = keyPair.getPrivate();
		PublicKey pubKey = keyPair.getPublic();

		// Save the private key to an ASN.1 DER file:
		byte[] privKeyDer = privKey.getEncoded();
		OutputStream outPrivKey = new FileOutputStream("privKey.der");
		outPrivKey.write(privKeyDer);
		outPrivKey.close();

		// Save the public key to an ASN.1 DER file:
		byte[] pubKeyDer = pubKey.getEncoded();
		OutputStream outPubKey = new FileOutputStream("pubKey.der");
		outPubKey.write(pubKeyDer);
		outPubKey.close();

		Signature _signature = Signature.getInstance("SHA1withRSA");
		_signature.initSign(privKey);

		// Sign the string "The quick brown fox jumps over the lazy dog"
		String strToSign = "The quick brown fox jumps over the lazy dog";
		byte[] bytesToSign = strToSign.getBytes();
		_signature.update(bytesToSign,0,bytesToSign.length);

        // If you signed a string with characters in non-English languages, you may
        // wish to see the exact bytes signed, perhaps by loading the following file
        // in a hex editor.  Make sure the program verifying the signature uses the same
        // byte representations for the characters (i.e. the same character encoding).
		OutputStream outBytesSigned = new FileOutputStream("bytesToSign.txt");
		outBytesSigned.write(bytesToSign);
		outBytesSigned.close();

		// Sign the contents of a file with this commented-out code:
		//FileInputStream fis = new FileInputStream("dude.gif");
		//BufferedInputStream bufin = new BufferedInputStream(fis);
		//byte[] buffer = new byte[1024];
		//int len;
		//while ((len = bufin.read(buffer)) >= 0) {
			//_signature.update(buffer, 0, len);
			//};
		//bufin.close();

		// Create the signature.
		byte[] sigBytes = _signature.sign();

		OutputStream out = new FileOutputStream("sig.dat");
		out.write(sigBytes);
		out.close();

		// Convert the signature to a Base64 or hex string:
		CkByteData ckSigBytes = new CkByteData();
    	ckSigBytes.appendByteArray(sigBytes);

	    //  Convert to hexidecimalized string:
	    System.out.println(ckSigBytes.getEncoded("hex"));

	    //  Convert to base64 string:
	    System.out.println(ckSigBytes.getEncoded("base64"));

		System.out.println("Success!");

	    //} catch (NoSuchProviderException e) {
	    //	System.out.println("NoSuchProviderException!");
	    } catch (IOException e) {
	    	System.out.println("IOException!");
	    } catch (InvalidKeyException e) {
	    	System.out.println("InvalidKeyException!");
		} catch (SignatureException e) {
			System.out.println("SignatureException!");
		} catch (NoSuchAlgorithmException e) {
	    	System.out.println("NoSuchAlgorithmException!");
			}

  }
}

Verify Digital Signature examples using Chilkat in various programming languages:
ASP: Verify Java Signature
SQL Server: Verify Java Signature
C#: Verify Java Signature
C++: Verify Java Signature
MFC: Verify Java Signature
C: Verify Java Signature
Delphi: Verify Java Signature
Visual FoxPro: Verify Java Signature
Java: Verify Java Signature
Perl: Verify Java Signature
PHP: Verify Java Signature
Python: Verify Java Signature
Ruby: Verify Java Signature
VB.NET: Verify Java Signature
Visual Basic: Verify Java Signature
VBScript: Verify Java Signature