java凯撒密码
java caesar cipher code
我用 java 做了凯撒密码 运行s 但在用户输入密钥后没有加密任何东西!
这是我的代码
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();
}
}
运行:
Enter The Plain Text:
Reem LA
Enter The Key:
2
The Cipher Text
试试这个:
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();
}
}
这适用于所有字母字符串...但是根据您的程序,这会将原始消息转换为小写。所以这不是纯加密,因为您的程序不区分大小写。
如果你想让你的程序区分大小写,这里是程序:
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();
}
}
希望这能提供一些思路。祝一切顺利。
使用 indexOf
不是很有效...您可以对 char
值进行整数运算以获得它们的索引。
我在代码中加入了注释来解释更多,但这是我想出的。
public class CaesarCipher {
// Rotate a character k-positions
public static char cipher(char c, int k) {
// declare some helping constants
final int alphaLength = 26;
final char asciiShift = Character.isUpperCase(c) ? 'A' : 'a';
final int cipherShift = k % alphaLength;
// shift down to 0..25 for a..z
char shifted = (char) (c - asciiShift);
// rotate the letter and handle "wrap-around" for negatives and value >= 26
shifted = (char) ((shifted + cipherShift + alphaLength) % alphaLength);
// shift back up to english characters
return (char) (shifted + asciiShift);
}
// Rotate a string k-positions
public static String cipher(String s, int k) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
sb.append(cipher(s.charAt(i), k));
}
return sb.toString();
}
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
String password;
int key;
System.out.print("Please enter a password: ");
password = keyboard.nextLine();
do {
System.out.print("Please enter a key between 1-25: ");
key = keyboard.nextInt();
if (key < 1 || key > 25) {
System.out.printf(" The key must be between 1 and 25, you entered %d.\n", key);
}
} while (key < 1 || key > 25);
System.out.println("Password:\t" + password);
String encryption = cipher(password, key);
System.out.println("Encrypted:\t" + encryption);
System.out.println("Decrypted:\t" + cipher(encryption, -key));
}
}
输出应该类似于
Please enter a password: ABCDEFGHIJKLMNOPQRSTUVWXYZ
Please enter a key between 1-25: 1
Password: ABCDEFGHIJKLMNOPQRSTUVWXYZ
Encrypted: BCDEFGHIJKLMNOPQRSTUVWXYZA
Decrypted: ABCDEFGHIJKLMNOPQRSTUVWXYZ
这可能是最简单也最容易理解的方法:
import java.util.*;
public class CeaserCipher {
public static void main (String [] args) {
Scanner input = new Scanner(System.in);
System.out.print("Type your message ");
String message = input.nextLine().toUpperCase();
System.out.print("Set Encoding Key ");
int key = input.nextInt() % 26;
for (int i = 0; i < message.length(); i++) {
char c = message.charAt(i);
if (c <= 64 || c >= 91) {
c += 0;
}
else {
c += key;
if (c >= 91) {
c -= 26;
}
}
System.out.print(c);
}
}
}
private static final int ALPHABET_SIZE = 26;
public static String encryptOrDecrypt(String str, int rotateBy, boolean isEncryption) {
rotateBy = rotateBy % 26;
StringBuilder result = new StringBuilder();
for (int i = 0; i < str.length(); i++) {
char temp = str.charAt(i);
temp = rotate(temp, rotateBy,isEncryption);
result.append(temp);
}
return result.toString();
}
private static char rotate(char c, int rotateBy,boolean isEncryption) {
int start=0;
int end =0;
if (isLowerCase(c)) {
start='a';
end = 'z';
} else if (isUpperCase(c)) {
start='A';
end = 'Z';
}else {
return c;
}
if (isEncryption){
c += rotateBy;
}else {
c -= rotateBy;
}
if (c < start) {
return (char) (c + ALPHABET_SIZE);
}
if (c > end) {
return (char) (c - ALPHABET_SIZE);
}
return c;
}
public static void main(String args[]) {
String planeText= "TrilokiNathYadav";
System.out.println("Plane Text : "+planeText);
String encrypted =encryptOrDecrypt(planeText,2,true);
System.out.println("Encrypted : "+encrypted);
String decrypted = encryptOrDecrypt(encrypted,2,false);
System.out.println("Decrypted : "+decrypted);
}
我用 java 做了凯撒密码 运行s 但在用户输入密钥后没有加密任何东西!
这是我的代码
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();
}
}
运行:
Enter The Plain Text:
Reem LA
Enter The Key:
2
The Cipher Text
试试这个:
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();
}
}
这适用于所有字母字符串...但是根据您的程序,这会将原始消息转换为小写。所以这不是纯加密,因为您的程序不区分大小写。
如果你想让你的程序区分大小写,这里是程序:
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();
}
}
希望这能提供一些思路。祝一切顺利。
使用 indexOf
不是很有效...您可以对 char
值进行整数运算以获得它们的索引。
我在代码中加入了注释来解释更多,但这是我想出的。
public class CaesarCipher {
// Rotate a character k-positions
public static char cipher(char c, int k) {
// declare some helping constants
final int alphaLength = 26;
final char asciiShift = Character.isUpperCase(c) ? 'A' : 'a';
final int cipherShift = k % alphaLength;
// shift down to 0..25 for a..z
char shifted = (char) (c - asciiShift);
// rotate the letter and handle "wrap-around" for negatives and value >= 26
shifted = (char) ((shifted + cipherShift + alphaLength) % alphaLength);
// shift back up to english characters
return (char) (shifted + asciiShift);
}
// Rotate a string k-positions
public static String cipher(String s, int k) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
sb.append(cipher(s.charAt(i), k));
}
return sb.toString();
}
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
String password;
int key;
System.out.print("Please enter a password: ");
password = keyboard.nextLine();
do {
System.out.print("Please enter a key between 1-25: ");
key = keyboard.nextInt();
if (key < 1 || key > 25) {
System.out.printf(" The key must be between 1 and 25, you entered %d.\n", key);
}
} while (key < 1 || key > 25);
System.out.println("Password:\t" + password);
String encryption = cipher(password, key);
System.out.println("Encrypted:\t" + encryption);
System.out.println("Decrypted:\t" + cipher(encryption, -key));
}
}
输出应该类似于
Please enter a password: ABCDEFGHIJKLMNOPQRSTUVWXYZ
Please enter a key between 1-25: 1
Password: ABCDEFGHIJKLMNOPQRSTUVWXYZ
Encrypted: BCDEFGHIJKLMNOPQRSTUVWXYZA
Decrypted: ABCDEFGHIJKLMNOPQRSTUVWXYZ
这可能是最简单也最容易理解的方法:
import java.util.*;
public class CeaserCipher {
public static void main (String [] args) {
Scanner input = new Scanner(System.in);
System.out.print("Type your message ");
String message = input.nextLine().toUpperCase();
System.out.print("Set Encoding Key ");
int key = input.nextInt() % 26;
for (int i = 0; i < message.length(); i++) {
char c = message.charAt(i);
if (c <= 64 || c >= 91) {
c += 0;
}
else {
c += key;
if (c >= 91) {
c -= 26;
}
}
System.out.print(c);
}
}
}
private static final int ALPHABET_SIZE = 26;
public static String encryptOrDecrypt(String str, int rotateBy, boolean isEncryption) {
rotateBy = rotateBy % 26;
StringBuilder result = new StringBuilder();
for (int i = 0; i < str.length(); i++) {
char temp = str.charAt(i);
temp = rotate(temp, rotateBy,isEncryption);
result.append(temp);
}
return result.toString();
}
private static char rotate(char c, int rotateBy,boolean isEncryption) {
int start=0;
int end =0;
if (isLowerCase(c)) {
start='a';
end = 'z';
} else if (isUpperCase(c)) {
start='A';
end = 'Z';
}else {
return c;
}
if (isEncryption){
c += rotateBy;
}else {
c -= rotateBy;
}
if (c < start) {
return (char) (c + ALPHABET_SIZE);
}
if (c > end) {
return (char) (c - ALPHABET_SIZE);
}
return c;
}
public static void main(String args[]) {
String planeText= "TrilokiNathYadav";
System.out.println("Plane Text : "+planeText);
String encrypted =encryptOrDecrypt(planeText,2,true);
System.out.println("Encrypted : "+encrypted);
String decrypted = encryptOrDecrypt(encrypted,2,false);
System.out.println("Decrypted : "+decrypted);
}