恺撒密码 (Caesar cipher) 是一种相传尤利乌斯·恺撒曾使用过的密码。 恺撒千公元前100年左右诞生于古罗马,是一位著名的军事统帅恺撒密码是通过将明文中所使用的字母表按照一定的字数“平移”来进行加密的。
恺撒密码是通过将明文中所使用的字母表按照一定的字数“平移”来进行加密的。
暴力破解
由于字母表只有26个字母,因此加密用的密钥只有0到25共26种,只要按顺序将这26种密钥都尝试一遍,都就破解。
暴力破解:将所有可能的密钥全部尝试一遍,又称穷举搜索。
JAVA 暴力破解恺撒密码与恺撒加密。
public class kaisa {
private static String encrypt(String str,int mi) {
StringBuilder sb =new StringBuilder();
for (int i =0;i
char ch = str.charAt(i);
int temp = (int) ch;
//去除空格以其它字符
if (temp <65 || temp >90) {
sb.append(ch);
continue;
}
if(temp ==32) {
sb.append(" ");
continue;
}
temp +=mi;
if(temp >90) {
temp -=26;
}
char uppercase = (char) temp;
sb.append(uppercase);
}
return sb.toString();
}
private static void decrypt(String str) {
//遍历26
for(int k=0;k<26;++k) {
StringBuilder sb =new StringBuilder();
for (int i =0;i
char ch = str.charAt(i);
int temp = (int) ch;
//去除空格
//去除空格以其它字符
if (temp <65 || temp >90) {
sb.append(ch);
continue;
}
temp -=k;
if(temp <65) {
temp +=26;
}
char uppercase = (char) temp;
sb.append(uppercase);
}
System.out.println("第"+k+"次:"+sb.toString());
}
}
public static void main(String[] args){
String str ="ZW PFL NREK KF CVRIE DFIV RSFLK TIPGKFXIRGYP Z IVTFDDVEU RE FECZEV TFLIJV ZEJKILTKVU SP " +
"GIFWVJJFI URE SFEVY WIFD JKREWFIU LEZMVIJZKP ALJK JVRITY TIPGKFXIRGYP RK TFLIJVIR.FIX";
decrypt(str);
String str1 ="I LOVE YOU!";
String result =encrypt(str1,17);
System.out.println("加密结果:"+result);
}
}











网友评论