java 实现文件的加密输入和解密输出

来源:百度知道 编辑:UC知道 时间:2024/06/05 01:07:06

加密有多种算法,不知道你需要什么样的,下面是凯撒密码的例子,参考下吧

/**
* 该程序即可加密又可以解密,解密时在密钥上加入-号即可
*
* @param args
* args[0]:加密字符串 args[1]:密钥
*/
public static void main(String[] args) {
String result = "";
try {
// 需要加密字符串
String s = args[0];
// 密钥
int key = Integer.parseInt(args[1]);
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c >= 'a' && c <= 'z') {// 是小写字母
c += key % 26;// 移动key%26位
if (c < 'a')
c += 26; // 向左超界
if (c > 'z')
c -= 26;// 向右超界
} else if (c < 'A' && c > 'Z') {// 是大写字母
c += key % 26;
if (c < 'A')
c += 26;
if (c > 'Z')
c -= 26;
}
result += c;
}
System.out.println(result);
} catch (Exception e)