GetUniqueString() публичный статический Метод

Based on http://stackoverflow.com/a/1344255 Generate a unique string given number of bytes required. This string can be used as IV. IV byte size should be equal to cipher-block byte size. Allows seeing IV in plaintext so it can be passed along a url or some message.
public static GetUniqueString ( int numBytes ) : string
numBytes int
Результат string
Пример #1
0
    static void Main(string[] args)
    {
        Program p = new Program();

        //get 16 byte key (just demo - typically you will have a predetermined key)
        p.key = AnotherAES.GetUniqueString(16);
        string plainText = "Hello World!";

        //encrypt
        string hex = p.Encrypt(plainText);
        //decrypt
        string roundTrip = p.Decrypt(hex);

        Console.WriteLine("Round Trip: {0}", roundTrip);
    }
Пример #2
0
    string Encrypt(string plainText)
    {
        Console.WriteLine("\nSending (encrypt side)...");
        Console.WriteLine("Plain Text: {0}", plainText);
        Console.WriteLine("Key: {0}", key);
        string hex      = string.Empty;
        string ivString = AnotherAES.GetUniqueString(16);

        Console.WriteLine("IV: {0}", ivString);
        using (AnotherAES aes = new AnotherAES(key))
        {
            //encrypting side
            byte[] IV = Encoding.UTF8.GetBytes(ivString);
            //get encrypted bytes (IV bytes prepended to cipher bytes)
            byte[] encryptedBytes       = aes.Encrypt(plainText, IV);
            byte[] encryptedBytesWithIV = IV.Concat(encryptedBytes).ToArray();
            //get hex string to send with url
            //this hex has both IV and ciphertext
            hex = BitConverter.ToString(encryptedBytesWithIV).Replace("-", "");
            Console.WriteLine("sending hex: {0}", hex);
        }
        return(hex);
    }