security

Simple symmetric key encrypt/decrypt

In this example we shall show you how to encrypt and decrypt using a symmetric key. In the case of a symmetric key, the same key is used to encrypt and decrypt the data. Because both parties have the same key, the decryption essentially is performed by reversing some part of the encryption process. To encrypt and decrypt a String using a symmetric key one should perform the following steps:

  • Create a Key Object, using the KeyGenerator, for the DESede algorithm.
  • Create a Cipher that implements the DESede transformation, with the getInstance(String algorithm) API method.
  • Encrypt an initial input String. Initialize the cipher in encryption mode with the key, using the init(int opmode, Key key) API method. Then encrypt the byte array of the String and return the encrypted byte array, using the doFinal(byte[] input) API method of the Cipher. This step is described in the encryptF(String input, Key pkey, Cipher c) method of the example.
  • Decrypt the encrypted byte array. Initialize the key and the cipher in a decryption mode, using the same key, decrypt the byte array and return a new String representation from the decrypted byte array. This step is described in the decryptF(byte[] encryptionBytes, Key pkey, Cipher c) method of the example.

Let’s take a look at the code snippet that follows:

package com.javacodegeeks.snippets.core;

import java.security.InvalidKeyException;
import java.security.Key;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;

public class Main {

    static String algorithm = "DESede";

    public static void main(String[] args) throws Exception {

  Key symKey = KeyGenerator.getInstance(algorithm).generateKey();

  Cipher c = Cipher.getInstance(algorithm);

  byte[] encryptionBytes = encryptF("texttoencrypt",symKey,c);

  System.out.println("Decrypted: " + decryptF(encryptionBytes,symKey,c));
    }

    private static byte[] encryptF(String input,Key pkey,Cipher c) throws InvalidKeyException, BadPaddingException,

IllegalBlockSizeException {

  c.init(Cipher.ENCRYPT_MODE, pkey);

  byte[] inputBytes = input.getBytes();

  return c.doFinal(inputBytes);
    }

    private static String decryptF(byte[] encryptionBytes,Key pkey,Cipher c) throws InvalidKeyException,

BadPaddingException, IllegalBlockSizeException {

  c.init(Cipher.DECRYPT_MODE, pkey);

  byte[] decrypt = c.doFinal(encryptionBytes);

  String decrypted = new String(decrypt);

  return decrypted;
    }
}

Output:

Decrypted: texttoencrypt

 
This was an example of how to encrypt and decrypt using a symmetric key in Java.

Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button