码迷,mamicode.com
首页 > 编程语言 > 详细

【加密算法】其它内容

时间:2018-12-11 17:21:53      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:div   class   summary   end   text   bsp   convert   buffer   format   

    public enum OutputMethod
    {
        Base64 = 1, //Base64编码
        Hex = 2 //Hex编码
    }
internal class EadUtil
    {
        public static string Output(byte[] bytes, OutputMethod method = OutputMethod.Base64)
        {
            switch (method)
            {
                case OutputMethod.Base64:
                    return Base64(bytes);
                case OutputMethod.Hex:
                    return Hex(bytes);
                default:
                    return "";
            }
        }

        public static byte[] Input(string ciphertext, OutputMethod method = OutputMethod.Base64)
        {
            switch (method)
            {
                case OutputMethod.Base64:
                    return Base64(ciphertext);
                case OutputMethod.Hex:
                    return Hex(ciphertext);
                default:
                    return null;
            }
        }

        /// <summary>
        /// 转换为base64位
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        private static string Base64(byte[] bytes)
        {
            return Convert.ToBase64String(bytes);
        }

        /// <summary>
        /// 转换为Hex编码
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        private static string Hex(byte[] bytes)
        {
            StringBuilder builder = new StringBuilder();
            foreach (byte num in bytes)
            {
                builder.AppendFormat("{0:X2}", num);
            }
            return builder.ToString();
        }

        private static byte[] Base64(string ciphertext)
        {
            return Convert.FromBase64String(ciphertext);
        }

        private static byte[] Hex(string ciphertext)
        {
            byte[] buffer = new byte[ciphertext.Length / 2];
            for (int i = 0; i < (ciphertext.Length / 2); i++)
            {
                int num2 = Convert.ToInt32(ciphertext.Substring(i * 2, 2), 0x10);
                buffer[i] = (byte)num2;
            }

            return buffer;
        }
    }

 

【加密算法】其它内容

标签:div   class   summary   end   text   bsp   convert   buffer   format   

原文地址:https://www.cnblogs.com/weiweixiang/p/10102864.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!