C# 加密

看到好多加密算法,觉得自己写一个比较好。
网上保密密码的软件很多,但是不相信,自己写的,强度差也是保密强。
我是用哈希加密。

核心:移位,字符加密,填补空白。首先建立一个空白的字符串,指定长度length,要加密的文本根据密码获得每个字符在空白字符串的位置,这就是移位。然后把加密文本的每个字符都加密,本软件采用字符.unicode+密码.unicode。把空白字符串,放好加密字符串还空白的地方填充随机字符。

想法:
首先用密码得到要加密内容的哈希路径,把要加密内容放到一个临时的数组,路径就是他的位置。
然后把放在位置的字符串进行一次加密。
把空闲的位置放上随机的垃圾。

这样得到了加密的字符串 。

需要内存比较小,可以把加密的内容分为几个部分,然后并行加密。最后需要存储的空间比较大。

加密:
数据结构
string key 储存用户的密码
string str 要加密的字符串
string temp 储存,私有,字符数组,有_temp_length大小,储存加密后字符串

哈希路径函数
作用:
把一个字符依靠key用户密码得到在temp的位置。
例:
输入一个字符c,因为密码key决定在temp位置
计算输入c在temp位置t_position

可以把得到路径的字符放进去temp这样移位得到加密字符串。

哈希冲突函数
作用:
如果计算字符所在temp位置已经有字符,那么进行处理冲突,计算另一个位置。
例:
temp[哈希路径函数()]有字符,使用哈希冲突函数找到
temp[哈希冲突函数()]没有字符

空白填补
作用:
把temp[i]还空白的,填补随机字符
例:
temp[1-10]={c, ,c,c,c,c, ,c,c,c}
空白填补
temp[1-10]={c,填补,c,c,c,c,填补,c,c,c}

字符串加密
作用:
把temp加密,得到最后加密字符串
例:
temp=“123”
加密temp=“tem”

实现:
key[i] i=i>=key.length?0:i++;
用Conver.ToInt32(key[i])获得str第i个字符存temp位置。因为这位置可能比temp大小大,我们l=Conver.ToInt32(key[i])%_temp_length
字符串的i字符并没有在i>=key.length i=0是i++到字符串.length,实际不是使用i变量,而是自己再一个变量。
如果字符串[i]放到的temp[l]有字符了,就往下,找到一个没有字符的位置。
放下str[i],因为看到str[i],我们temp[l]=str[i]+key[i];这里的l是算出密码告诉字符串[i]所在位置和这个位置没有字符,i,str和key不一定是相同,因为如果key.length < str.length,i会变。
看到一片好多空,我们把空放random,得到混乱的密码。
写文件,buf = Encoding.Unicode.GetBytes(temp);

解密:
从文件 _temp_length*2转unicode,
temp = Encoding.Unicode.GetString(buf);
用密码获得字符串的位置,然后把字符串-key[i],解密

密码:
我们要保存key密码,我们使用md5。
key_temp=md5(key);
把key_temp做资源放到前面说的加密,str=key_temp得到密码加密,存放。
一次md5比较容易,我们nmd5,把密码
for(i=0;i<Convert.ToInt32(key[0]);i++)md5

using System;
using System.Diagnostics;
using System.Text;

namespace encryption_note
{
    public class string_decryption
    {
        ///需要 using System.IO;    
        private string_decryption()
        {            
            file_address = @".data.data";
            _temp_string_lenth = 1024;            
        }
        public string_decryption(string file_加密的文件 , int key_length_more512)
        {
            key_length_more512 = 1024;
            _temp_string_lenth = key_length_more512;

            file_address = file_加密的文件;            
        }
        public string_decryption(string key)
        {
            int 密钥_大于512;
            密钥_大于512 = 1024;
            _temp_string_lenth = 密钥_大于512;
            this.key = key;
        }

        public static string_decryption g_获得类()
        {
            return _string_decryption;
        }
        public static string_decryption g_获得类(string file_加密的文件)
        {
            _string_decryption = new string_decryption(file_加密的文件 , 1024);
            return _string_decryption;
        }

        ~string_decryption()
        {


        }

        public string key
        {
            get
            {
                if (_key.Length <= 0)
                {
                    return "林德熙";
                }
                return _key;
            }
            set
            {
                _key = value;
            }
        }
        /// <summary>
        /// 加密文件绝对位置
        /// </summary>
        public string file_address
        {
            set
            {
                _file_address = value;
            }
            get
            {
                return _file_address;
            }
        }
        private int ran
        {
            set
            {
                _random = new Random(value);
            }
            get
            {
                return _random.Next(2) == 0 ? _random.Next(19968 , 40864) : _random.Next(33 , 126);
            }
        }

        public string encryption(string str)
        {
            char[] temp_str = new char[_temp_string_lenth];
            int i , has , key_place;//has字符位置,key_place密码位置
            //str = encryptDes(str);
            str += "结束";
            str.PadRight(_temp_string_lenth);
            for (i = 0; i < _temp_string_lenth; i++)
            {
                temp_str[i] = Convert.ToChar(0);
            }
            key_place = 0;
            for (i = 0; i < str.Length; i++)
            {
                has = Convert.ToInt32(key[key_place]);
                has = has % _temp_string_lenth;
                while (temp_str[has] != Convert.ToChar(0))//如果位置有别的字符就下一个,到没有字符位置
                {
                    has++;
                    if (has >= _temp_string_lenth)
                    {
                        has = 0;
                    }
                    //has=has>=_temp_string_lenth?0:has++;
                }
                //temp_str[l] = (char)(str[i]);//+key[key_l]);
                temp_str[has] = (char)((str[i]) + key[key_place]);
                key_place++;
                if (key_place == key.Length)
                {
                    key_place = 0;
                }
                //key_place=key_place>=key.length?0:key_place++;
            }
            //把空填充
            for (i = 0; i < _temp_string_lenth; i++)
            {
                if (temp_str[i] == Convert.ToChar(0))
                {
                    temp_str[i] = Convert.ToChar(ran); //% 1000+1);
                }
            }
            string s = new string(temp_str);
            return s;
            //return null;
        }
        public string decryption(string str)
        {
            StringBuilder temp = new StringBuilder();
            char[] jie = str.ToCharArray();
            int has , key_place;//has字符位置,key_place密码位置
            bool accomplish;
            accomplish = false;//初始
            has = 0;
            key_place = 0;
            if (jie.Length < _temp_string_lenth - 1)
            {
                Debug.Write("错" + jie.Length.ToString());
                return null;
            }
            while (accomplish == false)//我while(true)
            {
                has = Convert.ToInt32(key[key_place]);
                has = has % _temp_string_lenth;//密码给字符所在位置
                while (jie[has] == Convert.ToChar(0))
                {
                    has++;
                    if (has >= _temp_string_lenth)
                    {
                        accomplish = true;
                        break;
                    }
                }
                if (accomplish)
                {
                    break;
                }               
                temp.Append((char)((jie[has]) - key[key_place]));
                jie[has] = Convert.ToChar(0);//把原来位置0
                key_place++;
                if (key_place == key.Length)
                {
                    key_place = 0;
                }
            }
            string temp_str = temp.ToString();
            int temp_l = temp_str.LastIndexOf("结束");
            if (temp_l > 0)
            {                
                return temp_str.Substring(0 , temp_l);
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 加密key[0].toint次md5
        /// </summary>
        /// <param name="key">密码</param>
        /// <returns>加密后密码</returns>
        public string n_md5(string key)
        {
            string temp;
            int i;
            int str_0_length;
            if (string.IsNullOrEmpty(key))
            {
                temp = "";
                return temp.PadRight(32 , '0');
            }
            str_0_length = Convert.ToInt32(key[0]);
            temp = get_MD5(key);
            for (i = 1; i < str_0_length; i++)
            {
                temp = get_MD5(temp);
            }
            return temp;
        }

        private int _temp_string_lenth;
        private static string_decryption _string_decryption = new string_decryption();
        private Random _random = new Random();
        //加密文件的路径
        private string _file_address;
        private string _key;       
        /// <summary>
        /// md5加密
        /// </summary>
        /// <param name="str">要加密字符串</param>
        /// <returns>加密后密码</returns>
        private string get_MD5(string str)
        {
            System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] temp;
            StringBuilder strb = new StringBuilder();
            temp = md5.ComputeHash(Encoding.Unicode.GetBytes(str));
            md5.Clear();
            for (int i = 0; i < temp.Length; i++)
            {
                strb.Append(temp[i].ToString("X").PadLeft(2 , '0'));
            }
            return strb.ToString().ToLower();            
        }        
    }

}

多个密码:
List<string> decryption;
里面加密密码,写入。每个大小_temp_length;

源码http:// pan.baidu.com/s/1pJ3cRwB 密码: w9td

https://code.csdn.net/lindexi_gd/encryption_note

原文地址:https://www.cnblogs.com/lindexi/p/12087798.html