Data Encryption: Encrypting and Decrypting Data in Java

in #honouree2 years ago (edited)

Encrypting and decrypting data is something that I have always been curious about. As data is a valuable resource in today's society, it is essential to know how to protect it from people who do not have to understand it or are not the receiver of this data. Technically, the one with the data becomes the most powerful globally, specifically in the business sector. With this, everyone aims to have data for their purposes, but not everyone should be able to access every information in the world, as some may use it illegally. Therefore, knowing and understanding some of the encryption and decryption methods is something that I would want to learn in the long run.

Encountering this in Data Encryption is something that satisfies this curiosity. Data Encryption in Java is applied through arrays and strings. Through using specific methods, we are able to create strings into character arrays and vice versa. The resulting code would be a string of all the characters in the array through the new String(A) constructor, where A is the character array. To apply it vice versa, from string to character array, we use the S.toCharArray() where S is the string on which we base the resulting character array.

As said in the previous paragraph, Data Encryption in Java is applied through the concept of arrays, specifically character arrays, and strings. The property of arrays and strings using indices to refer to every character inside of it is important for data encryption. The indices will be an essential property to be used in the example data encryption method called the Caesar Cipher. Before talking about the Caesar Cipher, it is important to define the two main terms in encryption and decryption. Plaintext refers to the original message, while ciphertext refers to the encrypted message.

The Caesar Cipher is said to be the oldest form of encryption. This is used to protect messages sent to their military. The Caesar Cipher has a simple method of encrypting the said messages. If we base it on the English Alphabet, the encrypted words will have their letters moved three letters from the original letter. For example, if the original letter is "A", the encrypted letter would be "D". Starting from "X", it will just go back to the beginning of the alphabet, "A". Therefore, "Y" is "B" and "Z" is "C". So with this, moving the letters of the resulting ciphertext from this encryption method to the left three times will be the way to decrypt the message and go back to the original message or the plaintext.

Using the letters of the 26 lettered - English Alphabet like the array indices in Java, A would be 0, B would be 1, and so on, with Z being 25. It is important to note that the indices will always start at 0 in the Java programming language. Hence, with these assigned indices on the letters of the alphabet, we are able to make a formula of (i+3) mod 26, where "i" is the index, "+3" is the movement of the letters in encryption, "mod" refers to the remainder if (i+3) is divided by "26" which is the total number of letters in the English Alphabet.

In the attached pictures of this article is the application of the Caesar Cipher in Java. In these codes, the inputted messages, the encryption, and the decryption of the said messages are all done in uppercase letters. So, it will cause an error if these will be in lowercase. Other than that, the main aspect of this code is to make sure that the indices of the letters are correct using the encryption and decryption methods. To try the said code, we used the plaintext "THE EAGLE IS IN PLAY; MEET AT JOE'S". The encrypted message is "WKH HDJOH LV LQ SODB; PHHW DW MRH'V".

Data Encryption is possible through the usage of arrays and strings in Java. Through this, the data that we want to encrypt and decrypt can be done by using codes.

Posted using HonoureeScreenshot 20230927 133647.pngScreenshot 20230927 133732.pngScreenshot 20230927 144431.pngScreenshot 20230927 144529.png