Java encryption using AES


Nitescu Lucian

I am trying to create an encryption system using the AES class:

package Source;
import java.security.MessageDigest;
import java.util.Arrays;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import javax.crypto.spec.IvParameterSpec;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class AES {
  static String IV = "AAAAAAAAAAAAAAAA";
  static String plaintext = "test text 123\0\0\0"; /*Note null padding*/
  static String encryptionKey = "H4tch4repratygonetowil5h4kers";
  public static void main(String [] args) {
    try {

      System.out.println("==Java==");
      System.out.println("plain:   " + plaintext);

      byte[] cipher = encrypt(plaintext, encryptionKey);

      System.out.print("cipher:  ");
      for (int i=0; i<cipher.length; i++)
        System.out.print(new Integer(cipher[i])+" ");
      System.out.println("");

      String decrypted = decrypt(cipher, encryptionKey);

      System.out.println("decrypt: " + decrypted);

    } catch (Exception e) {
      e.printStackTrace();
    } 
  }

  public static byte[] encrypt(String plainText, String encryptionKey) throws Exception {
    Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding", "SunJCE");
    SecretKeySpec key = new SecretKeySpec(encryptionKey.getBytes("UTF-8"), "AES");
    cipher.init(Cipher.ENCRYPT_MODE, key,new IvParameterSpec(IV.getBytes("UTF-8")));
    return cipher.doFinal(plainText.getBytes("UTF-8"));
  }

  private static String decrypt(byte[] cipherText, String encryptionKey) throws Exception{
    Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding", "SunJCE");
    SecretKeySpec key = new SecretKeySpec(encryptionKey.getBytes("UTF-8"), "AES");
    cipher.init(Cipher.DECRYPT_MODE, key,new IvParameterSpec(IV.getBytes("UTF-8")));
    return new String(cipher.doFinal(cipherText),"UTF-8");
  }
}

Here is my code implementation:

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JButton;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextPane;


import Source.AES;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JTextArea;

public class first {

    private JFrame frame;
    private JPasswordField passwordField;
    private JTextArea txtrEnterTextHere;

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

    /**
     * Create the application.
     */
    public first() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JOptionPane.showMessageDialog(null, "Welcome to Encryption System! ");
        JButton btnNewButton = new JButton("Send Info");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

                String text = txtrEnterTextHere.getText();
                String pass = passwordField.getText();
                String str = null;
                try {
                    str = new String(AES.encrypt(text, pass));
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                txtrEnterTextHere.setText(str);

                JOptionPane.showMessageDialog(null, "Your intel has been encrypted!");

            }
        });
        frame.getContentPane().add(btnNewButton, BorderLayout.NORTH);

        passwordField = new JPasswordField();
        frame.getContentPane().add(passwordField, BorderLayout.SOUTH);

        txtrEnterTextHere = new JTextArea();
        txtrEnterTextHere.setText("ENTER TEXT HERE AND PASSWORD BELLOW!");

        frame.getContentPane().add(txtrEnterTextHere, BorderLayout.CENTER);
    }

}

How can I make the variable "str" ​​actually have the value of the encrypted text? I get an error at runtime and I have an empty field... can I change the byte[] to a string?

Update error:

java.lang.IllegalArgumentException: javax.crypto.spec.SecretKeySpec at Source.AES.encrypt(AES.java:41). Null key at (SecretKeySpec.java:96) at first $2.actionPerformed (first.java:62) at javax.swing.DefaultButtonModel.setPressed (unknown source) at javax.swing.DefaultButtonModel.fireActionPerformed (unknown source) ) at javax.swing.AbstractButton$Handler.actionPerformed (unknown source) at javax.swing.DefaultButtonModel.setPressed (unknown source) at javax.swing.AbstractButton$Handler.actionPerformed (unknown source) at javax.swing.JComponent .processMouseEvent (unknown source) at javax.swing.JComponent.processMouseEvent (unknown source) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased (unknown source) at java.awt.Component.processEvent (unknown source) , java.awt.Container.processEvent (unknown source) at java.awt.Container.java.awt.Component.dispatchEventImpl (unknown source). java.awt.LightweightDispatcher. at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source). retargetMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) Unknown source at java.awt.Container.dispatchEventImpl(java.awt.Window.dispatchEventImpl Unknown source of java.awt.Component.dispatchEventImpl (unknown source at java.awt.EventQueue.dispatchEventImpl) at java.awt.EventQueue.access$500 (unknown source) at java.awt.EventQueue$3.run (unknown source) at java.awt.EventQueue$3.run(unknown source) at java.security.AccessController.doPrivileged(native java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(method) method). Java's ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(unknown source), java.awt.EventQueue$4.run(unknown source), java.security.AccessController.doPrivileged(native method), java.awt.EventQueue$4.run (unknown source). security.ProtectionDomain $JavaSecurityAccessImpl.doIntersectionPrivilege (unknown source) in java.awt.

Grisha Weintraub

You have several problems here.

  1. You have Empty key at javax.crypto.spec.SecretKeySpecan exception because you didn't enter any password.

  2. After entering the password, you will get a similar exception Invalid AES key length: 8 bytesbecause the key is expected to be a certain length (see here for more info ).

  3. After using the hash to generate the key, you will encounter the following exception - javax.crypto.IllegalBlockSizeException: Input length not multiple of 16 bytes, because your encryption mode does not use padding. you can change it toAES/CBC/PKCS5Padding

Finally, with the following "encrypt" method, you will get what you want:

public static byte[] encrypt(String plainText, String encryptionKey) throws Exception {
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding", "SunJCE");
    MessageDigest sha = MessageDigest.getInstance("SHA-1");
    byte [] hashedPassword = sha.digest(encryptionKey.getBytes("UTF-8"));
    hashedPassword = Arrays.copyOf(hashedPassword, 16);
    SecretKeySpec key = new SecretKeySpec(hashedPassword, "AES");
    cipher.init(Cipher.ENCRYPT_MODE, key,new IvParameterSpec(IV.getBytes("UTF-8")));
    return cipher.doFinal(plainText.getBytes("UTF-8"));
}

(*) Of course, in order to decrypt the message, you also have to make similar changes in the "decrypt" method...

Related


Java encryption using AES

Nitescu Lucian I am trying to create an encryption system using the AES class: package Source; import java.security.MessageDigest; import java.util.Arrays; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec;

AES encryption and decryption using Java

Yogesh D Here's what I'm doing, it might seem a little clunky, but it might help you out. I get one BadPaddingException. Read almost all related topics but couldn't find a proper solution. I am new to encryption decryption programming and need to implement it

AES encryption and decryption using Java

Yogesh D Here's what I'm doing, it might seem a little clunky, but it might help you out. I get one BadPaddingException. Read almost all related topics but couldn't find a proper solution. I am new to encryption decryption programming and need to implement it

AES encryption using Java and decryption using Java

rkj : I'm making an application that requires Java-based AES encryption and JavaScript-based decryption. I am using the following code as base form for encryption. public class AESencrp { private static final String ALGO = "AES"; private static final byte

AES encryption using Java and decryption using Java

j I'm making an application that requires Java-based AES encryption and JavaScript-based decryption. I am using the following code as base form for encryption. public class AESencrp { private static final String ALGO = "AES"; private static final byte[] k

AES encryption using Java and decryption using Java

rkj : I'm making an application that requires Java-based AES encryption and JavaScript-based decryption. I am using the following code as base form for encryption. public class AESencrp { private static final String ALGO = "AES"; private static final byte

AES encryption using CryptoJS

Sergio David Romero I need to implement AES encryption using JavaScript. Use AES/CBC/NoPadding mode, and created a way to do it in 16-length blocks. I have solved it using Java. look like: public static String encrypt(byte[] key, byte[] initVector, String valu

AES encryption using CryptoJS

Sergio David Romero I need to implement AES encryption using JavaScript. Use AES/CBC/NoPadding mode, and created a way to do it in 16-length blocks. I have solved it using Java. looks like: public static String encrypt(byte[] key, byte[] initVector, String val

AES encryption in JAVA and InputStream

Esh2001: Sorry, JAVA beginner here. I am trying some encryption decryption examples. My method should return InputStream and should also accept Inputstream as parameter. The signature of the method is shown below public static InputStream encriptFile(InputStre

AES encryption and decryption in Java

death: I'm pretty new to encryption/decryption and have to encrypt some data files, but not entirely sure I'm going about it the right way. Right now, I have a script to encrypt all files that are not included in my repository, but the decryptor is included in

Java AES encryption with salt

Cody: Well, turns out I'm a big fan of encryption/decryption. I just don't get it. How can I enable Java encryption String message1 = "hello world";with encryption String salt = "mySalt";using AES? How to decrypt after encryption? It will help me a lot if you

AES encryption in JAVA and InputStream

Esh2001: Sorry, JAVA beginner here. I am trying some encryption decryption examples. My method should return InputStream and should also accept Inputstream as parameter. The signature of the method is shown below public static InputStream encriptFile(InputStre

Java AES encryption and decryption

Praneeth I want to encrypt and decrypt a password using 128 bit AES encryption and a 16 byte key. There was an error javax.crypto.BadPaddingExceptiondecrypting the value . Do I lose anything when decrypting? public static void main(String args[]) { Test t

Java AES encryption with salt

Cody: Well, turns out I'm a big fan of encryption/decryption. I just don't get it. How can I enable Java encryption String message1 = "hello world";with encryption String salt = "mySalt";using AES? How to decrypt after encryption? It will help me a lot if you

Java AES encryption and decryption

Praneeth I want to encrypt and decrypt a password using 128 bit AES encryption and a 16 byte key. There was an error javax.crypto.BadPaddingExceptiondecrypting the value . Do I lose anything when decrypting? public static void main(String args[]) { Test t

AES encryption and decryption in Java

death: I'm pretty new to encryption/decryption and have to encrypt some data files, but not entirely sure I'm going about it the right way. Right now, I have a script to encrypt all files that are not included in my repository, but the decryptor is included in

AES encryption and decryption in Java

death: I'm pretty new to encryption/decryption and have to encrypt some data files, but not entirely sure I'm going about it the right way. Right now, I have a script to encrypt all files that are not included in my repository, but the decryptor is included in

Java AES 256 encryption

rupen I have the following Java code to encrypt a string using a 64 character key. My question is will this be AES-256 encrypted? String keyString = "C0BAE23DF8B51807B3E17D21925FADF273A70181E1D81B8EDE6C76A5C1F1716E"; byte[] keyValue = hexStringToByte(keyString

AES encryption in Java and .NET

Igor Kulman I have a Java code that encrypts data using AES. Here is a code snippet that encodes a simple string String key="MySecretKeyABCDE"; SecretKeySpec skey = new SecretKeySpec(key.getBytes(), "AES"); byte[] ivbytes = new byte[]{0, 1, 2, 3, 4, 5, 6, 7, 8

AES encryption in JAVA and InputStream

Esh2001: Sorry, JAVA beginner here. I am trying some encryption decryption examples. My method should return InputStream and should also accept Inputstream as parameter. The signature of the method is shown below public static InputStream encriptFile(InputStre

Java AES encryption and decryption

Praneeth I want to encrypt and decrypt a password using 128 bit AES encryption and a 16 byte key. There was an error javax.crypto.BadPaddingExceptiondecrypting the value . Do I lose anything when decrypting? public static void main(String args[]) { Test t

Java AES encryption with salt

Cody: Well, turns out I'm a big fan of encryption/decryption. I just don't get it. How can I enable Java encryption String message1 = "hello world";with encryption String salt = "mySalt";using AES? How to decrypt after encryption? It will help me a lot if you

AES encryption and decryption in Java

death: I'm pretty new to encryption/decryption and have to encrypt some data files, but not entirely sure I'm going about it the right way. Right now, I have a script to encrypt all files that are not included in my repository, but the decryptor is included in

AES encryption and decryption in Java

death: I'm pretty new to encryption/decryption and have to encrypt some data files, but not entirely sure I'm going about it the right way. Right now, I have a script to encrypt all files that are not included in my repository, but the decryptor is included in

AES encryption in Java and .NET

Igor Kulman I have a Java code that encrypts data using AES. Here is a code snippet that encodes a simple string String key="MySecretKeyABCDE"; SecretKeySpec skey = new SecretKeySpec(key.getBytes(), "AES"); byte[] ivbytes = new byte[]{0, 1, 2, 3, 4, 5, 6, 7, 8