Encryption code explanation.

That's usually a little easier to use.In summary, the conversation discusses the process of encrypting a message using a key and assigning values to letters. An example is provided for encrypting a message using the key "bcd." However, the code only works for one letter at a time and the speaker is seeking advice on how to make it work for more than one letter. The use of the Scanner class for input is also questioned.
  • #1
philippe311
17
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
  • #2
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.
 

What is encryption code?

Encryption code is a method of encoding information or data in a way that only authorized individuals can access it. It uses algorithms to convert plain text into a coded format, making it unreadable to anyone who does not have the corresponding key to decrypt it.

Why is encryption code important?

Encryption code is important because it helps protect sensitive information from being accessed by unauthorized individuals. This includes personal information, financial data, and confidential documents. It also helps ensure the privacy and security of communications and transactions over the internet.

How does encryption code work?

Encryption code works by using mathematical algorithms to convert plain text into a coded format. The process involves scrambling the data using a key, which is a unique set of characters or numbers. The recipient of the data can then use the same key to decipher and read the information.

What are some common types of encryption code?

Some common types of encryption code include symmetric encryption, asymmetric encryption, and hashing. Symmetric encryption uses the same key for both encryption and decryption, while asymmetric encryption uses two separate keys for these processes. Hashing involves converting data into a fixed-length code, making it difficult for anyone to reverse the process and obtain the original data.

Is encryption code unbreakable?

Encryption code is considered very secure, but it is not entirely unbreakable. Some methods of encryption may be more vulnerable to attacks than others, and with enough computing power and time, it is possible for hackers to crack the code. This is why it is important to use strong and constantly updated encryption methods to protect sensitive information.

Similar threads

  • Engineering and Comp Sci Homework Help
2
Replies
37
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
11
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
3K
Back
Top