Skip to Main Content

Java Security

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JAVA Encryption & Decryption

2922555Mar 29 2015 — edited Mar 29 2015

Good evening,

i am trying to do decryption over serial port(COM1, COM2 etc).

I use waspmote board and waspmote gateway

I do the encryption via waspmote board and i am trying to do the decryption via java(in an interface created by me)

I use AES 128 (AES/ECB/PKCS5Padding) on both

I have tried  everything and always i have the same error

Exception in thread "main" java.lang.IllegalArgumentException: Bad arguments

The wrong in in this line:

    public static String decrypt(byte [] encryptedData, String kleidh) throws Exception {

                byte[] keyRaw = kleidh.getBytes();

        SecretKeySpec skeySpec = new SecretKeySpec(keyRaw, "AES");

        Cipher c = Cipher.getInstance("AES/ECB/PKCS5Padding");

        System.out.println("hello1");

        c.init(Cipher.DECRYPT_MODE, skeySpec);

        System.out.println("hello2");

       //byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedData);

        System.out.println("hello100");

        byte[] decValue;

        decValue = c.doFinal(encryptedData,79,116);

        System.out.println("hello231");

        String decryptedValue = new String(decValue);

        return decryptedValue;

    }

The total code is the following:

package doulepse;

import java.security.*;

import java.security.spec.InvalidKeySpecException;

import javax.crypto.*;

import sun.misc.*;

import java.io.UnsupportedEncodingException;

import java.util.Arrays;

import java.util.Base64;

import java.util.logging.Level;

import java.util.logging.Logger;

import javax.crypto.spec.IvParameterSpec;

import javax.crypto.spec.SecretKeySpec;

import jssc.SerialPortList;

import jssc.SerialPort;

import jssc.SerialPortException;

public final class Doulepse {

    public Doulepse() throws UnsupportedEncodingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, Exception {

        SerialPort serialPort = new SerialPort("COM3");

        try {

            serialPort.openPort();//Open serial port

            serialPort.setParams(115200, 8, 1, 0);//Set params.

          

            String key = "libeliumlibelium";

          

            MySerialMonitor monitor = new MySerialMonitor();

            byte[] buffer = serialPort.readBytes(116);//Read frame size from serial port

            System.out.println(buffer.length);

            String word = new String(buffer, "UTF-8");

                     String realWord = decrypt(buffer, key);

                                    serialPort.closePort();//Close serial port                

        } catch (SerialPortException ex) {

            System.out.println(ex);

        }

    }

    public static String decrypt(byte [] encryptedData, String kleidh) throws Exception {

    

        byte[] keyRaw = kleidh.getBytes();

        SecretKeySpec skeySpec = new SecretKeySpec(keyRaw, "AES");

        Cipher c = Cipher.getInstance("AES/ECB/PKCS5Padding");

        System.out.println("hello1");

        c.init(Cipher.DECRYPT_MODE, skeySpec);

        System.out.println("hello2");

       System.out.println("hello100");

        byte[] decValue;

        decValue = c.doFinal(encryptedData,79,116);//offset of the encrypted message

        System.out.println("hello231");

        String decryptedValue = new String(decValue);

        return decryptedValue;

    }

    public static void main(String[] args) throws UnsupportedEncodingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, Exception {

        Doulepse kati = new Doulepse();

    }

}

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 26 2015
Added on Mar 29 2015
0 comments
1,530 views