前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >PBE加密 .net 实现

PBE加密 .net 实现

作者头像
FreeTimeWorker
发布2020-08-31 13:25:46
6290
发布2020-08-31 13:25:46
举报
文章被收录于专栏:C#开发点点滴滴
代码语言:javascript
复制
using System;
using System.Security.Cryptography;
using System.Text;

namespace Demo
{
    internal class PKCSKeyGenerator
    {
        byte[] key = new byte[8], iv = new byte[8];
        DESCryptoServiceProvider des = new DESCryptoServiceProvider();
        public byte[] Key { get { return key; } }
        public byte[] IV { get { return iv; } }
        public ICryptoTransform Encryptor { get { return des.CreateEncryptor(key, iv); } }
        public ICryptoTransform Decryptor { get { return des.CreateDecryptor(key, iv); } }
        public PKCSKeyGenerator() { }
        public PKCSKeyGenerator(String keystring, byte[] salt, int md5iterations, int segments)
        {
            Generate(keystring, salt, md5iterations, segments);
        }
        public ICryptoTransform Generate(String keystring, byte[] salt, int md5iterations, int segments)
        {
            int HASHLENGTH = 16;
            byte[] keymaterial = new byte[HASHLENGTH * segments];
            byte[] psbytes;
            psbytes = Encoding.UTF8.GetBytes(keystring);
            byte[] data00 = new byte[psbytes.Length + salt.Length];
            Array.Copy(psbytes, data00, psbytes.Length);
            Array.Copy(salt, 0, data00, psbytes.Length, salt.Length);
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] result = null;
            byte[] hashtarget = new byte[HASHLENGTH + data00.Length];

            for (int j = 0; j < segments; j++)
            {
                if (j == 0)
                {
                    result = data00;
                }
                else
                {
                    Array.Copy(result, hashtarget, result.Length);
                    Array.Copy(data00, 0, hashtarget, result.Length, data00.Length);
                    result = hashtarget;
                }
                for (int i = 0; i < md5iterations; i++)
                {
                    result = md5.ComputeHash(result);
                }
                Array.Copy(result, 0, keymaterial, j * HASHLENGTH, result.Length);
            }
            Array.Copy(keymaterial, 0, key, 0, 8);
            Array.Copy(keymaterial, 8, iv, 0, 8);
            return Encryptor;
        }
    }
}
代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.Text;

namespace Demo
{
    /// <summary>
    /// PBE加密
    /// </summary>
    public class PBE
    {
        /// <summary>
        /// 根据密码密钥和干扰盐值得到加密后的密文
        /// </summary>
        /// <param name="passWord">密码</param>
        /// <param name="passWordKey">加密key</param>
        /// <param name="salt">干扰盐</param>
        /// <returns></returns>
        public static String Encryption(String passWord, String passWordKey, string salt="00000000")
        {
            int count = 1000; 
            PKCSKeyGenerator cipher = new PKCSKeyGenerator(passWordKey, Encoding.UTF8.GetBytes(salt), count, 1);
            byte[] src = Encoding.UTF8.GetBytes(passWord);
            byte[] result = cipher.Encryptor.TransformFinalBlock(src, 0, src.Length);
            string ret = "";
            for (int i = 0; i < result.Length; i++)
            {
                ret += Convert.ToString(result[i], 16).PadLeft(2, '0');
            }
            return ret;
        }
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019-10-30 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档