Showing posts with label Encryption. Show all posts
Showing posts with label Encryption. Show all posts

Monday

Triple DES (3DES) with CBC mode algorithm in java

import java.security.Key;
import java.util.Arrays;

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

import org.apache.commons.codec.binary.Hex;

public static void main(String[] args) {
      String yourOwnKey = "ASDFGHJKLZXCVBNMQWERTYUI", originalText = "Give your text to encrypt.";
        decrypt(encrypt(originalText, yourOwnKey), yourOwnKey);
    }
public static String encrypt(String originalText, String yourOwnKey) {
      String encryptedText = null;
      byte[] bytes = null;
     
      try {
            bytes = originalText.getBytes();
            bytes = Arrays.copyOf(bytes, ((bytes.length + 7) / 8) * 8);
           
            Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
            SecretKey key = new SecretKeySpec(yourOwnKey.getBytes(), "DESede");
            IvParameterSpec iv = new IvParameterSpec(new byte[8]);
             
            cipher.init(Cipher.ENCRYPT_MODE, (Key) key, iv);
            byte[] encryptByte = cipher.doFinal(bytes);
            encryptedText = new String(Hex.encodeHex(encryptByte));
           
            System.out.println("originalText: " +originalText+ " --> encryptedText: " + encryptedText);
         
            } catch (Exception e) {
                  //throw || log
            }
      return encryptedText;
    }
public static String decrypt(String encryptedTxt, String yourOwnKey) {
      String decryptedText  = null;
      byte[] bytes = null;
     
      try {
            bytes = Hex.decodeHex(encryptedTxt.toCharArray());
           
            Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
            SecretKey key = new SecretKeySpec(yourOwnKey.getBytes(), "DESede");
            IvParameterSpec iv = new IvParameterSpec(new byte[8]);
             
            cipher.init(Cipher.DECRYPT_MODE, (Key) key, iv);
            byte[] decryptByte = cipher.doFinal(bytes);
           
            decryptedText = new String(decryptByte);
            if (decryptedText.indexOf((char) 0) > 0)
                  decryptedText = decryptedText.substring(0, decryptedText.indexOf((char) 0));
           
            System.out.println("encryptedTxt: " +encryptedTxt+ " --> decryptedText: " + decryptedText);
          
            } catch (Exception e) {
                  //throw || log
            }
      return decryptedText;
    }

Thursday

Encrypt/Decrypt string with DES algorithm in java

public static void main(String[] args) throws Exception {
           
            byte[] message = "Give your text to encrypt.".getBytes();
            byte[] keyValue = "YourOwnKey".getBytes();
           
            Cipher cipher = Cipher.getInstance("DES");
            DESKeySpec dks = new DESKeySpec(keyValue);
            SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
            SecretKey desKey = skf.generateSecret(dks);
           
            cipher.init(Cipher.ENCRYPT_MODE, desKey);
            byte[] textEncrypted = cipher.doFinal(message);
            String encrypted = DatatypeConverter.printBase64Binary(textEncrypted);
            System.out.println("Text Encrypted : " + encrypted);
           
            cipher.init(Cipher.DECRYPT_MODE, desKey);
            byte[] texDecrypted = cipher.doFinal(textEncrypted);
            System.out.println("Text Decryted : " + new String(texDecrypted) );
           
      }