c# des encryption and decryption, use unity www to download text and parse

    Use Unity's www to download the text and decrypt the pit encountered and record it.

    The first is the encryption and decryption of c#des. There are a lot of them on the Internet. The blogger just pastes the code.

    Define two keys, note that the length of the key string must be 8, otherwise it will be abnormal.

    

static string encryptKey = "qweasdqw";
        static string ivKey = "dseaseas";
 //encryption
        static byte[] EncryptBytes(string str)
        {
            string result = string.Empty;

            DESCryptoServiceProvider descsp = new DESCryptoServiceProvider();

            descsp.Mode = CipherMode.CBC;

            byte[] key = Encoding.UTF8.GetBytes(encryptKey);
            byte[] data = Encoding.UTF8.GetBytes(str);

            byte[] iv = Encoding.UTF8.GetBytes(ivKey);

            MemoryStream MStream = new MemoryStream();

            CryptoStream CStream = new CryptoStream(MStream, descsp.CreateEncryptor(key, iv), CryptoStreamMode.Write);

            CStream.Write(data, 0, data.Length);

            CStream.FlushFinalBlock();

            Console.WriteLine("Byte length after encryption: "+ MStream.ToArray().Length);

            return MStream.ToArray();
        }

        // decrypt
        static byte[] DecryptBytes(byte[] data)
        {

            Console.WriteLine("byte length when decrypting: "+data.Length);

            string result = string.Empty;

            DESCryptoServiceProvider descsp = new DESCryptoServiceProvider();

            descsp.Mode = CipherMode.CBC;

            byte[] key = Encoding.UTF8.GetBytes(encryptKey);

            byte[] iv = Encoding.UTF8.GetBytes(ivKey);

            MemoryStream MStream = new MemoryStream();

            CryptoStream CStream = new CryptoStream(MStream, descsp.CreateDecryptor(key, iv), CryptoStreamMode.Write);

            CStream.Write(data, 0, data.Length);

            CStream.FlushFinalBlock();
             return MStream.ToArray();
        }

     Then use the file stream to read the content of the original file, write the byte array into the file after encryption, and put it on the file server for download. It feels like a lot of holes in here.

     First of all, the original file is the content before encryption, that is, a string. I use StreamReader to read this string. One is because I only need to read the string, and the other is because when I read it with BinaryReader, it is always abnormal, which is very helpless. . The reading of the string is only a few lines of code, which is simple.

            StreamReader sr = new StreamReader(@"C:\MyDataFile\Json.txt");
            string line = sr.ReadToEnd();
            Console.WriteLine("data:\n" + line);
            sr.Close();

    The next step is to encrypt the string. After encryption, pay attention to directly write the encrypted result and the byte array into the file, do not convert it into a string and write it into the file. There will definitely be problems when you turn it around, and you have a deep understanding. The code is as follows, note that the byte stream must be written here, the BinaryWriter class

 
 
BinaryWriter bw = new BinaryWriter(new FileStream(@"C:\Share\Data\afterkey.txt", FileMode.Create));


            byte[] value = EncryptBytes(line);
            try
            {
                bw.Write(value.Length);
                bw.Write(value);
            }
            catch (IOException e)
            {
                Console.WriteLine(e.Message);
            }

     After writing to the file, start reading and decrypting the file. This is also relatively simple. Just read bytes directly and decrypt the returned byte array. And also use byte stream reading, code

BinaryReader br = new BinaryReader(new FileStream(@"C:\Share\Data\afterkey.txt", FileMode.Open));

            int len = br.ReadInt32();
            byte [] cache = br.ReadBytes (len);
            Console.WriteLine(cache.Length);

            Console.WriteLine("data:\n" + Encoding.UTF8.GetString( DecryptBytes(cache)));

            br.Close();

     This is to simulate local encrypted writing, then read and decrypt, and put the process of reading and decryption in unity. There are not many changes, but it is also very pitted. First, the process of encrypting and writing is operated locally, and then the encrypted and written file is placed on the file server for download, and then the byte stream of the file is accessed using Unity's www, and the byte stream is directly decrypted. The code is as follows:

IEnumerator DownLoadData()
    {
        Debug.Log("down load data");

        Debug.Log(url_02);
        WWW w = new WWW(url_02);
        yield return w;

        if (!string.IsNullOrEmpty(w.error))
        {
            Debug.LogError(w.error);
        }

        if (w.isDone)
        {
            Debug.Log(w.text);  
        }
        Debug.Log(w.bytes.Length);

        Debug.Log(Encoding.UTF8.GetString( DesDoString.DecryptBytes(w.bytes)));


    }

    Here desdostring. DecryptBytes is exactly the same as the above method. Here we must pay attention to the length of www.bytes. The length of this bytes = the length of the content of the declared byte length + the length of the content in bytes. Take the above code as an example, if you use BinaryWriter to write the byte length first, and then write the byte content. In this case, the byte length you get by using www is the length of the byte content + the length of the declared byte length content, that is to say, 400 is added after the length of your text byte content, but in fact you log the length of www.bytes In fact, it is 404, and there are 4 more lengths, which will cause you to decrypt abnormally. Therefore, when writing bytes in the front, be sure not to write the byte length, that is, the sentence bw.Write(value.Length) should be commented out, so that the length of www.bytes is the same as the length of the text bytes.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324495739&siteId=291194637