How to Create a Crypter in Java (BASIC)
Submitted by GeePee on Monday, April 27, 2015 - 23:11.
Introduction:
This tutorial is on how to create a simple encrypter and decrypter tool in Java. Level - Basic.
Essentials:
First we need to create the essentials to start our scripts, create a class (I've named mine "Main.java", original eh?) then give it the normal Java Application main function...
The Main Constructor:
I've decided to skip the menu where the user may select encrypt or decrypt so I just let them encrypt then decrypt instantly...
As you can see, the above constructor script creates a new scanner to get system input from the user, then it asks the user for a line to encrypt, takes the line in to a new line string variable, prints out the return value of our crypt function (yet to be created). It then does the same passing the opposite boolean value to crypt for decrypting a string.
Crypt:
Now we have to create our crypt function. This function takes a boolean of whether to encrypt or decrypt, and a string to manipulate...
So, the above script creates two strings which we are using as characters for our encryption/decryption, we also create an integer index variable with a default value as zero (0).
The function takes two parameters, whether it is encrypting or decrypting, and the character to manipulate. We check the encrypt variable to decide whether to encrypt or decrypt.
Within the encrypting block, we iterate through each character within the string value of the CHECKS variable until we reach the current character we manipulating, we then return the character at the index within the REPLACES list.
We do the opposite for decrypting since it's simply the opposite.
- package Crypter;
- import java.util.Scanner;
- public class Main {
- new Main();
- }
- }
- public Main() {
- line = scanner.nextLine();
- }
First we create a new string variable named encrypted and set it to nothing/an empty string. Then we iterate through each character within the string given and append the value of another new function named cryptChar to the end of the value of the variable encrypted. We pass the boolean value through from the parameters which holds whether the string manipulation should be to encrypt or decrypt it, as well as the character it is currently processing in the iteration loop.
cryptChar:
Finally we have a function to encrypt the specific character which is being processed. Because this program difficulty is only basic/beginner, we are using the method of cryptography called Caeser Cipher, which is where each character is simply replaced with another, the replacement is always to same and it can be cracked very easily by simply encrypting and decrypting single characters at one time and recording the results.
- private char cryptChar(boolean encrypt, char c) {
- int index = 0;
- if (encrypt) {
- for (int i=0;i<CHECKS.length();i++) {
- if (c == CHECKS.charAt(i)) {
- index = i;
- break;
- }else{
- index++;
- }
- }
- return REPLACES.charAt(index);
- }else{
- for (int i=0;i<REPLACES.length();i++) {
- if (c == REPLACES.charAt(i)) {
- index = i;
- break;
- }else
- index++;
- }
- return CHECKS.charAt(index);
- }
- }
Add new comment
- 362 views