加密算法其它内容
Posted weiweixiang
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了加密算法其它内容相关的知识,希望对你有一定的参考价值。
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; } }
以上是关于加密算法其它内容的主要内容,如果未能解决你的问题,请参考以下文章