postheadericon 3DES with CFM mode example in Java

package bsr;
import javax.crypto.*;
import javax.crypto.spec.*;
class D3ESCFM
public KeyGenerator keygenerator;
public SecretKey myDesKey;
Cipher c;
public IvParameterSpec iv;
public D3ESCFM() throws Exception

// Genrate the Key
keygenerator = KeyGenerator.getInstance("DESede");
keygenerator.init(new SecureRandom());
myDesKey = keygenerator.generateKey();

// Create the cipher
c = Cipher.getInstance("DESede/CFB/PKCS5Padding");

iv = new IvParameterSpec(new byte[8]);
public byte[] doEncryption(String s) throws Exception
   // Initialize the cipher for encryption
   c.init(Cipher.ENCRYPT_MODE, myDesKey,iv);

   //sensitive information
   byte[] text = s.getBytes();
// Encrypt the text
   byte[] textEncrypted = c.doFinal(text);

public String doDecryption(byte[] s)throws Exception

   // Initialize the same cipher for decryption
   c.init(Cipher.DECRYPT_MODE, myDesKey,iv);

           // Decrypt the text
   byte[] textDecrypted = c.doFinal(s);
                    return(new String(textDecrypted));

import bsr.*;
public class D3ES

public static void main(String[] argv) throws Exception
D3ESCFM d=new D3ESCFM();

String str=d.doEncryption("BipinRupadiya");;
System.out.println("Encrypted String : "+str);
System.out.println("Encrypted String : "+d.doDecryption(str));

To execute this code

  • open two command prompt
  • compile bsr package
  • compile
  • execute


mmitlearn said...

thank u sir

Blog Archive

Total Pageviews

© Powered by Blogger.