How to Encrypt and Decrypt any Text using Java

How to Encrypt and Decrypt any Text using Java
How to Encrypt and Decrypt any Text using Java

Hello everyone, Welcome to techxon.
In this Java tutorial we discuss How to Encrypt and Decrypt any Text using Java Programming Language. You don't need to use any other third party Jar libraries for do this. I simply used core Java functions for do Encrypt and Decrypt. First we need to create two simple static methods for do this.

  1. public static String encryptText(String yourText) { }
  2. public static String decryptText(String yourText) { }

Text Encryption Method

    //Text encryption method
    public static String encryptText(String yourText) {
        try {
            String encryptedText = "";

            //Text encryption process
            for (char c : yourText.trim().toCharArray()) {
                int val = c + 10;
                c = (char) val;
                encryptedText += c;
            }

            return encryptedText;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }

Text Decryption Method

    //Text decryption method
    public static String decryptText(String yourText) {
        try {
            String decrptedText = "";

            //Text decryption process
            for (char c : yourText.trim().toCharArray()) {
                int val = (int) c - 10;
                c = (char) (val);
                decrptedText += c;
            }

            return decrptedText;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }

Testing Encryption Method

    public static void main(String[] args) {

        System.out.println("Encrypted Text : " + encryptText("Hello World"));

    }

Output : Encrypted Text : Rovvy*ay|vn

Testing Decryption Method

   public static void main(String[] args) {

        System.out.println("Decrypted Text : " + decryptText("Rovvy*ay|vn"));

    }

Output : Decrypted Text : Hello World

Complete Java Class

package yourpackagename;

/**
 *
 * @author Gayan Ruchiranga
 */
public class TextEnccryptAndDecrypt {

    //Text encryption method
    public static String encryptText(String yourText) {
        try {
            String encryptedText = "";

            //Text encryption process
            for (char c : yourText.trim().toCharArray()) {
                int val = c + 10;
                c = (char) val;
                encryptedText += c;
            }

            return encryptedText;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }

    //Text decryption method
    public static String decryptText(String yourText) {
        try {
            String decrptedText = "";

            //Text decryption process
            for (char c : yourText.trim().toCharArray()) {
                int val = (int) c - 10;
                c = (char) (val);
                decrptedText += c;
            }

            return decrptedText;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {

        System.out.println("Encrypted Text : " + encryptText("Hello World"));
        System.out.println("Decrypted Text : " + decryptText("Rovvy*ay|vn"));

    }

}

Congratulations! Now you can Encrypt and Decrypt any Text using Java. Remember this help and leave a comment. Thank You! 

Comments