Link to home
Start Free TrialLog in
Avatar of jazzIIIlove
jazzIIIloveFlag for Sweden

asked on

Simple Symmetric Encryption in Java problem

I have a simple symmetric encryption taken from the book "Beginning Cryptography with Java  
by David Hook".  

I have a problem that I cannot run the original code as expected.

In the very original code below:
I got the runtime error as:
Exception in thread "main" java.security.NoSuchProviderException: No such provider: BC
      at javax.crypto.Cipher.getInstance(Unknown Source)
      at SimpleSymmetricExample.main(SimpleSymmetricExample.java:23)

If I convert the line from:
Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding", "BC");
to:
Cipher cipher = Cipher.getInstance("AES");

I got the output as:
input text : 00112233445566778899aabbccddeeff
Exception in thread "main" java.security.InvalidKeyException: Illegal key size or default parameters
      at javax.crypto.Cipher.a(Unknown Source)
      at javax.crypto.Cipher.a(Unknown Source)
      at javax.crypto.Cipher.a(Unknown Source)
      at javax.crypto.Cipher.init(Unknown Source)
      at javax.crypto.Cipher.init(Unknown Source)
      at SimpleSymmetricExample.main(SimpleSymmetricExample.java:31)
line 31 is as follows:
cipher.init(Cipher.ENCRYPT_MODE, key);

Note that I have NOT used any library in the first place.
I also tried to use the jar files in the link: http://www.bouncycastle.org/download/crypto-144.zip

What should I do to fix it?

Best regards.
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

/**
 * Basic symmetric encryption example
 */
////Utils
class Utils
{
    private static String digits = "0123456789abcdef";

    /**
     * Return length many bytes of the passed in byte array as a hex string.
     *
     * @param data the bytes to be converted.
     * @param length the number of bytes in the data block to be converted.
     * @return a hex representation of length bytes of data.
     */
    public static String toHex(byte[] data, int length)
    {
        StringBuffer    buf = new StringBuffer();

        for (int i = 0; i != length; i++)
        {
            int v = data[i] & 0xff;

            buf.append(digits.charAt(v >> 4));
            buf.append(digits.charAt(v & 0xf));
        }

        return buf.toString();
    }

    /**
     * Return the passed in byte array as a hex string.
     *
     * @param data the bytes to be converted.
     * @return a hex representation of data.
     */
    public static String toHex(byte[] data)
    {
        return toHex(data, data.length);
    }
}

//////SimpleSymmetricExample
public class SimpleSymmetricExample
{
    public static void main(String[] args) throws Exception
    {
        byte[]        input = new byte[] {
                          0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
                          (byte)0x88, (byte)0x99, (byte)0xaa, (byte)0xbb,
                          (byte)0xcc, (byte)0xdd, (byte)0xee, (byte)0xff };
        byte[]        keyBytes = new byte[] {
                          0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
                          0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
                          0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17 };

        SecretKeySpec key = new SecretKeySpec(keyBytes, "AES");

        Cipher        cipher = Cipher.getInstance("AES/ECB/NoPadding", "BC");

        System.out.println("input text : " + Utils.toHex(input));

        // encryption pass

        byte[] cipherText = new byte[input.length];

        cipher.init(Cipher.ENCRYPT_MODE, key);

        int ctLength = cipher.update(input, 0, input.length, cipherText, 0);

        ctLength += cipher.doFinal(cipherText, ctLength);

        System.out.println("cipher text: " + Utils.toHex(cipherText)
                                                     + " bytes: " + ctLength);

        // decryption pass

        byte[] plainText = new byte[ctLength];

        cipher.init(Cipher.DECRYPT_MODE, key);

        int ptLength = cipher.update(cipherText, 0, ctLength, plainText, 0);

        ptLength += cipher.doFinal(plainText, ptLength);

        System.out.println("plain text : " + Utils.toHex(plainText)
                                                     + " bytes: " + ptLength);
    }
}

Open in new window

Avatar of Gurvinder Pal Singh
Gurvinder Pal Singh
Flag of India image

can you replace line number 62 by
 Cipher c1 = Cipher.getInstance("DES");

for reference see
http://java.sun.com/j2se/1.4.2/docs/guide/security/jce/JCERefGuide.html#SimpleEncrEx
ASKER CERTIFIED SOLUTION
Avatar of Gurvinder Pal Singh
Gurvinder Pal Singh
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jazzIIIlove

ASKER

As in the code snippet, I added Cipher cipher = Cipher.getInstance("DES"); and Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
but the result is as follows:

What am I doing wrong?

input text : 00112233445566778899aabbccddeeff
Exception in thread "main" java.security.InvalidKeyException
      at com.ibm.crypto.provider.DESCipher.engineGetKeySize(Unknown Source)
      at javax.crypto.Cipher.b(Unknown Source)
      at javax.crypto.Cipher.a(Unknown Source)
      at javax.crypto.Cipher.a(Unknown Source)
      at javax.crypto.Cipher.a(Unknown Source)
      at javax.crypto.Cipher.init(Unknown Source)
      at javax.crypto.Cipher.init(Unknown Source)
      at SimpleSymmetricExample.main(SimpleSymmetricExample.java:74)
import java.security.Security;

import javax.crypto.Cipher; 
import javax.crypto.spec.SecretKeySpec; 
 
/** 
 * Basic symmetric encryption example 
 */ 
////Utils 
class Utils 
{ 
    private static String digits = "0123456789abcdef"; 
 
    /** 
     * Return length many bytes of the passed in byte array as a hex string. 
     * 
     * @param data the bytes to be converted. 
     * @param length the number of bytes in the data block to be converted. 
     * @return a hex representation of length bytes of data. 
     */ 
    public static String toHex(byte[] data, int length) 
    { 
        StringBuffer    buf = new StringBuffer(); 
 
        for (int i = 0; i != length; i++) 
        { 
            int v = data[i] & 0xff; 
 
            buf.append(digits.charAt(v >> 4)); 
            buf.append(digits.charAt(v & 0xf)); 
        } 
 
        return buf.toString(); 
    } 
 
    /** 
     * Return the passed in byte array as a hex string. 
     * 
     * @param data the bytes to be converted. 
     * @return a hex representation of data. 
     */ 
    public static String toHex(byte[] data) 
    { 
        return toHex(data, data.length); 
    } 
} 
 
//////SimpleSymmetricExample 
public class SimpleSymmetricExample 
{ 
    public static void main(String[] args) throws Exception 
    { 
    	Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
        byte[]        input = new byte[] { 
                          0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 
                          (byte)0x88, (byte)0x99, (byte)0xaa, (byte)0xbb, 
                          (byte)0xcc, (byte)0xdd, (byte)0xee, (byte)0xff }; 
        byte[]        keyBytes = new byte[] { 
                          0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 
                          0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 
                          0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17 }; 
 
        SecretKeySpec key = new SecretKeySpec(keyBytes, "AES"); 
 
        //Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding", "BC");
        Cipher cipher = Cipher.getInstance("DES");
 
        System.out.println("input text : " + Utils.toHex(input)); 
 
        // encryption pass 
 
        byte[] cipherText = new byte[input.length]; 
 
        cipher.init(Cipher.ENCRYPT_MODE, key); 
 
        int ctLength = cipher.update(input, 0, input.length, cipherText, 0); 
 
        ctLength += cipher.doFinal(cipherText, ctLength); 
 
        System.out.println("cipher text: " + Utils.toHex(cipherText) 
                                                     + " bytes: " + ctLength); 
 
        // decryption pass 
 
        byte[] plainText = new byte[ctLength]; 
 
        cipher.init(Cipher.DECRYPT_MODE, key); 
 
        int ptLength = cipher.update(cipherText, 0, ctLength, plainText, 0); 
 
        ptLength += cipher.doFinal(plainText, ptLength); 
 
        System.out.println("plain text : " + Utils.toHex(plainText) 
                                                     + " bytes: " + ptLength); 
    } 
}

Open in new window

the problem is in the line: cipher.init(Cipher.ENCRYPT_MODE, key);
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial