一种将文件编码为图片格式的办法,的DES加密算法

By admin in 4858.com on 2019年3月29日

世界上的文字能够被五个字节完全覆盖,也正是UTF-32,其余都以变长的格式。而恰好A汉兰达GB加起来八个字节,于是完全能够把三个字符映射为二个像素点嘛!

.NET Core上边的DES等加密算法要等到1.2
才支撑,大家只是要求那个算法的援助,作品《行使 JavaScriptService 在.NET
Core
里福衢寿车DES加密算法》供给用Nodejs,很多人认为那个略带不好,前几天就给大家介绍下BouncyCastle
(Portable.BouncyCastle)库为我们提供的原生的.NET
Core的协助库的Des算法。BouncyCastle的文书档案相比较少,折腾了久久才写出了.NET
代码等价的3个包裹。

刚好学会的C#的加密与解密MD5加密/路虎极光SA加密与解密/DES加密。也是刚刚申请的blog随便公布一下。

为了网站的安全,对密码加密,连接字符串加密时很关键的,未来就来介绍两种常用的加密方法:

再就是图片上的字节能够重复加密,格外好玩!笔者仅演示的一级无敌简单的取反操作。

  public class TDesbouncy
    {

、MD5加密

 MD5:(不可逆的加密)

 

        IBlockCipher engine = new DesEngine();

MD5 md5 = new MD5CryptoServiceProvider();
byte[] palindata =
Encoding.Default.GetBytes(textBox1.Text);//将要加密的字符串转换为字节数组
byte[] encryptdata =
md5.ComputeHash(palindata);//将字符串加密后也转移为字符数组
textBox2.Text =
Convert.ToBase64String(encryptdata);//将加密后的字节数组转换为加密字符串

我们得以使用C#自带的类MD5CryptoServiceProvider去贯彻

自个儿要好习惯看方形的图纸,于是开平方取整,如若字符数量不足就从字符自个儿随机取一段,那是为了图片底部不会多少个像素留白,炒鸡不好看,以金红(0xFFFFFFFF)作为终止字符。

        /// <summary>
        ///
使用DES加密,key输入密码的时候,必须选拔英文字符,区分轻重缓急写,且字符数量是捌个,不可能多也不能够少
        /// </summary>
        /// <param
name=”plainText”>需求加密的字符串</param>
        /// <param name=”keys”>加密字符串的密钥</param>
        /// <returns>加密后的字符串</returns>
        public string Encrypt(string keys, string plainText)
        {

、RAS加密

 MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();

static Bitmap ArgbTextEncode(string input)
        {
            var x = 0;
            var y = 0;
            var lenth = input.Length;
            var yValue = (int)(Math.Sqrt(input.Length) + 1);
            var fill = (yValue * yValue) % (Encoding.Unicode.GetByteCount(input) / 4);
            input += input.Substring(Rnd.random.Next(input.Length - fill - 1), fill);
            var image = new Bitmap(yValue, yValue);

            //fill
            for (int i = 0; i < yValue * yValue; i++)
            {
                if (i == lenth - 1)
                {
                    image.SetPixel(x, y, Color.White);
                }
                else
                {
                    byte[] bytes = Encoding.Unicode.GetBytes(input[i].ToString());
                    var prefix = 4 - bytes.Length;
                    bytes = Enumerable.Repeat<byte>(0, prefix).Concat(bytes).ToArray();
                    Encrypt(bytes);
                    image.SetPixel(x, y, Color.FromArgb(bytes[0], bytes[1], bytes[2], bytes[3]));
                }
                //Console.WriteLine("{0},{1} = {2}", x, y, input[i]);
                x++;
                if (x % yValue == 0)
                {
                    x = 0;
                    y++;
                }
            }
            return image;
        }

            byte[] ptBytes = Encoding.UTF8.GetBytes(plainText);
            byte[] rv = Encrypt(keys, ptBytes);
4858.com,            StringBuilder ret = new StringBuilder();
            foreach (byte b in rv)
            {
一种将文件编码为图片格式的办法,的DES加密算法。                ret.AppendFormat(“{0:X2}”, b);
            }
            return ret.ToString();
        }

首先注明全局变量:CspParameters param;

string
加密后的密码=BitConverter.ToString(md5.ComputeHash(Encoding.Default.GetBytes(原密码))).Replace(“-“,””);

  

        private byte[] Encrypt(string keys, byte[] ptBytes)
        {
            byte[] key = Encoding.UTF8.GetBytes(keys);
            BufferedBlockCipher cipher = new
PaddedBufferedBlockCipher(new CbcBlockCipher(engine),new
Pkcs7Padding());
            cipher.Init(true, new ParametersWithIV(new
DesParameters(key),key));
            byte[] rv = new
byte[cipher.GetOutputSize(ptBytes.Length)];
            int tam = cipher.ProcessBytes(ptBytes, 0, ptBytes.Length,
rv, 0);

RAS加密:

或者

找了2个日志文本,看看效果

             cipher.DoFinal(rv, tam);
             return rv;
        }

param = new CspParameters();
param.KeyContainerName =
“Olive”;//密匙容器的名称,保持加密解密一致才能解密成功
using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider
{
byte[] plaindata =
Encoding.Default.GetBytes(textBox1.Text);//将要加密的字符串转换为字节数组
byte[] encryptdata = rsa.Encrypt(plaindata,
false);//将加密后的字节数据转换为新的加密字节数组
textBox2.Text =
Convert.ToBase64String(encryptdata);//将加密后的字节数组转换为字符串
}

 public string Encrypt1(string password)

4858.com 1

        /// <summary>
        ///
使用DES解密,key输入密码的时候,必须使用英文字符,区分轻重缓急写,且字符数量是8个,无法多也无法少
        /// </summary>
        /// <param
name=”cipherText”>须求加密的字符串</param>
        /// <param name=”keys”>加密字符串的密钥</param>
        /// <returns>解密后的字符串</returns>
        public string Decrypt(string keys, string cipherText)
        {
            byte[] inputByteArray = new byte[cipherText.Length /
2];
            for (int x = 0; x < cipherText.Length / 2; x++)
            {
                int i = (Convert.ToInt32(cipherText.Substring(x * 2,
2), 16));
                inputByteArray[x] = (byte)i;
            }
            var rv = Decrypt(keys, inputByteArray);

RAS解密:

{ //获取byte数组

 

            return Encoding.UTF8.GetString(rv);

param = new CspParameters();
param.KeyContainerName = “Olive”;
using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider
{
byte[] encryptdata = Convert.FromBase64String(this.textBox2.Text);
byte[] decryptdata = rsa.Decrypt(encryptdata, false);
textBox3.Text = Encoding.Default.GetString(decryptdata);
}

Byte[] clearBytes = new UnicodeEncoding().GetBytes(password);

 

        }

DES加密:

 //获取hash值

解码函数

        private byte[] Decrypt(string keys, byte[] cipherText)
        {
            byte[] key = Encoding.UTF8.GetBytes(keys);
            BufferedBlockCipher cipher = new
PaddedBufferedBlockCipher(new CbcBlockCipher(engine));
            cipher.Init(false, new ParametersWithIV(new
DesParameters(key), key));
            byte[] rv = new
byte[cipher.GetOutputSize(cipherText.Length)];
            int tam = cipher.ProcessBytes(cipherText, 0,
cipherText.Length, rv, 0);

先是证明全局变量

 Byte[] hashedBytes =
((HashAlgorithm)CryptoConfig.CreateFromName(“MD5”)).ComputeHash(clearBytes);

 

            cipher.DoFinal(rv, tam);

byte[] buffer;
DESCryptoServiceProvider DesCSP = new DESCryptoServiceProvider();

//获取加密后的音讯

static string DecodeTextFromArgb(Bitmap image)
        {
            //ComplexImage comp = ComplexImage.FromBitmap(image);
            //comp.BackwardFourierTransform();
            var str = "";
            for (int i = 0; i < image.Height; i++)
            {
                for (int j = 0; j < image.Width; j++)
                {
                    var color = image.GetPixel(j, i);
                    if (color.A == 255 && color.R == 255 && color.G == 255 && color.B == 255)
                        return str;
                    var bytes = new byte[] { color.A, color.R, color.G, color.B };
                    Decrypt(bytes);
                    int skip = 0;
                    if (bytes[0] == 0)
                    {
                        if (bytes[1] == 0)
                        {
                            skip = 2;
                        }
                        else
                        {
                            skip = 1;
                        }
                    }
                    else
                    {
                        skip = 0;
                    }
                    var t = Encoding.Unicode.GetString(bytes.Skip(skip).ToArray());
                    //Console.WriteLine("{0},{1} = {2}", j, i, t);
                    str += t;
                }
            }
            return str;
        }

            return rv;
        }

DES解密:

return BitConverter.ToString(hashedBytes);

  

    }

MemoryStream ms = new MemoryStream;//将加密后的字节数据加入内部存款和储蓄器流中
CryptoStream cryStream = new CryptoStream(ms, DesCSP.CreateDecryptor(),
CryptoStreamMode.Read);//内部存款和储蓄器流连接到解密流中
StreamReader sr = new StreamReader(cryStream);
textBox5.Text = sr.ReadLine();//将解密流读取为字符串
sr.Close();
cryStream.Close();
ms.Close();

}

 

public static void Main(string[] args)
{
           
           
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

好了,就先写到那吗,界面就不出示了。

处理后的md5加密方法

当中用到的Encrypt和Decrypt是取反的,不然图片一片桃红,啥看不见。那句是自小编刚想到真的用取反操作符来,实际小编是用的255去减的。

            string key = “geffzhan”;
            string content =   “This project.config whoopla is a mess.
So what they mean by .NetCore, you still have to reference everything
correctly”;

 

unchecked((byte)~(byte)value)

            TDesbouncy bouncy = new TDesbouncy();

4858.com 24858.com 3View Code

  

            var encrypt = bouncy.Encrypt(key, content);
            Console.WriteLine(encrypt);

 public string GetMd5(string str)//MD5 32位加密
        {

Encrypt和Decrypt,其实一样的。。哈哈

            string descontent = bouncy.Decrypt(key, encrypt);
            Console.WriteLine(descontent);
}

            string cl = DateTime.Now.Month + str + DateTime.Now.Day;//将要加密的字符串加上前缀与后缀后再加密;
           
            string pwd = “”;
            MD5 md5 = MD5.Create();//实例化叁个md5对像
            // 加密后是1个字节类型的数组,那里要留意编码UTF8/Unicode等的挑三拣四
            byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(cl));
            s.Reverse();    //翻转生成的MD5码
            // 通过利用循环,将字节类型的数组转换为字符串,此字符串是常规字符格式化所得
            for (int i = 3; i < s.Length – 1; i++) //只取MD5码的一有的;恶意访问者不恐怕知晓取的是哪贰个人。
            {
                // 将赢得的字符串使用十六进制类型格式。格式后的字符是小写的假名,借使采纳大写(X)则格式后的字符是大写字符
                pwd = pwd + (s[i] < 198 ? s[i] + 28 : s[i]).ToString(“X”); // 进一步对转移的MD5码做一些改建。
            }
            return pwd;
        }

static byte[] Encrypt(byte[] values)
        {
            for (int i = 0; i < values.Length; i++)
            {
                values[i] = (byte)(255 - values[i]);
            }
            return values;
        }
        static byte[] Decrypt(byte[] values)
        {
            for (int i = 0; i < values.Length; i++)
            {
                values[i] = (byte)(255 - values[i]);
            }
            return values;
        }

 

  

DES:(可逆的加密)

本程序完整代码如下:

加密

class Program
    {
        static void Main(string[] args)
        {
            if (!args.Any())
            {
                return;
            }
            else if (args[0].EndsWith(".txt", StringComparison.OrdinalIgnoreCase))
            {
                var textRaw = File.ReadAllText(args[0], Encoding.Unicode);
                var image = ArgbTextEncode(textRaw);
                var imagePath = Path.ChangeExtension(args[0], ".png");
                image.Save(imagePath, ImageFormat.Png);
                image.Dispose();
                Console.WriteLine("TXT->PNG, " + imagePath);
                try
                {
                    Process.Start(imagePath);
                }
                catch
                {
                }
            }
            else if (args[0].EndsWith(".png", StringComparison.OrdinalIgnoreCase))
            {
                var image = (Bitmap)Bitmap.FromFile(args[0]);
                string text = "";
                try
                {
                    text = DecodeTextFromArgb(image);

                }
                catch (Exception ex)
                {
                    Console.WriteLine("PNG->TXT, 转换失败");
                    return;
                }
                var txtPath = Path.ChangeExtension(args[0], ".txt");
                File.WriteAllText(txtPath, text, Encoding.Unicode);
                Console.WriteLine("PNG->TXT, " + txtPath);
                try
                {
                    Process.Start(txtPath);
                }
                catch
                {
                }
            }
            else
            {
                Console.WriteLine("只支持TXT和PNG文件");
            }
        }

        static Bitmap ArgbTextEncode(string input)
        {
            var x = 0;
            var y = 0;
            var lenth = input.Length;
            var yValue = (int)(Math.Sqrt(input.Length) + 1);
            var fill = (yValue * yValue) % (Encoding.Unicode.GetByteCount(input) / 4);
            input += input.Substring(Rnd.random.Next(input.Length - fill - 1), fill);
            var image = new Bitmap(yValue, yValue);

            //fill
            for (int i = 0; i < yValue * yValue; i++)
            {
                if (i == lenth - 1)
                {
                    image.SetPixel(x, y, Color.White);
                }
                else
                {
                    byte[] bytes = Encoding.Unicode.GetBytes(input[i].ToString());
                    var prefix = 4 - bytes.Length;
                    bytes = Enumerable.Repeat<byte>(0, prefix).Concat(bytes).ToArray();
                    Encrypt(bytes);
                    image.SetPixel(x, y, Color.FromArgb(bytes[0], bytes[1], bytes[2], bytes[3]));
                }
                //Console.WriteLine("{0},{1} = {2}", x, y, input[i]);
                x++;
                if (x % yValue == 0)
                {
                    x = 0;
                    y++;
                }
            }
            return image;
        }

        static byte[] Encrypt(byte[] values)
        {
            for (int i = 0; i < values.Length; i++)
            {
                values[i] = (byte)(255 - values[i]);
            }
            return values;
        }
        static byte[] Decrypt(byte[] values)
        {
            for (int i = 0; i < values.Length; i++)
            {
                values[i] = (byte)(255 - values[i]);
            }
            return values;
        }

        static string DecodeTextFromArgb(Bitmap image)
        {
            //ComplexImage comp = ComplexImage.FromBitmap(image);
            //comp.BackwardFourierTransform();
            var str = "";
            for (int i = 0; i < image.Height; i++)
            {
                for (int j = 0; j < image.Width; j++)
                {
                    var color = image.GetPixel(j, i);
                    if (color.A == 255 && color.R == 255 && color.G == 255 && color.B == 255)
                        return str;
                    var bytes = new byte[] { color.A, color.R, color.G, color.B };
                    Decrypt(bytes);
                    int skip = 0;
                    if (bytes[0] == 0)
                    {
                        if (bytes[1] == 0)
                        {
                            skip = 2;
                        }
                        else
                        {
                            skip = 1;
                        }
                    }
                    else
                    {
                        skip = 0;
                    }
                    var t = Encoding.Unicode.GetString(bytes.Skip(skip).ToArray());
                    //Console.WriteLine("{0},{1} = {2}", j, i, t);
                    str += t;
                }
            }
            return str;
        }
    }

string key=”加密钥匙”;

  

public string Encrypt(string encryptString)

 源码地址:

{

byte[] keyBytes = Encoding.UTF8.GetBytes(key.Substring(0, 8));

byte[] keyIV = keyBytes;

 byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);

 DESCryptoServiceProvider provider = new DESCryptoServiceProvider();

MemoryStream mStream = new MemoryStream();

 CryptoStream cStream = new CryptoStream(mStream,
provider.CreateEncryptor (keyBytes,keyIV), CryptoStreamMode.Write);

cStream.Write(inputByteArray, 0, inputByteArray.Length);

cStream.FlushFinalBlock();

return Convert.ToBase64String(mStream.ToArray());

}

 解密:

 public string Decrypt(string decryptString)

 {

byte[] keyBytes = Encoding.UTF8.GetBytes(key.Substring(0, 8));

byte[] keyIV = keyBytes;

byte[] inputByteArray = Convert.FromBase64String(decryptString);

DESCryptoServiceProvider provider = new DESCryptoServiceProvider();

MemoryStream mStream = new MemoryStream();

 CryptoStream cStream = new CryptoStream(mStream,
provider.CreateDecryptor(keyBytes, keyIV), CryptoStreamMode.Write);

cStream.Write(inputByteArray, 0, inputByteArray.Length);

cStream.FlushFinalBlock();

return Encoding.UTF8.GetString(mStream.ToArray());

 }

网上的加密方法很多,最佳结合自身修改的,要不等种加密算法,外人一样也会知道…

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图
Copyright @ 2010-2019 美高梅手机版4858 版权所有