【加密算法】其它内容

    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;
        }
    }

 

posted @ 2018-12-11 16:11  a boy  阅读(386)  评论(0编辑  收藏  举报