栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

简单的不安全双向数据“混淆”?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

简单的不安全双向数据“混淆”?

此处的其他答案很好用,但是AES是一种更安全和最新的加密算法。这是我几年前获得的用于执行AES加密的类,随着时间的推移,我对它进行了修改以使其对Web应用程序更加友好(例如,我已经构建了可与URL友好的字符串一起使用的Encrypt
/ Decrypt方法)。它还具有使用字节数组的方法。

注意:您应该在Key(32个字节)和Vector(16个字节)数组中使用不同的值!您不希望有人仅仅假设您按原样使用此代码来弄清楚您的密钥!您所要做的就是更改Key和Vector数组中的一些数字(必须为<=
255)(我在Vector数组中留下了一个无效值,以确保您执行此操作…)。您可以使用https://www.random.org/bytes/轻松生成一个新集合:

  • 生成
    Key
  • 生成
    Vector

使用它很容易:只需实例化该类,然后(通常)将EncryptToString(string
StringToEncrypt)和DecryptString(string
StringToDecrypt)调用为方法。设置好此类后,再简单不过了(或更安全)。


using System;using System.Data;using System.Security.Cryptography;using System.IO;public class SimpleAES{    // Change these keys    private byte[] Key = __Replace_Me__({ 123, 217, 19, 11, 24, 26, 85, 45, 114, 184, 27, 162, 37, 112, 222, 209, 241, 24, 175, 144, 173, 53, 196, 29, 24, 26, 17, 218, 131, 236, 53, 209 });    // a hardpred IV should not be used for production AES-CBC pre    // IVs should be unpredictable per ciphertext    private byte[] Vector = __Replace_Me__({ 146, 64, 191, 111, 23, 3, 113, 119, 231, 121, 2521, 112, 79, 32, 114, 156 });    private ICryptoTransform EncryptorTransform, DecryptorTransform;    private System.Text.UTF8Encoding UTFEnprer;    public SimpleAES()    {        //This is our encryption method        RijndaelManaged rm = new RijndaelManaged();        //Create an encryptor and a decryptor using our encryption method, key, and vector.        EncryptorTransform = rm.CreateEncryptor(this.Key, this.Vector);        DecryptorTransform = rm.CreateDecryptor(this.Key, this.Vector);        //Used to translate bytes to text and vice versa        UTFEnprer = new System.Text.UTF8Encoding();    }    /// -------------- Two Utility Methods (not used but may be useful) -----------    /// Generates an encryption key.    static public byte[] GenerateEncryptionKey()    {        //Generate a Key.        RijndaelManaged rm = new RijndaelManaged();        rm.GenerateKey();        return rm.Key;    }    /// Generates a unique encryption vector    static public byte[] GenerateEncryptionVector()    {        //Generate a Vector        RijndaelManaged rm = new RijndaelManaged();        rm.GenerateIV();        return rm.IV;    }    /// ----------- The commonly used methods ------------------------------        /// Encrypt some text and return a string suitable for passing in a URL.    public string EncryptToString(string TextValue)    {        return ByteArrToString(Encrypt(TextValue));    }    /// Encrypt some text and return an encrypted byte array.    public byte[] Encrypt(string TextValue)    {        //Translates our text value into a byte array.        Byte[] bytes = UTFEnprer.GetBytes(TextValue);        //Used to stream the data in and out of the CryptoStream.        MemoryStream memoryStream = new MemoryStream();                #region Write the decrypted value to the encryption stream        CryptoStream cs = new CryptoStream(memoryStream, EncryptorTransform, CryptoStreamMode.Write);        cs.Write(bytes, 0, bytes.Length);        cs.FlushFinalBlock();        #endregion        #region Read encrypted value back out of the stream        memoryStream.Position = 0;        byte[] encrypted = new byte[memoryStream.Length];        memoryStream.Read(encrypted, 0, encrypted.Length);        #endregion        //Clean up.        cs.Close();        memoryStream.Close();        return encrypted;    }    /// The other side: Decryption methods    public string DecryptString(string EncryptedString)    {        return Decrypt(StrToByteArray(EncryptedString));    }    /// Decryption when working with byte arrays.        public string Decrypt(byte[] EncryptedValue)    {        #region Write the encrypted value to the decryption stream        MemoryStream encryptedStream = new MemoryStream();        CryptoStream decryptStream = new CryptoStream(encryptedStream, DecryptorTransform, CryptoStreamMode.Write);        decryptStream.Write(EncryptedValue, 0, EncryptedValue.Length);        decryptStream.FlushFinalBlock();        #endregion        #region Read the decrypted value from the stream.        encryptedStream.Position = 0;        Byte[] decryptedBytes = new Byte[encryptedStream.Length];        encryptedStream.Read(decryptedBytes, 0, decryptedBytes.Length);        encryptedStream.Close();        #endregion        return UTFEnprer.GetString(decryptedBytes);    }    /// Convert a string to a byte array.  NOTE: Normally we'd create a Byte Array from a string using an ASCII encoding (like so).    //      System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();    //      return encoding.GetBytes(str);    // However, this results in character values that cannot be passed in a URL.  So, instead, I just    // lay out all of the byte values in a long string of numbers (three per - must pad numbers less than 100).    public byte[] StrToByteArray(string str)    {        if (str.Length == 0) throw new Exception("Invalid string value in StrToByteArray");        byte val;        byte[] byteArr = new byte[str.Length / 3];        int i = 0;        int j = 0;        do        { val = byte.Parse(str.Substring(i, 3)); byteArr[j++] = val; i += 3;        }        while (i < str.Length);        return byteArr;    }    // Same comment as above.  Normally the conversion would use an ASCII encoding in the other direction:    //      System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();    //      return enc.GetString(byteArr);        public string ByteArrToString(byte[] byteArr)    {        byte val;        string tempStr = "";        for (int i = 0; i <= byteArr.GetUpperBound(0); i++)        { val = byteArr[i]; if (val < (byte)10)     tempStr += "00" + val.ToString(); else if (val < (byte)100)     tempStr += "0" + val.ToString(); else     tempStr += val.ToString();        }        return tempStr;    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/411955.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号