凯撒密码的Java代码 [英] java code for caesar cipher
本文介绍了凯撒密码的Java代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为凯撒密码编写代码.加密正确完成.问题在于解密功能.
对于纯文本= VictorY或56789 ..
加密正确完成,但解密不正确..
这是完整的代码
Code is written for Caesar cipher. Encryption is correctly done. Problems are with decryption function.
For plaintext = VictorY or 56789..
encryption is correctly done but decryption is not correct..
Here''s the full code
import java.util.Scanner;
public class Caesar {
public static void main(String[] args){
String cip=Caesar.encrypt();
Caesar.decrypt(cip);
}
// Caesar Encryption Function
private static String encrypt() {
char chars[] = {'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','0','1','2','3','4','5','6','7','8','9','!','@','#','$','%','^','&','(',')','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 empty = "empty";
Scanner input = new Scanner(System.in);
System.out.println("Enter the plaintext");
String plainText = input.nextLine();
String cipher = null;
char[] plain = plainText.toCharArray();
for(int i = 0;i<plain.length;i++){>
for(int j = 0 ; j<85;j++){
if(j<=80){
if(plain[i]==chars[j]){
plain[i] = chars[j+5];
break;
}
}//End nested If
else if(plain[i] == chars[j]){
plain[i] = chars [j-81];
} //End else
} //End nested for loop
} //End of For loop
cipher = String.valueOf(plain);
System.out.println(" cipher text is "+cipher);
Scanner in = new Scanner(System.in);
System.out.println("To Decrypt plaintext enter 1");
int choice = in.nextInt();
if(choice == 1){
return cipher;
}
else{
System.out.println("Thank you");}
return empty;
}
// Caesar Decryption Function
private static String decrypt(String cip) {
char chars[] = {'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','0','1','2','3','4','5','6','7','8','9','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 cipher = null;
String empty = "empty";
char[] cipher1 = cip.toCharArray();
if(cip .equals(empty)){
System.out.println(" No text is Decrypted");
}
else{ //char[] cipher1 = cip.toCharArray();
for(int i = 0;i<cipher1.length;i++){>
for(int j = 0 ; j<85;j++){
if(j>=5 && cipher1[i]==chars[j]){
cipher1[i] = chars[j-5];
break;
}
if(cipher1[i] == chars[j] && j<5){
cipher1[i] = chars[81+j];
break;
} //End IF
} //End nested for loop
} //End of For loop
}
cipher=String.valueOf(cipher1);
System.out.println(" Plain text is '"+cipher+"'");
return cipher;
}
}
[edit]已添加代码块-OriginalGriff [/edit]
[edit]Code block added - OriginalGriff[/edit]
推荐答案
',' %',' ^',' &',' (',' )',' A',' ' ' ' ' F',' G',' ' ' J',' K' ,' L',' M',' N' ,' O',' P',' Q',' R',' ' ' ' ' W',' ' ' ' +',' -', ' *',' /',' [',' ]',' {', ' }',' =',' <',' >',' ? ',' _'}; 字符串为空= " ; 扫描仪输入= 新 Scanner(System.in); System.out.println(" ); 字符串 plainText = input.nextLine(); 字符串 cipher = null; 字符 [] plain = plainText.toCharArray(); for ( int i = 0 ; i< ; plain.length; i ++){> for ( int j = 0 ; j< ; 85; j ++){ 如果(j< = 80){ 如果(普通[i] ==字符[j]){ plain [i] =字符[j + 5]; break ; } } // 结束嵌套的if 其他 如果(普通[i] ==字符[j]){ plain [i] =字符[j- 81 ]; } // 结束其他 } // 结束嵌套循环 } // For循环结束 cipher = 字符串 .valueOf(plain); System.out.println(" + cipher)"; Scanner in = 新 Scanner(System.in); System.out.println(" ); int choice = in.nextInt(); 如果(选择== 1 ){ 返回密码; } 其他 { System.out.println(" );} 返回为空; } // 凯撒解密功能 私有 静态 字符串 ="code-sdkkeyword">字符串 cip){ 字符 chars [] = {' a',' b',' c',' d',' e',' ' ' ' i',' j', ' k',' l',' m',' <跨度class ="code-string"> n',' o',' p',' ' ' ' ' u',' v',' w',' x',' y',' z',' 0',' 1',' 2',' 3',' 4',' 5',' 6',' 7',' 8',' 9',' A', B', C', D',' E',' F' ,' G',' ' ' ' K',' L', ' M',' N',' O',' P',' Q', ' R',' S',' T',' U',' V' ,' W',' X',' Y',' Z',' ' ' '
','%','^','&','(',')','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 empty = "empty"; Scanner input = new Scanner(System.in); System.out.println("Enter the plaintext"); String plainText = input.nextLine(); String cipher = null; char[] plain = plainText.toCharArray(); for(int i = 0;i<plain.length;i++){> for(int j = 0 ; j<85;j++){ if(j<=80){ if(plain[i]==chars[j]){ plain[i] = chars[j+5]; break; } }//End nested If else if(plain[i] == chars[j]){ plain[i] = chars [j-81]; } //End else } //End nested for loop } //End of For loop cipher = String.valueOf(plain); System.out.println(" cipher text is "+cipher); Scanner in = new Scanner(System.in); System.out.println("To Decrypt plaintext enter 1"); int choice = in.nextInt(); if(choice == 1){ return cipher; } else{ System.out.println("Thank you");} return empty; } // Caesar Decryption Function private static String decrypt(String cip) { char chars[] = {'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','0','1','2','3','4','5','6','7','8','9','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','!','@','#','
',' %',' ^',' &',' (',' ' +',' -,' * ' ,'/' ,' [' ,'] ' ,' {' ,' } ' ,' = ' ,'< ' ,'> ' ,'?' ,' _ ' }; 字符串密码= null; 字符串empty ="empty"; char [] cipher1 = cip.toCharArray(); if(cip .equals(empty)){ System.out.println(不解密文本"); } else {//char [] cipher1 = cip.toCharArray(); for(int i = 0; i< cipher1.length; i ++){> for(int j = 0; j< 85; j ++){ if(j> = 5&& cipher1 [i] ==字符[j]){ cipher1 [i] =字符[j-5]; 休息; } if(cipher1 [i] == chars [j]&& j< 5){ cipher1 [i] =字符[81 + j]; 休息; } //万一 }//结束嵌套循环 }//For循环的结尾 } cipher = String.valueOf(cipher1); System.out.println(纯文本为' + cipher +" ' ); 返回密码; } }
','%','^','&','(',')','+','-,'*','/','[',']','{','}','=','<','>','?','_'}; String cipher = null; String empty = "empty"; char[] cipher1 = cip.toCharArray(); if(cip .equals(empty)){ System.out.println(" No text is Decrypted"); } else{ //char[] cipher1 = cip.toCharArray(); for(int i = 0;i<cipher1.length;i++){> for(int j = 0 ; j<85;j++){ if(j>=5 && cipher1[i]==chars[j]){ cipher1[i] = chars[j-5]; break; } if(cipher1[i] == chars[j] && j<5){ cipher1[i] = chars[81+j]; break; } //End IF } //End nested for loop } //End of For loop } cipher=String.valueOf(cipher1); System.out.println(" Plain text is '"+cipher+"'"); return cipher; } }
[edit]已添加代码块-OriginalGriff [/edit]
[edit]Code block added - OriginalGriff[/edit]
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'',''0'',''1'',''2'',''3'' ,''4'',''5'',''6'',''7'',''8'',''9'',''!'',''@'',' '#'',''
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'',''0'',''1'',''2'',''3'',''4'',''5'',''6'',''7'',''8'',''9'',''!'',''@'',''#'',''
这篇关于凯撒密码的Java代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文