加密解密,大器晚成种将文件编码为图片格式的

世界上的文字能够被八个字节完全覆盖,也等于UTF-32,别的都以变长的格式。而刚好A福特ExplorerGB加起来八个字节,于是完全能够把叁个字符映射为三个像素点嘛!

揽胜极光SA解密极度,求大神教导
public string Encryption(string strKey) { CspParameters param = new CspParameters(); param.KeyContainerName = "Rich_CRM";//密匙容器的称号,保持加密解密生龙活虎致技艺解密成功 using (奥迪Q7SACryptoServiceProvider rsa = new 牧马人SACryptoServiceProvider { byte[] plaindata = Encoding.Default.GetBytes;//就要加密的字符串调换为字节数组 byte[] encryptdata = rsa.Encrypt(plaindata, false);//将加密后的字节数据调换为新的加密字节数组 return Convert.ToBase64String(encryptdata);//将加密后的字节数组调换为字符串 } } //解密 public string Decrypt(string ciphertext) { CspParameters param = new CspParameters(); param.KeyContainerName = "Rich_CRM"; using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider { byte[] encryptdata = Convert.FromBase64String(ciphertext); byte[] decryptdata = rsa.Decrypt(encryptdata, true); return Encoding.Default.GetString(decryptdata); } } 同样的代码,用winform测量检验都以足以的,不过写webservice的时候,解密试行到byte[] decryptdata = rsa.Decrypt(encryptdata, true);会有标题,未有报错,直接跳出来了,那是为啥?求大神辅导

.NET Core上边的DES等加密算法要等到1.2 才支撑,大家只是要求这些算法的支撑,小说《选择 JavaScriptService 在.NET Core 里完成DES加密算法》需求用Nodejs,很四人感觉那么些有一点不佳,后天就给我们介绍下BouncyCastle (Portable.BouncyCastle卡塔尔国库为大家提供的原生的.NET Core的扶助库的Des算法。BouncyCastle的文档超级少,折腾了遥远才写出了.NET 代码等价的八个封装。

恰好学会的C#的加密与解密MD5加密/君越SA加密与解密/DES加密。也是刚刚申请的blog随意公布一下。

再者图片上的字节能够重复加密,非常风趣!笔者仅演示的顶尖无敌轻易的取反操作。

  public class TDesbouncy
    {

、MD5加密

 

        IBlockCipher engine = new DesEngine();

MD5 md5 = new MD5CryptoServiceProvider();
byte[] palindata = Encoding.Default.GetBytes(textBox1.Text);//将在加密的字符串转变为字节数组
byte[] encryptdata = md5.ComputeHash(palindata);//将字符串加密后也转移为字符数组
text博克斯2.Text = Convert.ToBase64String(encryptdata);//将加密后的字节数组调换为加密字符串

小编要好习贯看方形的图样,于是开平方取整,借使字符数量不足就从字符本人随机取意气风发段,那是为着图片尾巴部分不会多少个像素留白,炒鸡倒霉看,以深暗黄(0xFFFFFFFF)作为终止字符。

        /// <summary>
        /// 使用DES加密,key输入密码的时候,必须选用葡萄牙共和国语字符,区分轻重缓急写,且字符数量是8个,无法多也不可能少
        /// </summary>
        /// <param name="plainText">要求加密的字符串</param>
        /// <param name="keys">加密字符串的密钥</param>
        /// <returns>加密后的字符串</returns>
        public string Encrypt(string keys, string plainText)
        {

、RAS加密

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);
            StringBuilder ret = new StringBuilder();
            foreach (byte b in rv)
            {
                ret.AppendFormat("{0:X2}", b);
            }
            return ret.ToString();
        }

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

  

        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加密:

找了一个日记文本,看看效果

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

param = new CspParameters();
param.KeyContainerName = "Olive";//密匙容器的名目,保持加密解密风流洒脱致工夫解密成功
using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider
{
byte[] plaindata = Encoding.Default.GetBytes(text博克斯1.Text);//将在加密的字符串转变为字节数组
byte[] encryptdata = rsa.Encrypt(plaindata, false);//将加密后的字节数据调换为新的加密字节数组
textBox2.Text = Convert.ToBase64String(encryptdata);//将加密后的字节数组调换为字符串
}

图片 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解密:

 

            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);
}

 

        }

DES加密:

解码函数

        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);

第一申明全局变量

 

            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解密:

  

    }

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);

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

此中用到的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();

  

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

Encrypt和Decrypt,其实肖似的。。哈哈

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

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;
        }

  

本程序完整代码如下:

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;
        }
    }

  

 源码地址:

本文由星彩网app下载发布于计算机编程,转载请注明出处:加密解密,大器晚成种将文件编码为图片格式的

TAG标签: 星彩网app下载
Ctrl+D 将本页面保存为书签,全面了解最新资讯,方便快捷。