Simple Example for Java Encryption and Decryption?

Posted on 2009-02-24
Last Modified: 2012-06-21
I need to do a simple example of java encryption and decryption. Does anyone know of a good simple library and what functions I would use. All I need to do is encrypt and decrypt between two machines. But I can manually move the key.
Question by:alexmcferron
    LVL 86

    Expert Comment

    LVL 9

    Accepted Solution

    Try Java Cryptography API

    I have attached some code which I have used in my project.
    Should be useful.

    First you need to generate a key using
    You can generate a key using this function and save it as binary file in server.

    to encrypt use

    to decrypt use
    import javax.crypto.*;
    import javax.crypto.spec.*;
    import java.util.*;
    public class CipherText
            public static byte[] generateAESKey() throws Exception
                    KeyGenerator kgen = KeyGenerator.getInstance("AES");
                    // Generate the secret key specs.
                    //SecretKey skey = kgen.generateKey();
                    return skey.getEncoded();
            public static byte[] encryptAES(byte key[], byte data[]) throws Exception
                   SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
                   // Instantiate the cipher
                   Cipher cipher = Cipher.getInstance("AES/CTR/NoPadding");
                   cipher.init(Cipher.ENCRYPT_MODE, skeySpec, new IvParameterSpec(new byte[16]));
                   return cipher.doFinal(data);
            public static byte[] decryptAES(byte key[], byte msg[]) throws Exception
                   SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
                   // Instantiate the cipher
                   Cipher cipher = Cipher.getInstance("AES/CTR/NoPadding");
                   cipher.init(Cipher.DECRYPT_MODE, skeySpec, new IvParameterSpec(new byte[16]));
                   return cipher.doFinal(msg);

    Open in new window


    Author Closing Comment

    Thank you so much! worked perfectly!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Suggested Solutions

    Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
    Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
    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 …

    779 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

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now