0

Can anyone show me an example of how to do encryption in Java using BouncyCastle (or similar API) for data types other than String?

I want to encrypt other Java types like Integer, Double and Date etc. I've looked over bouncycastle.org but cannot find any documentation of their API.

Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
kapil das
  • 2,061
  • 1
  • 28
  • 29
  • Similar issue is here, check it out: http://stackoverflow.com/questions/11984610/generating-digital-certificates-using-bouncycastle – mahyar Sep 19 '13 at 04:47
  • This question at Stackoverflow should be helpful: http://stackoverflow.com/questions/2052213/an-example-of-encrypting-an-xml-file-in-java-using-bouncy-castle – Ernestas Kardzys Sep 19 '13 at 05:56

2 Answers2

0

I can give you the code which encrypt byte array, hope that might help you in some way. Remember below ENCRYPTION_KEY value can be anything, its totally depends on you.

 // Cryptographic algorithm
  private final String ALGORITHM = "AES";
  //
 private final byte[] ENCRYPTION_KEY = new byte[]
 {
        'E', 'r', ';', '|', '<', '@', 'p', 'p', 'l', '1', 'c', '@', 't', '1', '0', 'n'
 };

public byte[] encryptValue(byte[] valueToEnc) throws EncryptionException
{
    try
    {
        // Constructs a secret key from the given byte array and algorithm
        Key key = new SecretKeySpec(ENCRYPTION_KEY, ALGORITHM);
        // Creating Cipher object by calling getInstance() factory methods and
        // passing ALGORITHIM VALUE = "AES" which is a 128-bit block cipher
        // supporting keys of 128, 192, and 256 bits.
        Cipher c = Cipher.getInstance(ALGORITHM);
        // Initialize a Cipher object with Encryption Mode and generated key
        c.init(Cipher.ENCRYPT_MODE, key);
        // To encrypt data in a single step, calling doFinal() methods: If we
        // want to encrypt data in multiple steps, then need to call update()
        // methods instead of doFinal()
        byte[] encValue = c.doFinal(valueToEnc);
        // Encrypting value using Apache Base64().encode method
        byte[] encryptedByteValue = new Base64().encode(encValue);

        return encryptedByteValue;
    }
    catch (Exception e)
    {
        throw new EncryptionException(e.getMessage(), e);
    }
}

Hope this will help.

Tapan Upadhyay
  • 109
  • 1
  • 1
  • 9
0

Encryption is an operation that is always performed on binary data. Any examples you've seen that work with String objects will be converting those strings into byte arrays at some point during the process.

The goal is to define a canonical method of converting your data into a byte array representation. Once you have this, you can use example code found all over the Internet to perform the encryption.

You may wish to convert an Integer into a four byte array, for example. Perhaps using code such as:

ByteBuffer.allocate(4).putInt(integerValue).array()

A Date might be best converted to a UNIX timestamp and then converted to a byte array using the code above.

The recipient of your encrypted data must understand how you serialised it to a byte array so that they can reverse the process. Note that in the case of strings, it's important both sides agree on a charset used when converting to/from a byte array.

Duncan Jones
  • 67,400
  • 29
  • 193
  • 254