security

DES with CTR example

This is an example of how to encrypt data using the DES algorithm in CTR mode. Doing data encryption with the Data Encryption Standard algorithm, in CTR mode implies that you should:

  • Create a byte array to be used as initial password to be encrypted, a byte array to be used as a key and a a byte array to be used as initialization vector.
  • Create a new SecretKeySpec for the DES algorithm.
  • Create a new IvParameterSpec, using the initialization vector byte array. Create a new Cipher for the “DES/CTR/NoPadding” transformation, using an org.bouncycastle.jce.provider.BouncyCastleProvider(), with the getInstance(String transformation, String provider) API method.
  • Initialize the cipher in encryption mode using the key and iv specs created above, with the init(int opmode, Key key, AlgorithmParameterSpec params) API method.
  • Do the encryption of the initial byte array password, with update(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset) and doFinal(byte[] output, int outputOffset) API methods of the Cipher.
  • Initialize the cipher again, this time in decryption mode and do the decryption,

as described in the code snippet below.

package com.javacodegeeks.snippets.core;

import java.security.Security;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;


public class Main {

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

  

  Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

  byte[] pass = "www.javacodegeeks.com".getBytes();

  byte[] sKey = new byte[]{0x01, 0x23, 0x45, 0x67, (byte) 0x89, (byte) 0xab, (byte) 0xcd,


(byte) 0xef};

  

  byte[] initializationVector = new byte[]{0x00, 0x01, 0x02, 0x03, 0x00, 0x00, 0x00, 0x01};


  SecretKeySpec key = new SecretKeySpec(sKey, "DES");

  

  IvParameterSpec ivSpv = new IvParameterSpec(initializationVector);

  Cipher c = Cipher.getInstance("DES/CTR/NoPadding", "BC");

  System.out.println("input : " + new String(pass));


  // encryption pass

  c.init(Cipher.ENCRYPT_MODE, key, ivSpv);

  byte[] encText = new byte;

  int ctLen = c.update(pass, 0, pass.length, encText, 0);

  ctLen += c.doFinal(encText, ctLen);

  System.out.println("cipher: " + new String(encText).getBytes("UTF-8").toString() + " bytes: " + ctLen);


  // decryption pass

  c.init(Cipher.DECRYPT_MODE, key, ivSpv);

  byte[] decrpt = new byte;

  int ptLen = c.update(encText, 0, ctLen, decrpt, 0);

  ptLen += c.doFinal(decrpt, ptLen);

  System.out.println("plain : " + new String(decrpt) + " bytes: " + ptLen);
    }
}

Output:

input : www.javacodegeeks.com
cipher: [B@4d63e95 bytes: 21
plain : www.javacodegeeks.com bytes: 21

 
This was an example of how to encrypt data using the DES algorithm in CTR mode in Java.

Byron Kiourtzoglou

Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron 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
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button