大家好,又见面了,我是你们的朋友全栈君。
传入同一密钥如:key123456 进行加密解密
package com.pub;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
public class PwdTools{
/**
* 加密
*
* @param content
* 待加密内容
* @param key
* 加密的密钥
* @return
*/
public static String encrypt(String content,String key){
try{
KeyGenerator kgen=KeyGenerator.getInstance(“AES”);
SecureRandom secureRandom = SecureRandom.getInstance(“SHA1PRNG” );
secureRandom.setSeed(key.getBytes());
kgen.init(128,secureRandom);
SecretKey secretKey=kgen.generateKey();
byte[] enCodeFormat=secretKey.getEncoded();
SecretKeySpec secretKeySpec=new SecretKeySpec(enCodeFormat,”AES”);
Cipher cipher=Cipher.getInstance(“AES”);
byte[] byteContent=content.getBytes(“utf-8”);
cipher.init(Cipher.ENCRYPT_MODE,secretKeySpec);
byte[] byteRresult=cipher.doFinal(byteContent);
StringBuffer sb=new StringBuffer();
for(int i=0;i
md5加密,不可解密
public static String getMd5(String plainText) {
try {
MessageDigest md = MessageDigest.getInstance(“MD5”);
md.update(plainText.getBytes());
byte b[] = md.digest();
int i;
StringBuffer buf = new StringBuffer(“”);
for (int offset = 0; offset < b.length; offset++) {
i = b[offset];
if (i < 0)
i += 256;
if (i < 16)
buf.append(“0”);
buf.append(Integer.toHexString(i));
}
//32位加密
return buf.toString();
// 16位的加密
//return buf.toString().substring(8, 24);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/159778.html原文链接:https://javaforall.cn
本文参与 腾讯云自媒体分享计划 ,欢迎热爱写作的你一起参与!本文分享自作者个人站点/博客:https://javaforall.cn复制如有侵权,请联系 本站 删除。