栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java通用BouncyCastle实现的DES3加密的方法

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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方法的调用生成明文字符串
原码
 /// 
  /// DES3加密
  /// https://www.go4expert.com/articles/bouncy-castle-net-implementation-triple-t24829/
  /// 
  public class BouncyCastleHelper
  {
    static IBlockCipher engine = new DesEngine();

    /// 
    /// 生成一个16位的key.
    /// 
    /// 
    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);
    }

    /// 
    /// 做一个16位的md5加密,防止被其它人解析.
    /// 
    /// 
    /// 
    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;
    }

    /// 
    /// 使用DES3加密
    /// 
    /// 需要加密的字符串
    /// 加密字符串的密钥
    /// 加密后的字符串
    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;
    }

    /// 
    /// 使用DES3解密
    /// 
    /// 需要加密的字符串
    /// 加密字符串的密钥
    /// 解密后的字符串
    public static string Decrypt(string cipherText, string keys)
    {
      // 把密文进行base64的解码
      byte[] base64StringBytes = Convert.Frombase64String(cipherText);
      var rv = Decrypt(base64StringBytes, keys);
      // 字符数组转为明文字符串
      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实现的DES3加密的文章就介绍到这了,更多相关java实现DES3加密内容请搜索考高分网以前的文章或继续浏览下面的相关文章希望大家以后多多支持考高分网!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/129675.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号