RSA Encryption using Private and Public Key

Cryptography

In some projects, there is a need to encrypt some data to prevent hackers to get it. Encryption is one of the ways to achieve data security. Encryption will convert the data in the plain text into an unreadable text called the cipher text. Decryption is converting the cipher text back to plain text. Encryption and decryption are part of cryptography. But, keep on your mind because hackers have million ways to get plain data even you encrypted it with very good encryption algorithm.

In this time, I want to give you an example how you can encrypt your secret data using RSA algorithm with private and public-key in Java. RSA is one of the algorithm for public-key cryptography that is based on factoring large integers. RSA stands for Ron Rivest, Adi Shamir and Leonard Adleman, who first publicly described it.

And here is the output:

 

3 Comments
  1. Irnawan Eka Putra

    Hi dude, i would like to add something in your code. I think we need to convert byte encrypted to Hex first, if you are not convert to hex the string not readable, you may need the encrypted string to keep it and used.

    I adding 2 methods in your class:

    public String binaryToHexString(byte[] bytes) {
    String result = “”;

    for (int i = 0; i < bytes.length; i++) {
    String byteStr = Integer.toHexString(bytes[i]);
    if (byteStr.length() 2) {
    byteStr = byteStr.substring(byteStr.length() – 2);
    }
    result += byteStr;
    }
    return result;
    }

    public byte[] hexStrToBinary(String hexStr)
    throws Exception {
    byte[] result = new byte[hexStr.length() / 2];
    if ((result.length * 2) != hexStr.length()) {
    throw new Exception(“error.unevenHexDigits”);
    }
    int index = 0;
    for (int i = 0; i < hexStr.length(); i += 2) {
    String subString = hexStr.substring(i, i + 2);
    try {
    result[index++] = (byte) Integer.parseInt(subString, 16);
    } catch (NumberFormatException nfe) {
    throw new Exception("error.numberFormat", nfe);
    }
    }
    return result;
    }

    and then your main class will be like this:

    // Get encrypted text
    byte[] encryptedByte = secUtils.encrypt(plain.getBytes(),
    secUtils.getPublicKey());
    String encrypted = secUtils.binaryToHexString(encryptedByte);
    System.out.println("Encrypted text: " + encrypted);

    //and when you want to use the string to decrypte you need to converting back to btye, in order to use decrypted method
    byte[] encryptedByteRestore = null;
    try {
    encryptedByteRestore = secUtils.hexStrToBinary(encrypted);
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    // Get decrypted text
    byte[] decryptedByte = secUtils.decrypt(encryptedByteRestore ,
    secUtils.getPrivateKey());
    String decrypted = new String(decryptedByte);
    System.out.println("Plain text: " + decrypted);

    And then the result will be like this:

    Encrypted text2: 2882745c625f4c00517f7f44622a148571914981f3005fe1872bd5c5d3bcdf4619e39afe146fa783dd1e261ffe649adbc82c48fca2bd287dceb03095ce03271bd897ee418aef3fe12cdb4c0031437be82ab91a4e41ed197c56ec017a440992b9051469afb0e9772b728a4aea33469235a3b9b9f04900fc51906df36cd20ac78b3f862fd61012ed33a1c98df332d57f9020da27ff557e15f50a4f93ed988b6666a5ec413cb49a77be2ddbb64d043c400f0fed00d9cff3e3a1f6321ecc230703a794a52c73bfe4478fee5f966f152b4d9a37c6b976195c013586db7cdf479c67db7afc05a8665c884f4fa6a22c6bf344c4bba539d80655384ebc9ce167a18d459d
    Plain text: This is my secret message in plain text

    Regards,
    Irnawan Eka

    1. Robertus Lilik Haryanto

      Hi Irnawan Eka,

      I got what you mean, I also used as you mentioned in my code. But in this article, I just wanna show the simple way to encrypt data and I removed the Hexadecimal converting process. Not all people need to convert to Hexadecimal, right? It depends on their requirement.

      Thanks

  2. JS_KUN

    This is very good and simple Tutorial about the Asymmetric Encryption using Java. Good Job Bro…

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*