5 Replies - 520 Views - Last Post: 28 February 2012 - 12:42 AM Rate Topic: -----

#1 scriptdiddy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 27-February 12

Decryption error: Needs Input to be multiple of 16

Posted 27 February 2012 - 08:35 PM

I'm trying to develop a simple encryption/decryption program. The problem I am running into is when I attempt to decrypt the encrypted message, I get an error message stating that the Input length must be multiple of 16 when decrypting with cipher. I read somewhere that the encrypted message might need to be encoded before converting it to a string. I'm not sure how to do this? Or if there is an alternative way can someone please help me out?


import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;


public class Cryption {
	public static void cryption(String[] args, String message) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException {
		byte[] encodedKey = "ADBSJHJS12547896".getBytes();
		KeyGenerator keyGen = KeyGenerator.getInstance("AES");
		Key aesKey = keyGen.generateKey();
		
		System.out.println("CheckType: "+ Global.checkType);
		Cipher cipher = Cipher.getInstance("AES");
		cipher.init(Cipher.ENCRYPT_MODE, aesKey);
		byte[] input = Global.message.getBytes();
		
		// Check if clicked Encrypted
		if(Global.checkType==true) {
			// Encrypt
			byte[] messageEncrypted = cipher.doFinal(input);
			System.out.println("Encrypted Text: " + messageEncrypted);
			Global.encValue = messageEncrypted.toString();
		}
		
		// Check if clicked Decrypted
		if(Global.checkType==false) {
			//String mes = message;
			System.out.println(Global.message);
			System.out.println("Char lenght " + Global.message.length());
			byte[] mesByte = Global.message.getBytes();
			
			
			// Decrypt
			cipher.init(Cipher.DECRYPT_MODE, aesKey);
			byte[] messageDecrypted = cipher.doFinal(mesByte);
			System.out.println("Text Decrypted: " + new String(messageDecrypted));
		}
	}

}


This post has been edited by blackcompe: 27 February 2012 - 08:53 PM
Reason for edit:: Please use [code] tags when posting to the forum.


Is This A Good Question/Topic? 0
  • +

Replies To: Decryption error: Needs Input to be multiple of 16

#2 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1009
  • View blog
  • Posts: 2,185
  • Joined: 05-May 05

Re: Decryption error: Needs Input to be multiple of 16

Posted 27 February 2012 - 08:55 PM

Please post the Global class.
Was This Post Helpful? 0
  • +
  • -

#3 scriptdiddy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 27-February 12

Re: Decryption error: Needs Input to be multiple of 16

Posted 27 February 2012 - 09:04 PM

It as simple as the code below.



public class Global {
	public static String message;
	public static String encValue;
	public static Boolean checkType;
}



Was This Post Helpful? 0
  • +
  • -

#4 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1009
  • View blog
  • Posts: 2,185
  • Joined: 05-May 05

Re: Decryption error: Needs Input to be multiple of 16

Posted 27 February 2012 - 09:18 PM

Post something that compiles, runs, and re-creates the problem you're having. You're asking us to help you diagnose a problem without giving us the resources to do so.

This line will print null because it doesn't have a value. I can't specify it because I don't know what is.

System.out.println("CheckType: " + Global.checkType);


Also, what is args?

public static void cryption(String[] args, String message)



I know you don't use it, but is it relevant?

This post has been edited by blackcompe: 27 February 2012 - 09:19 PM

Was This Post Helpful? 0
  • +
  • -

#5 scriptdiddy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 27-February 12

Re: Decryption error: Needs Input to be multiple of 16

Posted 27 February 2012 - 09:24 PM

Below is my Main.java. The Global.java is above as well as the Cryption.java.

Main.java

import java.awt.EventQueue;

public class Main extends JFrame {

    private static void setUp() throws Exception {
    }
    
	private JPanel contentPane;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) throws Exception {
		setUp();
		
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Main frame = new Main();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Main() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		final JTextPane textPane = new JTextPane();
		textPane.setBounds(10, 10, 434, 226);
		contentPane.add(textPane);
		
		JLabel label = new JLabel("");
		label.setBounds(230, 10, 220, 89);
		contentPane.add(label);
		
		JLabel label_1 = new JLabel("");
		label_1.setBounds(10, 99, 220, 89);
		contentPane.add(label_1);
		
		JButton btnDecrypt = new JButton("Decrypt");
		btnDecrypt.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Global.message = textPane.getText();
				Global.checkType=false;
				Cryption cryption = new Cryption();
				try {
					cryption.cryption(null, Global.message);
				} catch (InvalidKeyException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (NoSuchAlgorithmException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (NoSuchPaddingException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (IllegalBlockSizeException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (BadPaddingException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (InvalidAlgorithmParameterException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (UnsupportedEncodingException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		btnDecrypt.setBounds(230, 248, 220, 29);
		contentPane.add(btnDecrypt);
		
		JButton btnEncrypt = new JButton("Encrypt");
		btnEncrypt.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Global.message = textPane.getText();
				Global.checkType = true;
				Cryption cryption = new Cryption();
				try {
					cryption.cryption(null, Global.message);
				} catch (InvalidKeyException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (NoSuchAlgorithmException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (NoSuchPaddingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalBlockSizeException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (BadPaddingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (InvalidAlgorithmParameterException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				textPane.setText(Global.encValue);
			}
		});
		btnEncrypt.setBounds(6, 248, 220, 29);
		contentPane.add(btnEncrypt);
	}

}


Was This Post Helpful? 0
  • +
  • -

#6 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1009
  • View blog
  • Posts: 2,185
  • Joined: 05-May 05

Re: Decryption error: Needs Input to be multiple of 16

Posted 28 February 2012 - 12:42 AM

Global.message was the string you encrypted initially. You never modify it. Then you try decrypting it.

byte[] mesByte = Global.message.getBytes();
			
// Decrypt
cipher.init(Cipher.DECRYPT_MODE, aesKey);
byte[] messageDecrypted = cipher.doFinal(mesByte);



It's not supposed to be decrypted; it's an input. It's probably not a multiple of 16 bytes, which is (I'm assuming) a property of all encrypted output and that's why the exception is thrown.

Perhaps you meant to decrypt the bytes (or string) that you encrypted.

byte[] messageEncrypted = cipher.doFinal(input);
System.out.println("Encrypted Text: " + messageEncrypted);
Global.encValue = messageEncrypted.toString();



Send Global.encValue to doFinal when decrypting. I wouldn't be surprised if that resulted in a problem as well because Global.encValue doesn't have the right value. You're giving it the string value of an array, Global.encValue = messageEncrypted.toString();, not the string value of the bytes, Global.encValue = new String(messageEncrypted);.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1