java加密解密算法_JAVA实现的AZDG加密-解密算法

JAVA实现的AZDG加密-解密算法。源程序:

import java.io.IOException;

import java.security.MessageDigest;

public class Azdg {

public static void main(String[] args) {

Azdg azdg = new Azdg();

System.out.println(System.currentTimeMillis()/1000);

String clearText = "no=sh0105&time=12319399372223`112r3r223";//明文

String cipherText = "";//密文

String cleanText = "";//解密后的

String key = "1234567890";//密钥,可自定义

System.out.println("明文为:" + clearText);

cipherText = azdg.encrypt(clearText, key);

System.out.println("加密后为:"+cipherText);

cleanText = azdg.decrypt(cipherText, key);

System.out.println("解密后为:"+cleanText);

}

/**

* 加密算法

* */

public String encrypt(String txt, String key) {

String encrypt_key = "0f9cfb7a9acced8a4167ea8006ccd098";

int ctr = 0;

String tmp = "";

int i;

for (i = 0; i < txt.length(); i++) {

ctr = (ctr == encrypt_key.length()) ? 0 : ctr;

tmp = tmp + encrypt_key.charAt(ctr)

+ (char)(txt.charAt(i) ^ encrypt_key.charAt(ctr));

ctr++;

}

return base64_encode(key(tmp, key));

}

/**

* 解密算法

*/

public String decrypt(String cipherText, String key) {

// base64解码

cipherText = base64_decode(cipherText);

cipherText = key(cipherText, key);

String tmp = "";

for (int i = 0; i < cipherText.length(); i++) {

int c = cipherText.charAt(i) ^ cipherText.charAt(i + 1);

String x = "" + (char) c;

tmp += x;

i++;

}

return tmp;

}

public String key(String txt, String encrypt_key) {

encrypt_key = strMD5(encrypt_key);

int ctr = 0;

String tmp = "";

for (int i = 0; i < txt.length(); i++) {

ctr = (ctr == encrypt_key.length()) ? 0 : ctr;

int c = txt.charAt(i) ^ encrypt_key.charAt(ctr);

String x = "" + (char) c;

tmp = tmp + x;

ctr++;

}

return tmp;

}

public String base64_encode(String str) {

return new sun.misc.BASE64Encoder().encode(str.getBytes());

}

public String base64_decode(String str) {

sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder();

if (str == null)

return null;

try {

return new String(decoder.decodeBuffer(str));

} catch (IOException e) {

e.printStackTrace();

return null;

}

}

[1]

【责编:landy】

--------------------next---------------------

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值