java加密算法分享(rsa解密、對稱加密、md5加密)
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import com.sun.mail.util.BASE64DecoderStream;
import com.sun.mail.util.BASE64EncoderStream;
public class util {
/**
* 傳入名文和公鑰鑰對數(shù)據(jù)進(jìn)行RSA解密
* <br>返回值:String
* <br>@param src
* <br>@param pubkey
* <br>@return
*/
public static String rsaEncoding(String src,PublicKey pubkey){
try {
Cipher cip = Cipher.getInstance("RSA");
cip.init(cip.ENCRYPT_MODE, pubkey);
byte[] by = cip.doFinal(src.getBytes());
return new String(BASE64EncoderStream.encode(by));
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
} catch (NoSuchPaddingException e) {
throw new RuntimeException(e);
} catch (InvalidKeyException e) {
throw new RuntimeException(e);
} catch (IllegalBlockSizeException e) {
throw new RuntimeException(e);
} catch (BadPaddingException e) {
throw new RuntimeException(e);
}
}
/**
* 傳入RSA密文和私鑰對數(shù)據(jù)進(jìn)行解密
* <br>返回值:String
* <br>@param sec
* <br>@param privkey
* <br>@return
*/
public static String rsaDeEncoding(String sec,PrivateKey privkey){
try {
Cipher cip = Cipher.getInstance("RSA");
cip.init(cip.DECRYPT_MODE, privkey);
byte[] by = BASE64DecoderStream.decode(sec.getBytes());
return new String(cip.doFinal(by));
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
} catch (NoSuchPaddingException e) {
throw new RuntimeException(e);
} catch (InvalidKeyException e) {
throw new RuntimeException(e);
} catch (IllegalBlockSizeException e) {
throw new RuntimeException(e);
} catch (BadPaddingException e) {
throw new RuntimeException(e);
}
}
/**
* 傳入字符串、密鑰,并加密字符串(對稱加密加密),支持:DES、AES、DESede(3DES)
* <br>返回值:String 密文
* <br>@param src
* <br>@param key
* <br>@param method(DES、AES、DESede)
* <br>@return
*/
//對稱加密加密
public static String doubKeyEncoding(String src,String keysrc,String method) {
SecretKey key;
try {
//生成密鑰
KeyGenerator kg = KeyGenerator.getInstance(method);
//初始化此密鑰生成器。
kg.init(new SecureRandom(keysrc.getBytes("utf-8")));
key = kg.generateKey();
//加密
Cipher ciph = Cipher.getInstance(method);
ciph.init(Cipher.ENCRYPT_MODE, key);
ciph.update(src.getBytes("utf-8"));
//使用64進(jìn)行編碼,一避免出現(xiàn)丟數(shù)據(jù)情景
byte[] by = BASE64EncoderStream.encode(ciph.doFinal());
return new String(by);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
} catch (NoSuchPaddingException e) {
throw new RuntimeException(e);
} catch (InvalidKeyException e) {
throw new RuntimeException(e);
} catch (IllegalBlockSizeException e) {
throw new RuntimeException(e);
} catch (BadPaddingException e) {
throw new RuntimeException(e);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
/**
* 傳入字符串、密鑰、加密方式,并解密字符串(對稱加密解密密),支持:DES、AES、DESede(3DES)
* <br>生成時(shí)間:2014年5月2日 下午1:12:13
* <br>返回值:String 密鑰原文
* <br>@param sec
* <br>@param key
* <br>@param method(DES、AES、DESede)
* <br>@return
*/
public static String doubKeyDencoding(String sec,String keysrc,String method) {
SecretKey key;
try {
//生成密鑰
KeyGenerator kg = KeyGenerator.getInstance(method);
//初始化此密鑰生成器。
kg.init(new SecureRandom(keysrc.getBytes("utf-8")));
key = kg.generateKey();
//加密
Cipher ciph = Cipher.getInstance(method);
ciph.init(ciph.DECRYPT_MODE, key);
//使用64進(jìn)行解碼,一避免出現(xiàn)丟數(shù)據(jù)情景
byte[] by = BASE64DecoderStream.decode(sec.getBytes());
ciph.update(by);
return new String(ciph.doFinal());
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
} catch (NoSuchPaddingException e) {
throw new RuntimeException(e);
} catch (InvalidKeyException e) {
throw new RuntimeException(e);
} catch (IllegalBlockSizeException e) {
throw new RuntimeException(e);
} catch (BadPaddingException e) {
throw new RuntimeException(e);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
/**
* 單向信息加密(信息摘要),支持:md5、md2、SHA(SHA-1,SHA1)、SHA-256、SHA-384、SHA-512,
* <br>返回值:String 加密后的密文
* <br>@param src 傳入加密字符串(明文)
* <br>@param method 指定算法(md5、md2、SHA(SHA-1,SHA1)、SHA-256、SHA-384、SHA-512)
* <br>@return
*/
public static String ecodingPasswd(String src,String method){
try {
//信息摘要算法
MessageDigest md5 = MessageDigest.getInstance(method);
md5.update(src.getBytes());
byte[] encoding = md5.digest();
//使用64進(jìn)行編碼,一避免出現(xiàn)丟數(shù)據(jù)情景
return new String(BASE64EncoderStream.encode(encoding));
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e+"加密失?。?!");
}
}
}
相關(guān)文章
SpringSecurity+jwt+captcha登錄認(rèn)證授權(quán)流程總結(jié)
本文介紹了SpringSecurity、JWT和驗(yàn)證碼在Spring Boot 3.2.0中的應(yīng)用,包括登錄認(rèn)證和授權(quán)流程的詳細(xì)步驟,本文給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-11-11Java阻塞隊(duì)列BlockingQueue基礎(chǔ)與使用
本文詳細(xì)介紹了BlockingQueue家庭中的所有成員,包括他們各自的功能以及常見使用場景,通過實(shí)例代碼介紹了Java 阻塞隊(duì)列BlockingQueue的相關(guān)知識,需要的朋友可以參考下2023-01-01Java如何取掉json數(shù)據(jù)中值為null的屬性字段
這篇文章主要介紹了Java如何取掉json數(shù)據(jù)中值為null的屬性字段,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03在SpringBoot項(xiàng)目中如何實(shí)現(xiàn)線程池的動(dòng)態(tài)監(jiān)控
Spring Boot因其簡便、高效的特點(diǎn)廣受開發(fā)者喜愛,在復(fù)雜的業(yè)務(wù)場景下,如何確保Spring Boot應(yīng)用的高性能和穩(wěn)定性成為了一個(gè)關(guān)鍵問題,其中,線程池的管理策略直接影響到系統(tǒng)的吞吐量和資源利用效率,本文將重點(diǎn)探討在Spring Boot項(xiàng)目中,如何實(shí)現(xiàn)線程池的動(dòng)態(tài)監(jiān)控2023-10-10