2.3实现加解密程序。
替代密码:
替代密码算法的原理是使用替代法进行加密,就是将明文中的字符用其它字符替代后形成密文。例如:明文字母a,b,c,d ,用D,E,F,G做对应替换后形成密文。
替代密码包括多种类型,如单表替代密码,多明码替代密码,多字母替代密码,多表替代密码等。下面我们介绍一种典型的单表替代密码,恺撒(caesar)密码,又叫循环移位密码。它的加密方法,就是将明文中的每个字母用此字符在字母表中后面第k个字母替代。它的加密过程可以表示为下面的函数:
E(m)=(m+k) mod n
其中:m为明文字母在字母表中的位置数;n为字母表中的字母个数;k为密钥;E(m)为密文字母在字母表中对应的位置数。
例如,对于明文字母H,其在字母表中的位置数为8,设k=4,则按照上式计算出来的密文为L:
E(8) = (m+k) mod n= (8+4) mod 26 = 12 = L
源代码(java实现):
import java.util.Scanner;
public class MyTiHuan {
public final int smalla = 97;
public final int bigA = 65;
public final int smallz = 122;
public final int bigZ = 90;
public final int space = 32;
public final int smallZero = 96;
public final int bigZero = 64;
/**
* 判断是否为小写字母
*
* @param a
* @return
*/
public boolean isSmall(char a) {
if (a >= 97 && a <= 122) {
return true;
} else
return false;
}
/**
* 判断是否为大写字母
*
* @param A
* @return
*/
public boolean isBig(char A) {
if (A >= 65 && A <= 90) {
return true;
} else
return false;
}
/**
* 判断是否为空格
*
* @param spa
* @return
*/
public boolean isSpace(char spa) {
if (spa == space) {
return true;
} else
return false;
}
/**
* 检查非法字符
*
* @param a
* @return
*/
public boolean isPass(char a) {
if (a == space) {
return true;
} else if (a >= smalla && a <= smallz) {
return true;
} else if (a >= bigA && a <= bigZ) {
return true;
}
return false;
}
/**
*
* 加密
*
* @param mingwen
* 明文
* @param yiwei
* 右移的位数
* @return 密文
*
*/
public String enCode(String mingwen, int yiwei) {
StringBuffer sBuff = new StringBuffer();
char[] mw = mingwen.toCharArray();
System.out.println("加密中----");
for (int i = 0; i < mw.length; i++) {
char temp = 'a';
char c = mw[i];
if (isPass(c)) {
if (isSmall(c)) {
temp = (char) (c + yiwei);
if (!isPass(temp)) {
temp = (char) (temp - smallz + smallZero);
}
} else if (isBig(c)) {
temp = (char) (c + yiwei);
if (!isPass(temp)) {
temp = (char) (temp - bigZ + bigZero);
}
} else if (isSpace(c)) {
temp = c;
}
} else {
return sBuff.append("输入中带有非法字符!").toString();
}
sBuff.append(temp);
}
System.out.println("加密成功!");
return sBuff.toString();
}
/**
* 解密
*
* @param miwen
* 密文
* @param yiwei
* 左移的位数
* @return 明文
*/
public String deCode(String miwen, int yiwei) {
StringBuffer sBuff = new StringBuffer();
char[] mw = miwen.toCharArray();
System.out.println("解密中----");
for (int i = 0; i < mw.length; i++) {
char temp = 'a';
char c = mw[i];
if (isPass(c)) {
if (isSmall(c)) {
temp = (char) (c - yiwei);
if (!isPass(temp)) {
temp = (char) (smallz - smallZero + temp);
}
} else if (isBig(c)) {
temp = (char) (c - yiwei);
if (!isPass(temp)) {
temp = (char) (bigZ - bigZero + temp);
}
} else if (isSpace(c)) {
temp = c;
}
} else {
return sBuff.append("密文中带有非法字符!").toString();
}
sBuff.append(temp);
}
System.out.println("解密成功!");
return sBuff.toString();
}
public static void main(String[] args) {
MyTiHuan mt = new MyTiHuan();
System.out.println("请输入明文:");
Scanner scan = new Scanner(System.in);
String mingw = scan.nextLine();
String miw = mt.enCode(mingw, 3);
System.out.println("密文为:");
System.out.println(miw);
mingw = mt.deCode(miw, 3);
System.out.println("明文为:");
System.out.println(mingw);
}
}
运行结果: