English 中文(简体)
两种方法 AES 加密
原标题:Two Way AES Encryption

I m使用我在这里发现的用于AES加密/加密的一类。 它对我来说的确是正确的,但在我使用加密方法加密时,加密只包含数字。 我期望它包含编号、信件和符号。 你们是否知道为什么只包含数量? 增 编

这里指的是:

 public class AESEncryption
{
    // These can be anything I desire but must be less than or equal to 255
    private byte[] Key = { 222, 237, 16, 14, 28, 26, 85, 45, 114, 184, 27, 192, 37, 112, 222, 209, 241, 24, 175, 144, 173, 53, 105, 29, 24, 26, 17, 218, 131, 236, 53, 209 };
    private byte[] Vector = { 146, 64, 101, 111, 23, 32, 113, 119, 231, 121, 211, 11, 99, 32, 104, 156 };


    private ICryptoTransform EncryptorTransform, DecryptorTransform;
    private System.Text.UTF8Encoding UTFEncoder;

    public AESEncryption()
    {
        //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 
        UTFEncoder = 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 = UTFEncoder.GetBytes(TextValue);

        //Used to stream the data in and out of the CryptoStream. 
        MemoryStream memoryStream = new MemoryStream();

        /* 
         * We will have to write the unencrypted bytes to the stream, 
         * then read the encrypted result back from the stream. 
         */
        #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 UTFEncoder.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;
    }
}
问题回答

它把tes作为惯性价值(3位数)加以编码,并将 t作为扼杀,反之亦然。

从法典和评论中可以看出:

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

AND the inverse method

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

“AES”算法采用并回归一个星体(byte[]),即一套8个轨道编号。

这是贵国计算机内的所有数据,不论数据类型如何,都储存在<代码>string、Imageint<或任何其他方面。 如你所知,这行文如下:

//Translates our text value into a byte array.
Byte[] bytes = UTFEncoder.GetBytes(TextValue);

converts your clear string into a clear byte array, then runs it through the encryption algoritm. After encryption the byte array can no longer be decoded into a string (which is the point of encryption).

如果你想把它储存起来,你可以把64条放在密码上,这样它就好了,但无论从哪一种方式来看,都不会被人read。

如果您使用<代码>File.WriteAllBytes功能,其输出来源为Encode

File.WriteAllBytes("C:MyTestFile.txt", Encrypt("My Clear Text"));

It will store the bytes into a text file. If you then open "C:MyTestFile.txt" with a text editor of your choice, you will see that the file does in fact contain all manner of charachters, both printing and non printing.

假定你使用正确的<代码>key和vector,则加密法将tes改成明灯,因为你可以看到

return UTFEncoder.GetString(decryptedBytes);

这些人回到你原来的身边。

如果说这没有任何意义,那么,那么,我就试图保持简单,也许会退一步,考虑什么是<条码>逐条/代码”,那么工作类型如何。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签