Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
I want to encrypt and decrypt a string which contains alphabets from A-Z using java
Encryption:-For example i have a string from A-Z and user gives A as input then the output must be 1 in this way if user inputs B then output must be 2 and soon up to Z.

Decryption:-For example i have a string from A-Z and user gives 1 as input then the output must be A in this way if user input 2 then output must be B and soon up to Z.

i was able to solve decryption but i am not able to solve encryption.please remember while answering that Iam a beginner and i want to write the encryption program and decryption program in one program only.

What I have tried:

Java
import java.util.Scanner;
import java.lang.String;
public class Decrypt {

		public static void main(String[] args){
			
			Scanner sc=new Scanner(System.in);
		    String s1 = " ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		   int n = sc.nextInt();
		    char ch = s1.charAt(n);
		    
		    System.out.println(ch);
		   
	            }  
	         
	        }
Posted
Updated 7-Nov-18 5:40am
v3
Comments
CPallini 7-Nov-18 5:32am    
I see a problem in your encryption/decryption logic. For instance, on encryption K->11.
Then, on decryption, 11->K or 11->AA ?
MadMyche 7-Nov-18 8:11am    
Nice
Member 14046337 7-Nov-18 9:24am    
i want decryption 11- k and encryption k-11.
CPallini 7-Nov-18 11:23am    
The you have to put separators (e.g. blanks) in the encrypted string, otherwise decryption won't be feasible, due to such ambiguties. Or possibly your encrypted info is an array of integers?
MadMyche 7-Nov-18 8:13am    
(hint) So basically you just want to get the ASCII value and substract 64 to "encrypt" (sic)

Java
String ss = sc.next();    // get the next token
ss = s.toUpperCase();     // convert to upper case
for (int i = 0; i < ss.length(); ++i)  // for each character in the token
{
    char c = ss.charAt(i);
    System.out.print("Letter is: " + c);          // display the character
    int val = c - 'A' + 1;                        // get its numeric value
    System.out.println(" , Value is: " + val);    // display the value
}

Note, this needs the addition of error checks to ensure that each character is alphabetic only.
 
Share this answer
 
Comments
CPallini 7-Nov-18 5:12am    
Such an encryption would be undecryptable, unless you put separators between the generated integers.
Richard MacCutchan 7-Nov-18 5:16am    
I know, but it answers the question.
CPallini 7-Nov-18 5:22am    
I see. Now I wonder how the OP was 'able to solve decryption'.
Richard MacCutchan 7-Nov-18 5:52am    
It's not decryption (or encryption) it's substitution.
Member 14046337 7-Nov-18 9:52am    
ss=s.toUppercase();.Here s is showing some errors.
If you can't solve encryption, then you can't solve decryption, because you don't have "genuine" encrypted examples to test against. And since decryption is the exact reverse of encryption solving one pretty much gives you the code for the other!

This is a minor variation of the Caesar Cypher: and you will find many, many examples of that implemented in pretty much every language: Caesar Cypher java - Google Search[^] offers 68,000 hits.

But do bear in mind that a Caesar Cypher isn't "real encryption", and isn't at all secure - they publish whole magazines full of them (generally called "Codebreaker" or similar) every month so really bored people can solve them in their spare time instead of watching Jeremy Kyle or yet another putrid game show...
 
Share this answer
 
Thank u for everyone help.This is even better than stack overflow
import java.util.Scanner;
public class Encrypt {

		public static void main(String[] args){
			Scanner sc=new Scanner(System.in);
		    String s1 = " ABCDEFGHIJKLMNOPQRST";
		     int n = sc.nextInt();
		    char ch = s1.charAt(n);
		    
		    System.out.println(ch);
		
		    
	String ss = sc.next();    // get the next token

ss = ss.toUpperCase();     // convert to upper case
for (int i = 0; i < ss.length(); ++i)  // for each character in the token
{
    char c = ss.charAt(i);
    System.out.print("Letter is: " + c);          // display the character
    int val = c - 'A' + 1;                        // get its numeric value
    System.out.println(" , Value is: " + val);    // display the value
}}}
 
Share this answer
 
Comments
Evrim Tekin 7-Nov-18 20:08pm    
Hi

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900