Java通用BouncyCastle實現(xiàn)的DES3加密的方法
對于BouncyCastle類庫(包)來說,他提供了很多加密算法,在與.net和java進(jìn)行相互加解密過程中,得到了不錯的應(yīng)用,本文以DES3為例,來說一下DES3加解密的過程。
加密過程
- 明文字符轉(zhuǎn)為byte數(shù)組
- 對密鑰進(jìn)行處理,處理后一般為16或者24字節(jié)
- 對明文進(jìn)行DES3加密,生成密文的byte數(shù)組
- 對密文byte數(shù)組進(jìn)行base64的編碼
解密過程
- 對密文byte數(shù)組進(jìn)行base64的解碼
- 對密鑰進(jìn)行處理,處理后一般為16或者24字節(jié)
- 對解碼后的byte數(shù)組進(jìn)行DES3解密
- 對解密之后的byte數(shù)組進(jìn)行Encoding.UTF8.GetString方法的調(diào)用生成明文字符串
原碼
/// <summary> /// DES3加密 /// https://www.go4expert.com/articles/bouncy-castle-net-implementation-triple-t24829/ /// </summary> public class BouncyCastleHelper { static IBlockCipher engine = new DesEngine(); /// <summary> /// 生成一個16位的key. /// </summary> /// <returns></returns> public string GenerateDES3Key() { CipherKeyGenerator cipherKeyGenerator = new CipherKeyGenerator(); cipherKeyGenerator.Init(new KeyGenerationParameters(new SecureRandom(), 192)); //192 specifies the size of key in bits i.e 24 bytes var keyDES3 = cipherKeyGenerator.GenerateKey(); BigInteger bigInteger = new BigInteger(keyDES3); return bigInteger.ToString(16); } /// <summary> /// 做一個16位的md5加密,防止被其它人解析. /// </summary> /// <param name="Source"></param> /// <returns></returns> static byte[] GetMd5Digest(string Source) { var msgBytes = Encoding.UTF8.GetBytes(Source); var md5Digest = new MD5Digest(); md5Digest.BlockUpdate(msgBytes, 0, msgBytes.Length); byte[] result = new byte[md5Digest.GetDigestSize()]; md5Digest.DoFinal(result, 0); return result; } /// <summary> /// 使用DES3加密 /// </summary> /// <param name="plainText">需要加密的字符串</param> /// <param name="keys">加密字符串的密鑰</param> /// <returns>加密后的字符串</returns> public static string Encrypt(string plainText, string keys) { byte[] ptBytes = Encoding.UTF8.GetBytes(plainText); byte[] rv = Encrypt(ptBytes, keys); // 密文轉(zhuǎn)為base64字符串 return Convert.ToBase64String(rv); } static byte[] Encrypt(byte[] ptBytes, string keys) { byte[] key = GetMd5Digest(keys); BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new DesEdeEngine()); cipher.Init(true, new KeyParameter(key)); byte[] rv = new byte[cipher.GetOutputSize(ptBytes.Length)]; int tam = cipher.ProcessBytes(ptBytes, 0, ptBytes.Length, rv, 0); cipher.DoFinal(rv, tam); return rv; } /// <summary> /// 使用DES3解密 /// </summary> /// <param name="cipherText">需要加密的字符串</param> /// <param name="keys">加密字符串的密鑰</param> /// <returns>解密后的字符串</returns> public static string Decrypt(string cipherText, string keys) { // 把密文進(jìn)行base64的解碼 byte[] base64StringBytes = Convert.FromBase64String(cipherText); var rv = Decrypt(base64StringBytes, keys); // 字符數(shù)組轉(zhuǎn)為明文字符串 return Encoding.UTF8.GetString(rv, 0, rv.Length); } static byte[] Decrypt(byte[] cipherText, string keys) { byte[] key = GetMd5Digest(keys); BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new DesEdeEngine()); cipher.Init(false, new KeyParameter(key)); byte[] comparisonBytes = new byte[cipher.GetOutputSize(cipherText.Length)]; int length = cipher.ProcessBytes(cipherText, comparisonBytes, 0); cipher.DoFinal(comparisonBytes, length); //Do the final block return comparisonBytes; } }
調(diào)用
string result = BouncyCastleHelper.Encrypt("hello", "abc123"); Console.WriteLine("hello=" + result); Console.WriteLine("plainText=" + BouncyCastleHelper.Decrypt(result, "abc123"));
結(jié)果
到此這篇關(guān)于Java通用BouncyCastle實現(xiàn)的DES3加密的文章就介紹到這了,更多相關(guān)java實現(xiàn)DES3加密內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java實現(xiàn)用位運算維護(hù)狀態(tài)碼
位運算是一種非常高效的運算方式,在算法考察中比較常見,那么業(yè)務(wù)代碼中我們?nèi)绾问褂梦贿\算呢,感興趣的小伙伴快跟隨小編一起學(xué)習(xí)一下吧2024-03-03MyBatis中動態(tài)SQL語句@Provider的用法
本文主要介紹了MyBatis中動態(tài)SQL語句@Provider的用法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06java數(shù)據(jù)結(jié)構(gòu)與算法之奇偶排序算法完整示例
這篇文章主要介紹了java數(shù)據(jù)結(jié)構(gòu)與算法之奇偶排序算法,較為詳細(xì)的分析了奇偶算法的原理并結(jié)合完整示例形式給出了實現(xiàn)技巧,需要的朋友可以參考下2016-08-08Struts2學(xué)習(xí)筆記(3)-DMI動態(tài)調(diào)用方式
本文主要介紹Struts2的DMI動態(tài)調(diào)用的兩種方式,簡單實用,希望能給大家做一個參考。2016-06-06使用vue3.x+vite+element-ui+vue-router+vuex+axios搭建項目
因為vue3出了一段時間了,element也出了基于vue3.x版本的element-plus,這篇文章就拿他們搭建一個項目,希望能給你帶來幫助2021-08-08MySQL text類型對應(yīng)mybatis jdbcType類型方式
這篇文章主要介紹了MySQL text類型對應(yīng)mybatis jdbcType類型方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-07-07使用SpringMVC響應(yīng)json格式返回的結(jié)果類型
這篇文章主要介紹了使用SpringMVC響應(yīng)json格式返回的結(jié)果類型,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07