本文介绍了java caesar密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的代码
public class CaesarCipher
{
public static final String ALPHABET =abcdefghijklmnopqrstuvwxyz;
public static String encrypt(String plainText,int shiftKey)
{
plainText = plainText.toLowerCase();
String cipherText =; (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 =; (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;
}
返回plainText;
}
public static void main(String [] args)
{
扫描仪sc =新扫描仪(System.in);
System.out.println(输入加密字符串);
String message = new String();
message = sc.next();
System.out.println(encrypt(message,3));
System.out.println(decrypt(encrypt(message,3),3));
sc.close();
}
}
运行:
输入纯文本:
Reem LA
输入密钥:
2
密码文本
解决方案
使用 indexOf
不是非常有效...您可以在 char
值上执行整数运算以获取其索引。
我在代码中添加了注释来解释更多,但这是我想出来的。
public class CaesarCipher {
//旋转一个字符k位置
public static char cipher c,int k){
//声明一些帮助常量
final int alphaLength = 26;
final char asciiShift = Character.isUpperCase(c)? 'A':'a'
final int cipherShift = k%alphaLength;
//转移到0..25为a..z
char shif =(char)(c - asciiShift);
//旋转字母并处理负面和值的wrap-around> = 26
shif =(char)((shift + cipherShift + alphaLength)%alphaLength);
//换回英文字符
return(char)(shift + asciiShift);
}
//旋转一个字符串k位置
public static String cipher(String s,int k){
StringBuilder sb = new StringBuilder(); (int i = 0; i< s.length(); i ++){
sb.append(cipher(s.charAt(i),k));
}
return sb.toString();
}
public static void main(String [] args){
扫描仪键盘=新的扫描仪(System.in);
字符串密码;
int key;
System.out.print(请输入密码:);
password = keyboard.nextLine();
do {
System.out.print(请输入1-25之间的键:);
key = keyboard.nextInt();
if(key< 1 || key> 25){
System.out.printf(该键必须介于1和25之间,您输入%d.\\\
,键);
}
} while(key< 1 || key> 25);
System.out.println(密码:\t+密码);
字符串加密=密码(密码,密钥);
System.out.println(加密:\t+加密);
System.out.println(Decrypted:\t+ cipher(encryption,-key));
}
}
输出应该是像/ p>
请输入密码:ABCDEFGHIJKLMNOPQRSTUVWXYZ
请输入1-25:1之间的密钥
密码:ABCDEFGHIJKLMNOPQRSTUVWXYZ
加密:BCDEFGHIJKLMNOPQRSTUVWXYZA
解密:ABCDEFGHIJKLMNOPQRSTUVWXYZ
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
解决方案
Using indexOf
is not very efficient... You can do integer arithmetic on char
values to get their indices.
I included comments in the code to explain more, but this is what I came up with.
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));
}
}
The output should be something like
Please enter a password: ABCDEFGHIJKLMNOPQRSTUVWXYZ
Please enter a key between 1-25: 1
Password: ABCDEFGHIJKLMNOPQRSTUVWXYZ
Encrypted: BCDEFGHIJKLMNOPQRSTUVWXYZA
Decrypted: ABCDEFGHIJKLMNOPQRSTUVWXYZ
这篇关于java caesar密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!