php bouncy castle,基础才是重中之重~BouncyCastle实现的DES3加密~java通用

对于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[] ptB.........

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值