加密算法

懒的打字,代码很简单:
package org.taotao.test;

import java.io.PrintStream;

public class Encryptstr {

static String sWrong;

static String sSkey = "1314520";

public Encryptstr() {
sWrong = "";
}

public static String encryptkey(String s, int i) {
String s1 = s;
int j = s.length();
if (i <= j)
s1 = s.substring(0, i);
else
for (; j < i; j = s1.length())
if (j + s.length() >= i)
s1 = s1 + s.substring(0, i - j);
else
s1 = s1 + s;

return s1;
}

public String encrypt(String s, String s1) {
String s2 = "";
if (s1 == "") {
sWrong = "字符串为空!";
return s2;
}
if (s == "") {
sWrong = "请指定密钥!";
return s2;
}
int i = s1.length();
String s3 = encryptkey(s, i);
for (int k = 0; k < i; k++) {
char c = s1.charAt(k);
char c1 = s3.charAt(k);
char c2 = c;
char c3 = c1;
int j = c2 ^ c3;
j += 29;
j = 1000 + ((j / 10) % 10) * 100 + (j / 100) * 10 + j % 10;
s2 = s2 + Integer.toString(j).substring(1);
}

return s2;
}

public String encrypt(String s) {
return encrypt(sSkey, s);
}

public static String decode(String s, String s1) {
String s2 = "";
if (s1 == "") {
sWrong = "字符串为空!";
return s2;
}
if (s == "") {
sWrong = "请指定密钥!";
return s2;
}
int i = s1.length() / 3;
String s3 = encryptkey(s, i);
for (int l = 0; l < i; l++) {
String s4 = s1.substring(l * 3, (l + 1) * 3);
char c1 = s3.charAt(l);
int k = Integer.parseInt(s4);
k = ((k / 10) % 10) * 100 + (k / 100) * 10 + k % 10;
char c2 = c1;
int j = k - 29 ^ c2;
char c = (char) j;
s2 = s2 + c;
}

return s2;
}

public static String decode(String s) {
return decode(sSkey, s);
}

public static void main(String args[]) throws Exception {
//以下是测试
System.out.println("以下是测试");
String str = "admin";
String key = "1314520";
Encryptstr encrypt = new Encryptstr();

str = encrypt.encrypt(str);// 加密
System.out.println(str);

str = decode(key, str); // 解密
System.out.println(str);
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值