How to Create a Crypter in Java (BASIC)

Submitted by: 
Language: 
Visitors have accessed this post 1412 times.

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...

  1. package Crypter;
  2. import java.util.Scanner;
  3. public class Main {
  4. public static void main(String args[]) {
  5. new Main();
  6. }
  7. }

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...

  1. public Main() {
  2. Scanner scanner = new Scanner(System.in);
  3. System.out.println("Enter the line you wish to encrypt: ");
  4. String line = scanner.nextLine();
  5. System.out.println(crypt(true, line.toLowerCase()));
  6. System.out.println("Enter the line you wish to decrypt: ");
  7. line = scanner.nextLine();
  8. System.out.println(crypt(false, line.toLowerCase()));
  9. }

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...

  1. private String crypt(boolean encrypting, String line) {
  2. String encrypted = "";
  3. for (int i=0;i<line.length();i++) {
  4. encrypted += cryptChar(encrypting, line.charAt(i));
  5. }
  6. return encrypted;
  7. }

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.

  1. private char cryptChar(boolean encrypt, char c) {
  2. String CHECKS = ("abcdefghijklmnopqrstuvwxyz");
  3. String REPLACES = ("qwertyuiopasdfghjklzxcvbnm");
  4. int index = 0;
  5. if (encrypt) {
  6. for (int i=0;i<CHECKS.length();i++) {
  7. if (c == CHECKS.charAt(i)) {
  8. System.out.println("Yes");
  9. index = i;
  10. break;
  11. }else{
  12. System.out.println("No");
  13. index++;
  14. }
  15. }
  16. return REPLACES.charAt(index);
  17. }else{
  18. for (int i=0;i<REPLACES.length();i++) {
  19. if (c == REPLACES.charAt(i)) {
  20. index = i;
  21. break;
  22. }else
  23. index++;
  24. }
  25. return CHECKS.charAt(index);
  26. }
  27. }

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.


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.