-4

How can I encrypt and decrypt text by using two string
for more clarification

String order =  {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};
String random = {q,w,e,r,t,y,u,i,o,p,a,s,d,f,g,h,j,k,l,z,x,c,v,b,n,m};

when i encrypt any word for example : abcdefg ;
when i encrypt it it will shown as qwerty ;

and when i decrypt he will do the reverse
if he write hello the decrypted text will be pcssi

i try hard to use the index of the two strings but i couldn't find any result

Pshemo
  • 122,468
  • 25
  • 185
  • 269

3 Answers3

4

Since this is likely a homework, here are some hints on how to complete it:

  • You need to fix the syntax: string literals go in double-quotes with no commas
  • To "encode" a character, find its position in the order string using indexOf, then use that index in the call of random.charAt.
  • To "decode" a character, do the same thing in reverse order: call indexOf on random, then pass the index to order.charAt.

Here is a skeletal code to encode a single character, and decode it back (demo):

char orig = 'x';
char conv = random.charAt(order.indexOf(orig));
char back = order.charAt(random.indexOf(conv));

This code is incomplete - it is going to fail if the character being looked up is not present in the order string, or when either the order or the random string do not cover the entire alphabet exactly once.

Note: this implementation of a primitive substitution cipher has nothing to do with security: if anything, it makes your system less secure by pretending to encode things which essentially remain in plain text.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • THANKS a lot but it isnt a homework iam going to develop a program for encryption and decryption using more than one algorithms so that thanks another time and i will done it thanks for help – Hashem H Ramadan Oct 31 '13 at 16:30
  • 1
    @EngHashem You are welcome! Try this out and see if it works. If it does, consider accepting an answer by clicking the grey check mark next to it. This would let other site visitors know that you are no longer actively looking for an improved solution, and earn you a new badge on Stack Overflow. Good luck! – Sergey Kalinichenko Oct 31 '13 at 16:32
  • 1
    @EngHashem As others have already pointed out, this mechanism can hardly be considered encryption. Take a look at [this question](http://stackoverflow.com/q/1132567/851811) for an example of a basic two-way password encryption. – Xavi López Oct 31 '13 at 16:43
1

If you're not forced to use strings

Your best option here would to use an AbstractMap.SimpleEntry<K,V>. The objective here is to "bind" one letter to another, allowing you to find a key (for example the a), and retrieve the value (the encrypted one, in this case the q). When you need to decrypt, you can with a bit of coding find the key that belongs to your value from the same table. Assuming you use a 1 to 1 encryption like in your example, you will be able to do this relatively easily.

else

Use the charAt(int pos) method as described in some of the other responses here.

Voidpaw
  • 910
  • 1
  • 5
  • 18
0
public class Encription {
    
    private String text;
    private int key;
    
    public Encription(String text, int k){
        this.text = "";
        key = k;//(int)(Math.random()*5);
        String[] tokens = text.split(" ");
        for(int i = tokens.length-1; i>=0; i--){
            for(int j = 0; j < tokens[i].length(); j++)
                this.text += (char)(tokens[i].charAt(j) + key);
            this.text += " ";
        }
        
    }
    
    @Override
    public String toString()  {
        
        return text;
    }
    
    public static void main(String[] args){
        Encription e;// = new Encription("Humming birds hum on trees");
        java.util.Scanner scan = new java.util.Scanner(System.in);
        boolean ans = true;
        while(ans){
            int key = -1;
            while(key <= 0 || key > 5){
                System.out.print("Enter a key value between 1 and 5: ");
                key = scan.nextInt();
            }
            scan.nextLine();
            System.out.print("Enter text: ");
            String phrase = scan.nextLine();
            e = new Encription(phrase, key);
            
            System.out.println("The encrypted text is: "+e);
            System.out.println("Do you wish to enter another text? (y/n)");
            char c = scan.next().charAt(0);
            ans = (c=='y' || c== 'Y');
            
        }
        
    }
}