Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
547 views
in Technique[技术] by (71.8m points)

encryption - java caesar cipher code

i did caesar cipher code by java it runs but doesnt encrypt anything after user enter the key !

here is my code

public class CaesarCipher
{
    public static final String ALPHABET = "abcdefghijklmnopqrstuvwxyz";

    public static String encrypt(String plainText, int shiftKey)
    {
        plainText = plainText.toLowerCase();
        String cipherText = "";
        for (int i = 0; i < plainText.length(); i++)
        {
            int charPosition = ALPHABET.indexOf(plainText.charAt(i));
            int keyVal = (shiftKey + charPosition) % 26;
            char replaceVal = ALPHABET.charAt(keyVal);
            cipherText += replaceVal;
        }
        return cipherText;
    }

    public static String decrypt(String cipherText, int shiftKey)
    {
        cipherText = cipherText.toLowerCase();
        String plainText = "";
        for (int i = 0; i < cipherText.length(); i++)
        {
            int charPosition = ALPHABET.indexOf(cipherText.charAt(i));
            int keyVal = (charPosition - shiftKey) % 26;
            if (keyVal < 0)
            {
                keyVal = ALPHABET.length() + keyVal;
            }
            char replaceVal = ALPHABET.charAt(keyVal);
            plainText += replaceVal;
        }
        return plainText;
    }

    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the String for Encryption: ");
        String message = new String();
        message = sc.next();
        System.out.println(encrypt(message, 3));
        System.out.println(decrypt(encrypt(message, 3), 3));
        sc.close();
    }
}

run:

Enter The Plain Text:
Reem LA
Enter The Key:
2
The Cipher Text
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try this:

import java.util.*;

public class CaesarCipher
{ 
public static final String ALPHABET = "abcdefghijklmnopqrstuvwxyz";
public static String encrypt(String plainText, int shiftKey)
{
    plainText = plainText.toLowerCase();
    String cipherText = "";
    for (int i = 0; i < plainText.length(); i++)
    {
        char replaceVal = plainText.charAt(i);
        int charPosition = ALPHABET.indexOf(replaceVal);        
        if(charPosition != -1) {
            int keyVal = (shiftKey + charPosition) % 26;
            replaceVal = ALPHABET.charAt(keyVal);
        }

        cipherText += replaceVal;
    }
    return cipherText;
}

public static String decrypt(String cipherText, int shiftKey)
{
    cipherText = cipherText.toLowerCase();
    String plainText = "";
    for (int i = 0; i < cipherText.length(); i++)
    {
        char replaceVal = cipherText.charAt(i);
        int charPosition = ALPHABET.indexOf(replaceVal);
        if(charPosition != -1) {
            int keyVal = (charPosition - shiftKey) % 26;
            if (keyVal < 0) {
                keyVal = ALPHABET.length() + keyVal;
            }   
            replaceVal = ALPHABET.charAt(keyVal);
        }     
        plainText += replaceVal;
    }
    return plainText;
}

public static void main(String[] args)
{
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter the String for Encryption: ");
    String message = new String();
    message = sc.nextLine();
    System.out.println(encrypt(message, 3));
    System.out.println(decrypt(encrypt(message, 3), 3));
    sc.close();
}
}

This will work for all alphabetical strings... But as per your program, this will convert the original message to lowercase. So this is not pure encryption since your program is case insensitive.

If you want your program to be case sensitive, here is the program:

import java.util.*;

public class CaesarCipher
{ 
public static final String ALPHABET_LOWER = "abcdefghijklmnopqrstuvwxyz";
public static final String ALPHABET_UPPER = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
public static String encrypt(String plainText, int shiftKey)
{
    String cipherText = "";
    for (int i = 0; i < plainText.length(); i++)
    {
        int charPosition = -1;
        char replaceVal;
        int keyVal = -1;
        char val = plainText.charAt(i);
        System.out.println(val);
        if(Character.isUpperCase(val)) {
            charPosition = ALPHABET_UPPER.indexOf(val);
            if(charPosition != -1) {
                keyVal = (shiftKey + charPosition) % 26;
                replaceVal = ALPHABET_UPPER.charAt(keyVal);
            } else {
                replaceVal = plainText.charAt(i);
            }           
        } else {
            charPosition = ALPHABET_LOWER.indexOf(val);
            if(charPosition != -1) {
                keyVal = (shiftKey + charPosition) % 26;
                replaceVal = ALPHABET_LOWER.charAt(keyVal);
            } else {
                replaceVal = plainText.charAt(i);
            }
        }       
        System.out.println("Cipher: "+cipherText);
        cipherText += replaceVal;        
    }
    return cipherText;
}

public static String decrypt(String cipherText, int shiftKey)
{
    String plainText = "";
    for (int i = 0; i < cipherText.length(); i++)
    {
        int charPosition = -1;
        char replaceVal;
        int keyVal = -1;
        char val = cipherText.charAt(i);

        if(Character.isUpperCase(val)) {
            charPosition = ALPHABET_UPPER.indexOf(val);
            if(charPosition != -1) {
                keyVal = (charPosition - shiftKey) % 26;
                if (keyVal < 0) {
                    keyVal = ALPHABET_UPPER.length() + keyVal;
                }
                replaceVal = ALPHABET_UPPER.charAt(keyVal);
            } else {
                replaceVal = cipherText.charAt(i);
            }           
        } else {
            charPosition = ALPHABET_LOWER.indexOf(val);
            if(charPosition != -1) {
                keyVal = (charPosition - shiftKey) % 26;
                if (keyVal < 0) {
                    keyVal = ALPHABET_LOWER.length() + keyVal;
                }
                replaceVal = ALPHABET_LOWER.charAt(keyVal);
            } else {
                replaceVal = cipherText.charAt(i);
            }
        }
        plainText += replaceVal;
    }
    return plainText;
}

public static void main(String[] args)
{
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter the String for Encryption: ");
    String message = new String();
    message = sc.nextLine();
    System.out.println(encrypt(message, 3));
    System.out.println(decrypt(encrypt(message, 3), 3));
    sc.close();
}
}

Hope this gives some idea. All the best.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...