Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const char key[] = {'x41', 'x41', 'x37', 'x44',
- 'x44', 'x34', 'x30', 'x33',
- 'x30', 'x35', 'x39', 'x4e',
- 'x36', 'x37', 'x30', 'x38'};
- AESstreamState encrypt_state; //built in Dynamic C type
- char init_vector[16];
- int i;
- int bufLength;
- sprintf(Buf, "%s", "testabc");
- bufLength = strlen(Buf);
- for (i = 0; i < 16; i++)
- init_vector[i] = rand() % 255;
- printf("Key: ");
- for (i = 0; i < sizeof(key); i++)
- printf("%d ", key[i]);
- printf("n");
- AESinitStream(&encrypt_state, key, init_vector); //built in Dynamic C function
- AESencryptStream(&encrypt_state, Buf, bufLength); //built in Dynamic C function
- printf("Data: ");
- for (i = 0; i < strlen(Buf); i++)
- printf("%d ", Buf[i]);
- printf("n");
- //set first byte to something that lets the server know it's encrypted
- //set 2nd through 16th byte to the IV (initialization vector) so every message will be different even if they have the same contents
- for (i = bufLength-1; i >= 0; i--)
- Buf[i+17] = Buf[i];
- Buf[0] = 237; //φ
- printf("IV: ");
- for (i = 1; i < 17; i++)
- {
- printf("%d ", init_vector[i-1]);
- Buf[i] = init_vector[i-1];
- }
- printf("n");
- private string DecryptAES(byte[] cipherText, byte[] IV)
- {
- byte[] key = {
- 0x41, 0x41, 0x37, 0x44,
- 0x44, 0x34, 0x30, 0x33,
- 0x30, 0x35, 0x39, 0x4e,
- 0x36, 0x37, 0x30, 0x38
- };
- // Check arguments.
- if (cipherText == null || cipherText.Length <= 0)
- {
- throw new ArgumentNullException("cipherText");
- }
- else if (IV == null || IV.Length <= 0 || IV.Length != 16)
- {
- throw new ArgumentNullException("IV");
- }
- Console.Write("Key: ");
- for (int i = 0; i < key.Length; i++)
- Console.Write("{0} ", key[i]);
- Console.WriteLine();
- Console.Write("Data: ");
- for (int i = 0; i < cipherText.Length; i++)
- Console.Write("{0} ", cipherText[i]);
- Console.WriteLine();
- Console.Write("IV: ");
- for (int i = 0; i < IV.Length; i++)
- Console.Write("{0} ", IV[i]);
- Console.WriteLine();
- // Create an RijndaelManaged object
- // with the specified key and IV.
- using (RijndaelManaged rijAlg = new RijndaelManaged())
- {
- rijAlg.Mode = CipherMode.CFB;
- rijAlg.FeedbackSize = 8;
- rijAlg.BlockSize = 128;
- rijAlg.Padding = PaddingMode.None;
- rijAlg.Key = key;
- rijAlg.IV = IV;
- // Create a decrytor to perform the stream transform.
- ICryptoTransform decryptor = rijAlg.CreateDecryptor(rijAlg.Key, rijAlg.IV);
- // Create the streams used for decryption.
- using (System.IO.MemoryStream msDecrypt = new System.IO.MemoryStream(cipherText))
- {
- using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
- {
- using (System.IO.StreamReader srDecrypt = new System.IO.StreamReader(csDecrypt))
- {
- // Read the decrypted bytes from the decrypting stream
- // and place them in a string.
- string plaintext = null;
- plaintext = srDecrypt.ReadToEnd();
- Console.WriteLine("Decrypted: " + plaintext);
- return plaintext;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement