Guest User

Untitled

a guest
Jun 18th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. public static string DecryptString(string data, string password)
  2. {
  3.  
  4. byte[] allBytes = ToByteArray(data);
  5. byte[] one = ToByteArray("1");
  6. string plaintext = null;
  7. // this is all of the bytes
  8.  
  9. byte[] passwordByteArray = ToByteArray(password);
  10.  
  11. using (var aes = Aes.Create())
  12. {
  13. aes.KeySize = KeySize;
  14. aes.BlockSize = BlockSize;
  15. aes.Mode = CipherMode.CBC;
  16.  
  17. // get the key salt
  18. byte[] keySalt = new byte[KeySize / 8];
  19. Array.Copy(allBytes, keySalt, keySalt.Length);
  20.  
  21. // Yii2 says
  22. //$key = $this->hkdf($this->kdfHash, $secret, $keySalt, $info, $keySize);
  23. //
  24. //Yii2 hkdf says
  25. //$prKey = hash_hmac($algo, $inputKey, $salt, true);
  26. //$hmac = '';
  27. //$outputKey = '';
  28. //for ($i = 1; $i <= $blocks; $i++) {
  29. // $hmac = hash_hmac($algo, $hmac . $info . chr($i), $prKey, true);
  30. // $outputKey .= $hmac;
  31. //}
  32. // chr($i) is the char byte of 1;
  33. // the blocksize is 1
  34. // info here is nothing
  35.  
  36. // hash first key with keysalt and password
  37. HMACSHA256 hmac = new HMACSHA256(keySalt);
  38. byte[] computedHash = hmac.ComputeHash(passwordByteArray);
  39.  
  40. // hash primary key with one byte and computed hash
  41. HMACSHA256 hmac2 = new HMACSHA256(one);
  42. byte[] prKey = hmac2.ComputeHash(computedHash);
  43.  
  44. // if we want to verify the mac hash this is where we would do it.
  45. // Yii2 encryption data.
  46. // $encrypted = openssl_encrypt($data, $this->cipher, $key, OPENSSL_RAW_DATA, $iv);
  47. //
  48. //$authKey = $this->hkdf($this->kdfHash, $key, null, $this->authKeyInfo, $keySize);
  49. //hashed = $this->hashData($iv. $encrypted, $authKey);
  50. //hashed = [macHash][data]
  51.  
  52. // get the MAC code
  53. byte[] MAC = new byte[MacHashSize / 8];
  54. Array.Copy(allBytes, keySalt.Length, MAC, 0, MAC.Length);
  55.  
  56. // get our IV
  57. byte[] iv = new byte[BlockSize / 8];
  58. Array.Copy(allBytes, (keySalt.Length + MAC.Length), iv, 0, iv.Length);
  59.  
  60. // get the data we need to decrypt
  61. byte[] cipherBytes = new byte[allBytes.Length - iv.Length - MAC.Length - keySalt.Length];
  62. Array.Copy(allBytes, (keySalt.Length + MAC.Length + iv.Length), cipherBytes, 0, cipherBytes.Length);
  63.  
  64. // Create a decrytor to perform the stream transform.
  65. var decryptor = aes.CreateDecryptor(prKey, iv);
  66.  
  67. // Create the streams used for decryption.
  68. using (MemoryStream msDecrypt = new MemoryStream(cipherBytes))
  69. {
  70. using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
  71. {
  72. using (StreamReader srDecrypt = new StreamReader(csDecrypt))
  73. {
  74. //Read the decrypted bytes from the decrypting stream
  75. //and place them in a string.
  76. plaintext = srDecrypt.ReadToEnd();
  77. }
  78. }
  79. }
  80. }
  81.  
  82. return plaintext;
  83. }
  84.  
  85. public static byte[] ToByteArray(string value)
  86. {
  87. byte[] allBytes = new byte[value.Length];
  88. int i = 0;
  89. foreach (byte bite in value)
  90. {
  91. allBytes[i] = Convert.ToByte(bite);
  92. i++;
  93. }
  94.  
  95. return allBytes;
  96. }
Add Comment
Please, Sign In to add comment