The views expressed on this blog are my own do not necessarily reflect the views of my employer.

Friday, February 11, 2022

Oneway Encryption , Encryption and Decryption - Sample codes

When we want to encrypt the sensitive data like user passwords e t c i am giving 2 different approches here


1. One way encryption : oneway encryption is like once we encrypt the data we can't decrypt, we should live with that.
2. Encryption and Decrytion : with this approcach we can enrcypt the given data as well as decrypt the same back to original text.

   Sample Java:
import java.security.MessageDigest;
import javax.crypto.Cipher;

import javax.crypto.spec.SecretKeySpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class PasswordManagement {
   private PasswordManagement() { }
     public static String oneWayEncryptKey(String plainText) {
       String strEncrypt = "";
       try {
       MessageDigest md = MessageDigest.getInstance("SHA");
       md.update(plainText.getBytes("UTF-8"));
       strEncrypt = (new BASE64Encoder()).encode(md.digest());
       } catch (Exception e) {
      System.out.println("String Encryption Exception : " + e);
      } return strEncrypt;
    }

// This method is required for ENCRYPTION
  public static String encryptkey(String strEncryptText) {
    String strEncrypt = "";
    try {
      BASE64Encoder encoder = new BASE64Encoder();
      Cipher cipher = getCipher();
      cipher.init(Cipher.ENCRYPT_MODE,getSecKey());
      byte[] clearText = strEncryptText.getBytes("UTF8");
      byte[] cipherText = cipher.doFinal(clearText);
      strEncrypt = encoder.encode(cipherText);
      } catch (Exception e) {
      System.out.println("String Encryption Exception : " +e);
      }
    return strEncrypt;
  }

// This method is required for DECRYPTION
  public static String decryptkey(String strEncryptText) {
    String strEncrypt = "";
    try {
      BASE64Decoder decoder = new BASE64Decoder();
      Cipher cipher = getCipher();
      cipher.init(Cipher.DECRYPT_MODE, getSecKey());
      byte[] decryptByte = decoder.decodeBuffer(strEncryptText);
      byte[] cipherText = cipher.doFinal(decryptByte);
      strEncrypt = new String(cipherText, "UTF8");
      }catch (Exception e) {
      System.out.println("String Decryption Exception : " + e);
      }
    return strEncrypt;
  }

  private static SecretKey getSecKey() {
    byte[] keyBytes ={ 1, 2, 3, 4, 5, 6, 7, 8 };
    SecretKey secretKey = new SecretKeySpec(keyBytes,"DES");
    return secretKey;
  }

  private static Cipher getCipher() {
    Cipher cipher = null;
    try {
      cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
    } catch(Exception e) {
      System.out.println("Cipher getting Exception : " + e);
    } return cipher;
  }

  public static void main(String[] args) {
    PasswordManagement class1 = new PasswordManagement();
    String oneWayEncrypt = null;
    String encryptText =null;
    String decryptText = null;
    oneWayEncrypt =class1.oneWayEncryptKey("I@m&uperm@n");
    encryptText = class1.encryptkey("I@m&uperm@n");
    decryptText = class1.decryptkey(encryptText);
    System.out.println("One way encryption : " + oneWayEncrypt);
    System.out.println("encryption : " + encryptText);
     System.out.println("Decryption : " + decryptText);
  }
}


Result :
One way encryption : w6u00KTaZtxtar8eKt9z9sfk4zE=
encryption : 8mrVHuJQK0B2nu0DeZUiaA==
Decryption : I@m&uperm@n

No comments:

Post a Comment