How Does Simple Encryption Work in Java Programming?

Click For Summary
SUMMARY

This discussion focuses on implementing simple encryption in Java using a character-based approach. The user describes a method where each letter of a message is converted to a numerical value, and a key is used to modify these values for encryption. The provided Java code only encrypts a single character due to its handling of input, which needs to be adjusted to process multiple characters from both the message and the key. Suggestions include using a loop for character input and considering the BufferedReader class for more efficient reading.

PREREQUISITES
  • Understanding of basic Java programming concepts
  • Familiarity with character encoding and ASCII values
  • Knowledge of loops and conditional statements in Java
  • Experience with Java I/O classes, specifically Scanner and BufferedReader
NEXT STEPS
  • Implement a loop to iterate through each character of the message and key in Java
  • Explore the BufferedReader class for efficient input handling in Java
  • Learn about modular arithmetic to handle character wrapping in encryption
  • Investigate more advanced encryption techniques, such as AES in Java
USEFUL FOR

Students learning Java programming, developers interested in basic encryption techniques, and anyone looking to enhance their understanding of character manipulation in Java.

philippe311
Messages
15
Reaction score
0
Hello,


My professor wants us to code a program that encrypts a massage and decrypt it , I know that to encrypt something you need a message and a key, and you assign a number to each letter or the message, and you do the same with the key, and then you add the two numbers together to get the message encrypted

for example if the message was
abcabc
and the key was
bcd

and

a = 0
b = 1
c = 2
d = 3

we do this to encrypt it
abcabc
bcdbcd
--------
235235

and 235235 is bdfbdf according to the assigned values of a, b, c, and d

so the final encrypted message is bdfbdf

--------------
this is what i wrote, but it only encrypts one letter of the message, and it uses one letter of the key, i need an idea of how to make it work for more than one letter

import java.util.Scanner;

public class assign3
{
public static String[] array ={"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 static void main(String [] args)
{


Scanner input = new Scanner (System.in);

System.out.println("insert messege =");
String message = input.next();
int i = 0;
int num = 0;

char ch;
while(i < message.length()){
ch = message.charAt (i);
num = ch - 'a';
System.out.println(num);
i = i +1;
}



System.out.println("insert key =");
String key = input.next();
char ch2;
ch2 = key.charAt (0);
int num2 = ch2 - 'a';
System.out.println(num2);


int q;

q = num + num2;
System.out.println("encrypted number = " + q);

if (q>=26)
{
q = q%26;
}


System.out.println("Encrypted letter = " + array[q]);
}


}
 
Physics news on Phys.org
philippe311 said:
Hello,


My professor wants us to code a program that encrypts a massage and decrypt it
That would be message. Massage is something entirely different.
philippe311 said:
, I know that to encrypt something you need a message and a key, and you assign a number to each letter or the message, and you do the same with the key, and then you add the two numbers together to get the message encrypted

for example if the message was
abcabc
and the key was
bcd

and

a = 0
b = 1
c = 2
d = 3

we do this to encrypt it
abcabc
bcdbcd
--------
235235

and 235235 is bdfbdf according to the assigned values of a, b, c, and d

so the final encrypted message is bdfbdf

--------------
this is what i wrote, but it only encrypts one letter of the message, and it uses one letter of the key, i need an idea of how to make it work for more than one letter
I added [ code] tags to format your code.
philippe311 said:
Code:
import java.util.Scanner;

public class assign3
{
	public static String[] array ={"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 static void main(String [] args)
	{


		Scanner input = new Scanner (System.in);

			System.out.println("insert messege =");
			String message = input.next();
I think that you are getting only one character from the line above. Same for your code to input the key.
philippe311 said:
Code:
			int i = 0;
			int num = 0;
			
			char ch;
			while(i < message.length()){
				ch = message.charAt (i);
				num = ch - 'a';
				System.out.println(num);
				i = i +1;
			}

			

			System.out.println("insert key =");
			String key = input.next();
			char ch2;
			ch2 = key.charAt (0);
			int num2 = ch2 - 'a';
			System.out.println(num2);


			int q;

			q = num + num2;
			System.out.println("encrypted number = " + q);

			if (q>=26)
			{
				q = q%26;
			}


			System.out.println("Encrypted letter = " + array[q]);
	}


}

Is there some reason you're using the Scanner class to do I/O? You can do input as you are doing, but you need to have a loop to read all the characters of the message and the key, not just a single character each. Another possibility for input is the BufferedReader class using its readLine() method.
 

Similar threads

  • · Replies 37 ·
2
Replies
37
Views
5K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
Replies
9
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 12 ·
Replies
12
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K