[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Simple Symmetric Encryption in Java problem

Posted on 2009-12-28
5
Medium Priority
?
1,698 Views
Last Modified: 2013-11-23
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

0
Comment
Question by:jazzIIIlove
  • 3
  • 2
5 Comments
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 26130538
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
0
 
LVL 40

Accepted Solution

by:
Gurvinder Pal Singh earned 2000 total points
ID: 26130551
Similar issue is discussed here, have you tried this
http://forums.sun.com/thread.jspa?threadID=581327
0
 
LVL 12

Author Comment

by:jazzIIIlove
ID: 26130652
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

0
 
LVL 12

Author Comment

by:jazzIIIlove
ID: 26130658
the problem is in the line: cipher.init(Cipher.ENCRYPT_MODE, key);
0
 
LVL 40

Assisted Solution

by:Gurvinder Pal Singh
Gurvinder Pal Singh earned 2000 total points
ID: 26130782
0

Featured Post

Cyber Threats to Small Businesses (Part 2)

The evolving cybersecurity landscape presents SMBs with a host of new threats to their clients, their data, and their bottom line. In part 2 of this blog series, learn three quick processes Webroot’s CISO, Gary Hayslip, recommends to help small businesses beat modern threats.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you thought ransomware was bad, think again! Doxware has the potential to be even more damaging.
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month19 days, 2 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question