Skip to content

DimazRizky/Simple-cryptography-with-4-letter-key-in-TERMUX

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 

Repository files navigation

WHAT IS THE PURPOSE OF THIS CODE?

This code is used to encrypt or can also decrypt plaintext using Java as the main language.

⚠️This code only runs on terminal/shell and does not display GUI!!

INSTALLING GUIDE IN TERMUX


How to running the Code in Termux?

First clone this project

Open Termux and run the following commands to install OpenJDK v21 or latest:

termux-setup-storage
pkg upgrade
pkg update 
pkg install openjdk-21

Compile Java Code Run the following command to compile the Java file:

javac crypto.java

If there are no errors, this will create a file named crypto.class

Run the Program:

java crypto.java

By following these steps, you can run this code in Termux, read text from user input, and produce an encrypted or decrypted result based on the chosen mode.


CODE IDENTIFICATION


I will explain in detail the meaning of the code I wrote to make it easier for you to read this code.

-- First, import the scanner library

Scanner Library

import java.util.Scanner;

Imports the Scanner class from java.util

Main Class Declaration

public class crypto {

This declares a class named crypto. Inside this class, we define all methods needed for encryption, decryption, and creating the shifted alphabet.

createShiftedAlphabet Function

public static String createShiftedAlphabet(String key) {

String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

This function takes key (as a string) from the user and creates a shifted version of the alphabet based on that key.

String alphabet contains the basic A-Z alphabet as a reference.


int keyLength = key.length();
    int startIndex = alphabet.indexOf(key.charAt(0));
    String shiftedAlphabet = key;

keyLength stores the length of key, and startIndex determines the position of the first letter of key in alphabet. shiftedAlphabet is initialized with the key.

for (int i = startIndex + keyLength; i < alphabet.length(); i++) {
        shiftedAlphabet += alphabet.charAt(i);
    }

This first loop appends the letters after the key until the end of the alphabet.

for (int i = 0; i < startIndex; i++) {
        shiftedAlphabet += alphabet.charAt(i);
    }

And the second loop appends the letters that come before the key (letters not already in shiftedAlphabet).

return shiftedAlphabet;
}

This returns shiftedAlphabet, which contains the alphabet shifted according to the key.

--

Encrypt Function

public static String encrypt(String text, String shiftedAlphabet) {

The encrypt function takes two parameters: text (the text to be encrypted) and shiftedAlphabet (the shifted alphabet created using key).

String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    text = text.toUpperCase();
    StringBuilder encryptedText = new StringBuilder();

This initializes alphabet, converts text to uppercase, and creates a StringBuilder to store the encrypted result.

for (char c : text.toCharArray()) {

The for loop iterates over each character in text.

if (Character.isLetter(c)) {
            int index = alphabet.indexOf(c);
            encryptedText.append(shiftedAlphabet.charAt(index));
        } else {
            encryptedText.append(c);
        }
    }

If the character is a letter, the program finds its index in alphabet and appends the corresponding character in shiftedAlphabet to encryptedText. If it’s not a letter, the character is appended directly.

return encryptedText.toString();
}

This returns encryptedText as the final encrypted string.


Decrypt Function

public static String decrypt(String text, String shiftedAlphabet) {

The decrypt function takes text and shiftedAlphabet and reverses the encryption to restore the original text.

String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    text = text.toUpperCase();
    StringBuilder decryptedText = new StringBuilder();

This initializes alphabet, converts text to uppercase, and creates a StringBuilder to store the decrypted text.

for (char c : text.toCharArray()) {
        if (Character.isLetter(c)) {
            int index = shiftedAlphabet.indexOf(c);
            decryptedText.append(alphabet.charAt(index));
        } else {
            decryptedText.append(c);
        }
    }

If the character is a letter, the program finds its index in shiftedAlphabet and retrieves the corresponding letter in alphabet to restore the original character. If it’s not a letter, it’s directly appended to decryptedText.

return decryptedText.toString();
}

This returns decryptedText as the final decrypted string.


## main Function - User Interface
public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);

This creates a Scanner object to read user input from the console.

System.out.println("Choose a mode:");
    System.out.println("1. Encrypt");
    System.out.println("2. Decrypt");
    System.out.print("Enter your choice (1 or 2): ");
    int choice = scanner.nextInt();
    scanner.nextLine();

The program prompts the user to select a mode: 1 for encryption or 2 for decryption. scanner.nextLine() clears the buffer after reading the integer.

System.out.print("Enter text: ");
    String text = scanner.nextLine();

    System.out.print("Enter KEY (e.g., RSTU): ");
    String key = scanner.nextLine().toUpperCase();

The program then prompts the user to enter the text to be encrypted or decrypted, as well as the key.

String shiftedAlphabet = createShiftedAlphabet(key);
    System.out.println("Shifted alphabet: " + shiftedAlphabet);

createShiftedAlphabet is called with key to create the shiftedAlphabet, which is then printed.

if (choice == 1) {
        String encryptedText = encrypt(text, shiftedAlphabet);
        System.out.println("Encrypted result: " + encryptedText);
    } else if (choice == 2) {
        String decryptedText = decrypt(text, shiftedAlphabet);
        System.out.println("Decrypted result: " + decryptedText);
    } else {
        System.out.println("Invalid choice. Program exits.");
    }
    scanner.close();
}

If choice is 1, the text is encrypted and the result is printed; if it is 2, the text is decrypted. If the choice is invalid, the program displays a message and exits.


That's all for those of you who read my explanation and thank you.

MIT License

Copyright (c) 2024 DimazRizky

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages