1. 程式人生 > 程式設計 >Java通用BouncyCastle實現的DES3加密的方法

Java通用BouncyCastle實現的DES3加密的方法

對於BouncyCastle類庫(包)來說,他提供了很多加密演算法,在與.net和java進行相互加解密過程中,得到了不錯的應用,本文以DES3為例,來說一下DES3加解密的過程。

加密過程

  • 明文字元轉為byte陣列
  • 對金鑰進行處理,處理後一般為16或者24位元組
  • 對明文進行DES3加密,生成密文的byte陣列
  • 對密文byte陣列進行base64的編碼

解密過程

  • 對密文byte陣列進行base64的解碼
  • 對金鑰進行處理,處理後一般為16或者24位元組
  • 對解碼後的byte陣列進行DES3解密
  • 對解密之後的byte陣列進行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,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,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);
      // 字元陣列轉為明文字串
      return Encoding.UTF8.GetString(rv,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實現的DES3加密的方法

到此這篇關於Java通用BouncyCastle實現的DES3加密的文章就介紹到這了,更多相關java實現DES3加密內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!