欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java通用BouncyCastle實(shí)現(xiàn)的DES3加密的方法

 更新時(shí)間:2020年12月28日 10:33:00   作者:張占嶺 lind  
這篇文章主要介紹了Java通用BouncyCastle實(shí)現(xiàn)的DES3加密的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作,具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

對(duì)于BouncyCastle類(lèi)庫(kù)(包)來(lái)說(shuō),他提供了很多加密算法,在與.net和java進(jìn)行相互加解密過(guò)程中,得到了不錯(cuò)的應(yīng)用,本文以DES3為例,來(lái)說(shuō)一下DES3加解密的過(guò)程。

加密過(guò)程

  • 明文字符轉(zhuǎn)為byte數(shù)組
  • 對(duì)密鑰進(jìn)行處理,處理后一般為16或者24字節(jié)
  • 對(duì)明文進(jìn)行DES3加密,生成密文的byte數(shù)組
  • 對(duì)密文byte數(shù)組進(jìn)行base64的編碼

解密過(guò)程

  • 對(duì)密文byte數(shù)組進(jìn)行base64的解碼
  • 對(duì)密鑰進(jìn)行處理,處理后一般為16或者24字節(jié)
  • 對(duì)解碼后的byte數(shù)組進(jìn)行DES3解密
  • 對(duì)解密之后的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>
    /// 生成一個(gè)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>
    /// 做一個(gè)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實(shí)現(xiàn)的DES3加密的文章就介紹到這了,更多相關(guān)java實(shí)現(xiàn)DES3加密內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論