How can you easy Encrypt and Decrypt data with Java 8

0
40
Spread the love

How can you easy Encrypt and Decrypt data with Java 8:

welcome again in this tutorial, so today we are going to show a littile example about How can you easy Encrypt and Decrypt data with Java 8, in this demo we are going to use a simple key to encrypt and decrypt a message.

cyber security

So in our case we will use number 10 as a key to encrypt and decrypt data.

What is Encryption ?

In cryptography, encryption is the process of encoding a message or information in such a way that only authorized parties can access it and those who are not authorized cannot.

encryption decryption

What is decryption?

Decryption is the process of taking encoded or encrypted text or other data and converting it back into text that you or the computer can read and understand. This term could be used to describe a method of unencrypting the data manually or unencrypting the data using the proper codes or keys

So, as we mentioned before we are going to use the same key to encrypt and decrypt the message.

our message in the text editor notpad will be : “Hello everybody at YourTechPage”.

we save our message in file called Mymessage.txt

Encryption case:

Let’s begin first with encryption Sample, we ‘ll create EncryptionTest Class.

package com.test.security;

import java.io.FileInputStream;
import java.io.FileOutputStream;

public class EncryptionTest {
	public static void main(String[] args) {
		try {
			int test;
			FileInputStream inputStream = 
                        new FileInputStream("Mymessage.txt");
			FileOutputStream outputStream = 
                        new FileOutputStream("outputText.txt");

			while ((test = inputStream.read()) != -1) {
				test -= 10; // 10 is the encryption key .
				System.out.print((char) test);
				outputStream.write(test);
			}
			outputStream.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
}

Let’s create other java class we call it DecryptionTest to decrypt the encoded message, we still use the same key number = 10

package com.test.security;

import java.io.FileInputStream;
import java.io.FileOutputStream;

class DecryptionTest {
	public static void main(String[] args) {
		try {
			int test;

			FileInputStream inputStream = new 
                        FileInputStream("outputText.txt");
			FileOutputStream outputStream = new 
                        FileOutputStream("newTextMessage.txt");
			while ((test = inputStream.read()) != -1) {
				test += 10; // 10 is the decryption key and the same key used as well.

				System.out.print((char) test);
				outputStream.write(test);
				Thread.sleep(5);
			}
			outputStream.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
}

the final output is the same message as the first one, “Hello everybody at YourTechPage”.

LEAVE A REPLY

Please enter your comment!
Please enter your name here