linux基础学习,Java加密解密介绍

/*编码格式;默认使用uft-8/

public String charset = “utf-8”;

/*DES/

public int keysizeDES = 0;

/*AES/

public int keysizeAES = 128;

public static EncryptUtil me;

private EncryptUtil(){

//单例

}

//双重锁

public static EncryptUtil getInstance(){

if (me==null) {

synchronized (EncryptUtil.class) {

if(me == null){

me = new EncryptUtil();

}

}

}

return me;

}

/**

  • 使用MessageDigest进行单向加密(无密码)

  • @param res 被加密的文本

  • @param algorithm 加密算法名称

  • @return

*/

private String messageDigest(String res,String algorithm){

try {

MessageDigest md = MessageDigest.getInstance(algorithm);

byte[] resBytes = charset==null?res.getBytes():res.getBytes(charset);

return base64(md.digest(resBytes));

} catch (Exception e) {

e.printStackTrace();

}

return null;

}

/**

  • 使用KeyGenerator进行单向/双向加密(可设密码)

  • @param res 被加密的原文

  • @param algorithm 加密使用的算法名称

  • @param key 加密使用的秘钥

  • @return

*/

private String keyGeneratorMac(String res,String algorithm,String key){

try {

SecretKey sk = null;

if (key==null) {

KeyGenerator kg = KeyGenerator.getInstance(algorithm);

sk = kg.generateKey();

}else {

byte[] keyBytes = charset==null?key.getBytes():key.getBytes(charset);

sk = new SecretKeySpec(keyBytes, algorithm);

}

Mac mac = Mac.getInstance(algorithm);

mac.init(sk);

byte[] result = mac.doFinal(res.getBytes());

return base64(result);

} catch (Exception e) {

e.printStackTrace();

}

r

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值