Jump to content
  • Advertisement
Sign in to follow this  
Ueneth Echil

C&C My encryption program?

This topic is 3926 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I've been working on a program which will encrypt a message with a keyword into numbers. It's based off some already cipher or whatever, but the main thing I'm looking for is feedback on program design. Please note that while I have it setup, the decryption doesn't work, and I haven't programmed it to handle spaces yet, so please don't try. EncryptionProgram.java
// EncryptionProgram.java

// Add decryption and allow for spaces



import java.io.*;

import java.util.Scanner;

import java.lang.Integer;



public class EncryptionProgram

{

	public static void main(String args[])

	{

		Scanner input = new Scanner(System.in);

		

		System.out.print("Enter the keyword: ");

		String keyword = input.nextLine();

		

		System.out.print("<Encrypt> <Decrypt>: ");

		String choice = input.nextLine();

		

		String word;

		

		if (choice.compareToIgnoreCase("encrypt") == 0)

		{

			System.out.print("Enter the text to be encrypted: ");

			word = input.nextLine();

			

			Encryption encryption = new Encryption(word, keyword);

			

			encryption.Display();			

		}

		

		else if (choice.compareToIgnoreCase("decrypt") == 0)

		{

			System.out.print("Enter the text to be decrypted: ");

			word = input.nextLine();

			

			Decryption decryption = new Decryption(word, keyword);

			

			decryption.Display();

		}

		

		else

		{

			System.out.println("Input Error.\n");

		}	

	}

}

Encryption.java
// Encryption.java

import java.io.*;

import java.util.Scanner;

import java.lang.Integer;



class Encryption

{

	private String encryptedText = "";

	private String messageText;

	private String keyword;

	

	private String[][] alphabet;

	private int keywordCount = 0;

	

	private String[] alphabetTemp = new String[26];

	private String[] alphabetTemp2 = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};

	

	public Encryption(String input, String key)

	{

		messageText = input;

		keyword = key;

		

		Encrypt();

	}

	

	public void Encrypt()

	{

		messageText.toLowerCase();

		

		// Gets the individual letters of keyword

		for (int k = 0; k < keyword.length(); k++)

		{

			alphabetTemp[k] = keyword.substring(k,k+1);

			keywordCount++;

		}

		

		alphabet = new String[26][keywordCount+1];

		// Adds the keyword to the first row

		for (int k = 1; k < keyword.length()+1; k++)

		{

			alphabet[0][k] = alphabetTemp[k-1];

		}

		

		int numCode = 1;

		// Adds numbers to the first column

		for (int k = 0; numCode < 27; k++)

		{

			String temp = "" + numCode;

			alphabet[k][0] = temp;

			numCode++;

		}

		

		// Adds the rest of the alphabet

		for (int k = 0; k < keyword.length(); k++)

		{

			String asciiNum = alphabet[0][k+1];

			char asciiNumChar = asciiNum.charAt(0);

			int asciiNumInt = (int)asciiNumChar;

			

			for (int j = 0; j < 25; j++)

			{

				asciiNumInt++;

				

				if (asciiNumInt == 123)

					asciiNumInt = 97;

					

				char temp = (char)asciiNumInt;

				String temp2 = "" + temp;

				alphabet[j+1][k+1] = temp2;

			}

		}

		

		int pos = 1;

		

		// Encryption itself

		for (int k = 0; k < messageText.length(); k++)

		{

			for (int l = 0; l < 26; l++)

			{

				if (alphabet[l][pos].compareToIgnoreCase(messageText.substring(k,k+1)) == 0)

				{

					encryptedText += (alphabet[l][0] + " ");

				}		

			}

			

			pos++;

			

			if (pos > keyword.length())

				pos = 1;

		}

	}

	

	public void Display()

	{

		/*

		System.out.println();

		for (int k = 0; k < alphabet.length; k++)

		{

			for (int j = 0; j < alphabet[k].length; j++)

			{

				System.out.print(alphabet[k][j] + " ");

			}

			

			System.out.println();

		}

		*/

		

		System.out.println();

		System.out.println("====================================");

		System.out.println("Message text: " + messageText);

		System.out.println("Encrypted text: " + encryptedText);

		System.out.println("====================================");

		System.out.println();

	}

}

Decryption.java
// Decryption.java

import java.io.*;

import java.util.Scanner;

import java.lang.Integer;



class Decryption

{

	private String encryptedText;

	private String messageText;

	private String keyword;

	

	public Decryption(String input, String key)

	{

		encryptedText = input;

		keyword = key;

	}

	

	public void Decrypt()

	{

		// Insert algorithm here

	}

	

	public void Display()

	{

		System.out.println();

		System.out.println("====================================");

		System.out.println("Encrypted text: " + encryptedText);

		System.out.println("Message text: ");

		System.out.println("====================================");

		System.out.println();

	}

}

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!