这篇文章将为大家详细讲解有关使用java怎么实现一个可逆加密算法,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
package cn.exam.signup.service.pay.util;
import java.math.BigInteger;
import java.util.Arrays;
public class EncrUtil {
private static final int RADIX = 16;
private static final String SEED = "0933910847463829232312312";
public static final String encrypt(String password) {
if (password == null)
return "";
if (password.length() == 0)
return "";
BigInteger bi_passwd = new BigInteger(password.getBytes());
BigInteger bi_r0 = new BigInteger(SEED);
BigInteger bi_r1 = bi_r0.xor(bi_passwd);
return bi_r1.toString(RADIX);
}
public static final String decrypt(String encrypted) {
if (encrypted == null)
return "";
if (encrypted.length() == 0)
return "";
BigInteger bi_confuse = new BigInteger(SEED);
try {
BigInteger bi_r1 = new BigInteger(encrypted, RADIX);
BigInteger bi_r0 = bi_r1.xor(bi_confuse);
return new String(bi_r0.toByteArray());
} catch (Exception e) {
return "";
}
}
public static void main(String args[]){
System.out.println(Arrays.toString(args));
if(args==null || args.length!=2) return;
if("-e".equals(args[0])){
System.out.println(args[1]+" encrypt password is "+encrypt(args[1]));
}else if("-d".equals(args[0])){
System.out.println(args[1]+" decrypt password is "+decrypt(args[1]));
}else{
System.out.println("args -e:encrypt");
System.out.println("args -d:decrypt");
}
}
}
运行以上代码:
[-e, 1234567890]
1234567890 encrypt password is 313233376455276898a5[-d, 313233376455276898a5]
313233376455276898a5 decrypt password is 1234567890
关于使用java怎么实现一个可逆加密算法就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。