文章出處

.NET Core上面的DES等加密算法要等到1.2 才支持,我們可是急需這個算法的支持,文章《使用 JavaScriptService 在.NET Core 里實現DES加密算法》需要用Nodejs,很多人覺得這個有點不好,今天就給大家介紹下BouncyCastle (Portable.BouncyCastle)https://www.nuget.org/packages/Portable.BouncyCastle/庫為我們提供的原生的.NET Core的支持庫的Des算法。BouncyCastle的文檔比較少,折騰了好久才寫出了.NET 代碼等價的一個封裝。

  public class TDesbouncy
    {

        IBlockCipher engine = new DesEngine();

        /// <summary>
        /// 使用DES加密,key輸入密碼的時候,必須使用英文字符,區分大小寫,且字符數量是8個,不能多也不能少
        /// </summary>
        /// <param name="plainText">需要加密的字符串</param>
        /// <param name="keys">加密字符串的密鑰</param>
        /// <returns>加密后的字符串</returns>
        public string Encrypt(string keys, string plainText)
        {

            byte[] ptBytes = Encoding.UTF8.GetBytes(plainText);
            byte[] rv = Encrypt(keys, ptBytes);
            StringBuilder ret = new StringBuilder();
            foreach (byte b in rv)
            {
                ret.AppendFormat("{0:X2}", b);
            }
            return ret.ToString();
        }

        private byte[] Encrypt(string keys, byte[] ptBytes)
        {
            byte[] key = Encoding.UTF8.GetBytes(keys);
            BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CbcBlockCipher(engine),new Pkcs7Padding());
            cipher.Init(true, new ParametersWithIV(new DesParameters(key),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>
        /// 使用DES解密,key輸入密碼的時候,必須使用英文字符,區分大小寫,且字符數量是8個,不能多也不能少
        /// </summary>
        /// <param name="cipherText">需要加密的字符串</param>
        /// <param name="keys">加密字符串的密鑰</param>
        /// <returns>解密后的字符串</returns>
        public string Decrypt(string keys, string cipherText)
        {
            byte[] inputByteArray = new byte[cipherText.Length / 2];
            for (int x = 0; x < cipherText.Length / 2; x++)
            {
                int i = (Convert.ToInt32(cipherText.Substring(x * 2, 2), 16));
                inputByteArray[x] = (byte)i;
            }
            var rv = Decrypt(keys, inputByteArray);

            return Encoding.UTF8.GetString(rv);

        }

        private byte[] Decrypt(string keys, byte[] cipherText)
        {
            byte[] key = Encoding.UTF8.GetBytes(keys);
            BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CbcBlockCipher(engine));
            cipher.Init(false, new ParametersWithIV(new DesParameters(key), key));
            byte[] rv = new byte[cipher.GetOutputSize(cipherText.Length)];
            int tam = cipher.ProcessBytes(cipherText, 0, cipherText.Length, rv, 0);

            cipher.DoFinal(rv, tam);

            return rv;
        }

    }

public static void Main(string[] args)
{
           
            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

            string key = "geffzhan";
            string content =   "This project.config whoopla is a mess. So what they mean by .NetCore, you still have to reference everything correctly";

            TDesbouncy bouncy = new TDesbouncy();

            var encrypt = bouncy.Encrypt(key, content);
            Console.WriteLine(encrypt);

            string descontent = bouncy.Decrypt(key, encrypt);
            Console.WriteLine(descontent);
}


文章列表




Avast logo

Avast 防毒軟體已檢查此封電子郵件的病毒。
www.avast.com


arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()