前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >原 (手工)base64加密解密

原 (手工)base64加密解密

作者头像
魂祭心
发布2018-05-17 16:02:33
9670
发布2018-05-17 16:02:33
举报
文章被收录于专栏:魂祭心

加密算法

代码语言:javascript
复制
    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;
        }

解密算法

代码语言:javascript
复制
public static string Base64Unencrypt(string saltStr,string sourceStr)
        {
            string targetStr = string.Empty;
            byte[] temp = UnicodeEncoding.Unicode.GetBytes(sourceStr); 
            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;
        }

所需函数

代码语言:javascript
复制
 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;
        }

测试代码

代码语言:javascript
复制
            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);
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档