Java Crypto API

I have lately written lot of code that uses crypto. Here are some examples (RSA, RC4) written using Java Crypto Extensions. Production code was written in C++, but code here was used for testing. You may use the code as you want.
RSA examples require pregenerated RSA keys (password 123456):

keytool -genkey -keystore swihocKeystore -keyalg RSA

The code requires Java 1.5 or newer

 
package swihoc;
import java.nio.ByteBuffer;
import java.security.*;
import java.io.*;
import java.util.Enumeration;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
 
/**
* Class for encrypting data with RSA private key, that is read from Java Keystore
* @author Janno Kusman
*/
public class CryptoTest
{
static void rc4() throws Throwable
{
//max key size 16bytes/128bits
String keyHex = "FAD175662B944575980DE7BA787CE92A";
byte[] keyBytes = Util.toByteArray(keyHex);
 
//RC4 is supported from jdk1.5
Key key = new SecretKeySpec(keyBytes, "RC4");
 
Cipher rc4 = Cipher.getInstance("RC4");
 
//rc4 encryption and decryption is same operation
rc4.init(Cipher.ENCRYPT_MODE, key);
 
String inHex =
"14000024796a42b8af680e234dc49583"+
"00f72f8d0fd9fbbb431e189420f164ed"+
"4d61490b37f34f741596f718e69ceb5d"+
"a1219863d3d400d8";
 
byte[] input = Util.toByteArray(inHex);
System.out.println( Util.hexDump(input, "") );
 
System.out.println( Util.hexDump(keyBytes, "Key: ") );
 
byte[] encrypted = rc4.doFinal(input);
System.out.println( Util.hexDump(encrypted, "") );
 
byte[] decrypted = rc4.doFinal(encrypted);
System.out.println( Util.hexDump(decrypted, "") );
}
 
static void rsa() throws Throwable
{
/**
* RSA uses random for padding. This class ensures that encryption with
* same key results same encrypted string.
*/
class MyDebugRandom extends SecureRandom
{
public void nextBytes(byte[] bytes)
{
System.out.println("Initializing "+bytes.length+ " of bytes");
for(int i=0; i < bytes.length; i++)
{
bytes[i] = (byte)0xFF;
}
}
}
 
KeyStore ks = KeyStore.getInstance("JKS");
String pw = "123456";
ks.load(new FileInputStream("swihocKeystore"), pw.toCharArray());
 
System.out.println(ks.size());
Enumeration aliases = ks.aliases();
while(aliases.hasMoreElements()){
System.out.println(aliases.nextElement());
}
 
java.security.cert.Certificate cert = ks.getCertificate("mykey");
PublicKey pubKey = cert.getPublicKey();
//        System.out.println("pubKey="+pubKey);
 
Key key = ks.getKey("mykey", pw.toCharArray());
//        System.out.println("privKey=\""+key+"\"");
//        System.out.println();
//        System.out.println();
//        System.out.println(key.getAlgorithm());
//        System.out.println(key.getFormat());
 
SecureRandom random = new MyDebugRandom();
 
byte[] in = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
 
//String in = "FFFFFFFF";
 
System.out.println( Util.hexDump(in, "") );
 
Cipher rsa = Cipher.getInstance("RSA");
rsa.init(Cipher.ENCRYPT_MODE, pubKey, random);
 
//enrypt
byte[] encrypted = rsa.doFinal(in, 0, in.length);
System.out.println( Util.hexDump(encrypted, "") );
 
rsa.init(Cipher.DECRYPT_MODE, key);
byte[] decrypted = rsa.doFinal(encrypted, 0, encrypted.length);
System.out.println( Util.hexDump(decrypted, "") );
}
 
public static void main(String[] args) throws Throwable
{
rsa();
rc4();
}
 
}

Util.java can be downloaded from here

Leave a Reply