Java通用BouncyCastle實現(xiàn)的DES3加密的方法
更新時間:2020年12月28日 10:33:00 作者:張占嶺 lind
這篇文章主要介紹了Java通用BouncyCastle實現(xiàn)的DES3加密的方法,本文給大家介紹的非常詳細,對大家的學習或工作,具有一定的參考借鑒價值,需要的朋友可以參考下
對于BouncyCastle類庫(包)來說,他提供了很多加密算法,在與.net和java進行相互加解密過程中,得到了不錯的應用,本文以DES3為例,來說一下DES3加解密的過程。
加密過程
- 明文字符轉為byte數(shù)組
- 對密鑰進行處理,處理后一般為16或者24字節(jié)
- 對明文進行DES3加密,生成密文的byte數(shù)組
- 對密文byte數(shù)組進行base64的編碼
解密過程
- 對密文byte數(shù)組進行base64的解碼
- 對密鑰進行處理,處理后一般為16或者24字節(jié)
- 對解碼后的byte數(shù)組進行DES3解密
- 對解密之后的byte數(shù)組進行Encoding.UTF8.GetString方法的調用生成明文字符串
原碼
/// <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);
// 密文轉為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)
{
// 把密文進行base64的解碼
byte[] base64StringBytes = Convert.FromBase64String(cipherText);
var rv = Decrypt(base64StringBytes, keys);
// 字符數(shù)組轉為明文字符串
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;
}
}
調用
string result = BouncyCastleHelper.Encrypt("hello", "abc123");
Console.WriteLine("hello=" + result);
Console.WriteLine("plainText=" + BouncyCastleHelper.Decrypt(result, "abc123"));
結果

到此這篇關于Java通用BouncyCastle實現(xiàn)的DES3加密的文章就介紹到這了,更多相關java實現(xiàn)DES3加密內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
兩個小例子輕松搞懂 java 中遞歸與尾遞歸的優(yōu)化操作
這篇文章主要介紹了兩個小例子輕松搞懂 java 中遞歸與尾遞歸的優(yōu)化操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09
mybatis?mapper.xml?注釋帶參數(shù)的坑及解決
這篇文章主要介紹了mybatis?mapper.xml?注釋帶參數(shù)的坑及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01
Java多線程教程之如何利用Future實現(xiàn)攜帶結果的任務
Callable與Future兩功能是Java?5版本中加入的,這篇文章主要給大家介紹了關于Java多線程教程之如何利用Future實現(xiàn)攜帶結果任務的相關資料,需要的朋友可以參考下2021-12-12
java 實現(xiàn)websocket的兩種方式實例詳解
這篇文章主要介紹了java 實現(xiàn)websocket的兩種方式實例詳解,一種使用tomcat的websocket實現(xiàn),一種使用spring的websocket,本文通過代碼給大家介紹的非常詳細,需要的朋友可以參考下2018-07-07

