Advertisement
Guest User

Untitled

a guest
May 26th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.60 KB | None | 0 0
  1. const char key[] = {'x41', 'x41', 'x37', 'x44',
  2. 'x44', 'x34', 'x30', 'x33',
  3. 'x30', 'x35', 'x39', 'x4e',
  4. 'x36', 'x37', 'x30', 'x38'};
  5. AESstreamState encrypt_state; //built in Dynamic C type
  6. char init_vector[16];
  7. int i;
  8. int bufLength;
  9.  
  10. sprintf(Buf, "%s", "testabc");
  11. bufLength = strlen(Buf);
  12.  
  13. for (i = 0; i < 16; i++)
  14. init_vector[i] = rand() % 255;
  15.  
  16. printf("Key: ");
  17. for (i = 0; i < sizeof(key); i++)
  18. printf("%d ", key[i]);
  19. printf("n");
  20.  
  21. AESinitStream(&encrypt_state, key, init_vector); //built in Dynamic C function
  22. AESencryptStream(&encrypt_state, Buf, bufLength); //built in Dynamic C function
  23.  
  24. printf("Data: ");
  25. for (i = 0; i < strlen(Buf); i++)
  26. printf("%d ", Buf[i]);
  27. printf("n");
  28.  
  29. //set first byte to something that lets the server know it's encrypted
  30. //set 2nd through 16th byte to the IV (initialization vector) so every message will be different even if they have the same contents
  31. for (i = bufLength-1; i >= 0; i--)
  32. Buf[i+17] = Buf[i];
  33. Buf[0] = 237; //φ
  34.  
  35. printf("IV: ");
  36. for (i = 1; i < 17; i++)
  37. {
  38. printf("%d ", init_vector[i-1]);
  39. Buf[i] = init_vector[i-1];
  40. }
  41. printf("n");
  42.  
  43. private string DecryptAES(byte[] cipherText, byte[] IV)
  44. {
  45. byte[] key = {
  46. 0x41, 0x41, 0x37, 0x44,
  47. 0x44, 0x34, 0x30, 0x33,
  48. 0x30, 0x35, 0x39, 0x4e,
  49. 0x36, 0x37, 0x30, 0x38
  50. };
  51.  
  52. // Check arguments.
  53. if (cipherText == null || cipherText.Length <= 0)
  54. {
  55. throw new ArgumentNullException("cipherText");
  56. }
  57. else if (IV == null || IV.Length <= 0 || IV.Length != 16)
  58. {
  59. throw new ArgumentNullException("IV");
  60. }
  61.  
  62. Console.Write("Key: ");
  63. for (int i = 0; i < key.Length; i++)
  64. Console.Write("{0} ", key[i]);
  65. Console.WriteLine();
  66.  
  67. Console.Write("Data: ");
  68. for (int i = 0; i < cipherText.Length; i++)
  69. Console.Write("{0} ", cipherText[i]);
  70. Console.WriteLine();
  71.  
  72. Console.Write("IV: ");
  73. for (int i = 0; i < IV.Length; i++)
  74. Console.Write("{0} ", IV[i]);
  75. Console.WriteLine();
  76.  
  77. // Create an RijndaelManaged object
  78. // with the specified key and IV.
  79. using (RijndaelManaged rijAlg = new RijndaelManaged())
  80. {
  81. rijAlg.Mode = CipherMode.CFB;
  82. rijAlg.FeedbackSize = 8;
  83. rijAlg.BlockSize = 128;
  84. rijAlg.Padding = PaddingMode.None;
  85.  
  86. rijAlg.Key = key;
  87. rijAlg.IV = IV;
  88.  
  89. // Create a decrytor to perform the stream transform.
  90. ICryptoTransform decryptor = rijAlg.CreateDecryptor(rijAlg.Key, rijAlg.IV);
  91.  
  92. // Create the streams used for decryption.
  93. using (System.IO.MemoryStream msDecrypt = new System.IO.MemoryStream(cipherText))
  94. {
  95. using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
  96. {
  97. using (System.IO.StreamReader srDecrypt = new System.IO.StreamReader(csDecrypt))
  98. {
  99. // Read the decrypted bytes from the decrypting stream
  100. // and place them in a string.
  101. string plaintext = null;
  102.  
  103. plaintext = srDecrypt.ReadToEnd();
  104.  
  105. Console.WriteLine("Decrypted: " + plaintext);
  106.  
  107. return plaintext;
  108. }
  109. }
  110.  
  111. }
  112. }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement