PBE加密 .net 实现

2020-08-31 13:25:46 浏览数 (1)

代码语言: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;
        }
    }
}

0 人点赞