程序名称:Secret.java
程序内容:
package firststudy;
/**
* 加密算法
* @author web
*
*/
public class Secret {
public static void main(String[] args) {
String str = "故人西辞黄鹤楼,烟花三月下扬州,孤帆远影碧空尽,唯见长江天际流。";
char c = 'y';
System.out.println("原始语句 :" + str + "\n");
String ss = secret(str, c);
System.out.println("加密后语句:" + ss + "\n");
String sc = secret(ss, c);
System.out.println("解密后语句:" + sc + "\n");
}
/**
* 加密算法: 将文字转换为字节数组,对数组 异或 操作进行加密.
* @param str
* @param c
* @return
*/
public static String secret(String str, char c) {
byte[] bt = str.getBytes();
for(int i=0; i<bt.length; i++) {
bt[i] = (byte)(bt[i] ^ (int)c); //对每个字符进行 异或 操作.
}
return new String(bt, 0, bt.length);
}
}
运行结果: