caesar.java
来自「凯撒密码 Java版的 好像有些小毛病 帮我看看啊」· Java 代码 · 共 72 行
JAVA
72 行
package ende;
public class Caesar {
/**
06 *
07 * Contains two function of encrypt and decrypt of Caesar
08 *
09 * 古典密码学体系中的凯撒密码的加密与解密函数
10 * 仅仅属于算法上的研究
11 *
12 * @author soundofu
13 *
14 * @E-mail:zhuzongqian@gmail.com
15 */
//make a default dictionary of char string.
static private String dictionary = "abcdefghijklmnopqrstuvwxyz";
/*
22 * Function of encrypt
23 *
24 * String encrypt(String strs, int key);
25 *
26 */
static String encrypt(String strs,int key)
{
char [] encrypted = new char[strs.length()];
int pos;
for(int i=0;i<100;i++)
{
pos = dictionary.indexOf(strs.charAt(i));
if(pos != -1)
encrypted [i] = dictionary.charAt((pos+key)%dictionary.length());
}
return new String(encrypted);
}
/*
43 * Function of decrypt
44 *
45 * String decrypt(String strs,int key);
46 *
47 */
static String decrypt(String strs,int key)
{
char [] clearchar = new char[strs.length()];
int pos;
for(int i=0;i<100;i++)
{
pos = dictionary.indexOf(strs.charAt(i));
if(pos != -1)
clearchar [i] = dictionary.charAt((pos-key+dictionary.length())%dictionary.length());
}
return new String(clearchar);
}
public static void main(String[] args) {
String orignial = "ijkm"; //String to be encrypted
//Get the encrypted(String,int) function.
System.out.println(encrypt(orignial,8));
//Get the decrypted(String,int) function.
System.out.println(decrypt(orignial,8));
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?