标签:
加密算法
public static string Base64Encrypt(string saltStr,string sourceStr) { string targetStr = string.Empty; sourceStr = saltStr + sourceStr; if (sourceStr.Length % 3 == 1) {sourceStr = sourceStr + "=" + "="; } else if(sourceStr.Length%3==2) { sourceStr = sourceStr + "="; } byte[] sourceByte = UnicodeEncoding.Unicode.GetBytes(sourceStr); int count = sourceByte.Length; List<byte> by = new List<byte>(); for (int current = 0; current < count; current++) { by.AddRange(toBit(sourceByte[current])); } byte[] bit8 = new byte[8]; byte[] bit24 = new byte[24]; //temp int i = 0; int j = 0; List<byte> targetByte = new List<byte>(); foreach (byte b in by) { bit24[i] = b; if (i == 23) { bit24[i] = b; targetByte.AddRange(bit24tobit32(bit24)); i =-1; } i++; } i = 0; byte[] byy = new byte[targetByte.Count / 8]; foreach (byte b in targetByte) { bit8[i] = b; if (i == 7) { bit8[7] = b; byy[j++] = (byte)toNum(bit8); i = -1; } i++; } targetStr = UnicodeEncoding.Unicode.GetString(byy); return targetStr; }
解密算法
public static string Base64Unencrypt(string saltStr,string sourceStr) { string targetStr = string.Empty; byte[] temp = UnicodeEncoding.Unicode.GetBytes(sourceStr); //转byte byte[] sourceByte=new byte[0]; List<byte> targetByte = new List<byte>(); byte[] bit8 = new byte[8]; int i = 0; int j = 0; for (int m = 0; m < temp.Length; m++) { sourceByte = sourceByte.Concat(toBit(temp[m])).ToArray(); } foreach (byte b in sourceByte) { bit8[i] = b; if (i == 7) { bit8[i] = b; targetByte.AddRange(bit8.ToList<byte>().GetRange(2,6)); i = -1; } i++; } i = 0; byte[] byy=new byte[targetByte.Count/8]; foreach (byte b in targetByte) { bit8[i] = b; if (i == 7) { bit8[7] = b; byy[j++] = (byte)toNum(bit8); i = -1; } i++; } targetStr = UnicodeEncoding.Unicode.GetString(byy); targetStr = targetStr.TrimEnd(‘=‘); targetStr = targetStr.Substring(saltStr.Length - 1); return targetStr; }
所需函数
public static byte[] bit24tobit32(byte[] bit24) { byte[] bit32 = new byte[32]; int j = -1; for (int i = 0; i < 24; i++) { if (i % 6 == 0) { bit32[++j] = 0; bit32[++j] = 0; bit32[++j] = bit24[i]; } else { bit32[++j] = bit24[i]; } } return bit32; } public static int toNum(byte[] by) { int num = 0; int i = 7; foreach (byte b in by) { if (b != 0) { num = num + (int)Math.Pow(2,i); } i--; } return num; } public static byte[] toBit(byte num1) { byte[] bit = new byte[8]; int num = Convert.ToInt32(num1); if (num > -1 || num < 128) { for (int i = 0; i < 8; i++) { bit[7 - i] = (byte)((num - ((num = num >> 1) << 1))); } } else { throw new IndexOutOfRangeException("超出范围"); } return bit; }
测试代码
Console.WriteLine("输入需base64加密字符串"); string saltStr = string.Empty; string sourceStr = string.Empty; Console.WriteLine("输入加盐字符串"); saltStr = Console.ReadLine(); Console.WriteLine("输入需base64加密字符串"); sourceStr = Console.ReadLine(); string encryptStr = Base64Encrypt(saltStr, sourceStr); Console.WriteLine("加密后的字符串"+encryptStr); Console.WriteLine("输入加盐字符串"); saltStr = Console.ReadLine(); Console.WriteLine("输入需base64解密字符串,字符乱糟糟的不输了"); //sourceStr = Console.ReadLine(); string unEncryptStr = Base64Unencrypt(saltStr, encryptStr); Console.WriteLine("加密后的字符串" + unEncryptStr);
测试结果
标签:
原文地址:http://my.oschina.net/hunjixin/blog/508710