C# 返回空字符串的解密
我正在尝试使用C# 返回空字符串的解密,c#,php,encryption,openssl,C#,Php,Encryption,Openssl,我正在尝试使用openssl\u decrypt解密字符串。出于某种原因,无论我尝试了什么,它总是返回空的。下面是我正在使用的一个小函数非生产代码 PHP代码 public function Decrypt($string) { $password = "somepassword"; $method = "aes-256-cbc"; $hashPassword = hash('sha256', $password); $iv
openssl\u decrypt
解密字符串。出于某种原因,无论我尝试了什么,它总是返回空的。下面是我正在使用的一个小函数非生产代码
PHP代码
public function Decrypt($string)
{
$password = "somepassword";
$method = "aes-256-cbc";
$hashPassword = hash('sha256', $password);
$iv = chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0);
return openssl_decrypt(base64_decode($string), $method, $hashPassword, OPENSSL_RAW_DATA, $iv);
}
private static readonly byte[] FIELDS_KEY = SHA256.Create().ComputeHash(Encoding.ASCII.GetBytes("PASSWORDHERE"));
private static readonly byte[] FIELDS_IV = new byte[16] { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 };
public static string EncryptString(string plainText)
{
// Instantiate a new Aes object to perform string symmetric encryption
Aes encryptor = Aes.Create();
encryptor.Mode = CipherMode.CBC;
// Set key and IV
byte[] aesKey = new byte[32];
Array.Copy(FIELDS_KEY, 0, aesKey, 0, 32);
encryptor.Key = aesKey;
encryptor.IV = FIELDS_IV;
// Instantiate a new MemoryStream object to contain the encrypted bytes
MemoryStream memoryStream = new MemoryStream();
// Instantiate a new encryptor from our Aes object
ICryptoTransform aesEncryptor = encryptor.CreateEncryptor();
// Instantiate a new CryptoStream object to process the data and write it to the
// memory stream
CryptoStream cryptoStream = new CryptoStream(memoryStream, aesEncryptor, CryptoStreamMode.Write);
// Convert the plainText string into a byte array
byte[] plainBytes = Encoding.ASCII.GetBytes(plainText);
// Encrypt the input plaintext string
cryptoStream.Write(plainBytes, 0, plainBytes.Length);
// Complete the encryption process
cryptoStream.FlushFinalBlock();
// Convert the encrypted data from a MemoryStream to a byte array
byte[] cipherBytes = memoryStream.ToArray();
// Close both the MemoryStream and the CryptoStream
memoryStream.Close();
cryptoStream.Close();
// Convert the encrypted byte array to a base64 encoded string
string cipherText = Convert.ToBase64String(cipherBytes, 0, cipherBytes.Length);
// Return the encrypted data as a string
return cipherText;
}
另一方面,加密发生在c#
端,使用Xamarin
进行web调用。下面是我目前使用的加密函数
C#代码
public function Decrypt($string)
{
$password = "somepassword";
$method = "aes-256-cbc";
$hashPassword = hash('sha256', $password);
$iv = chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0);
return openssl_decrypt(base64_decode($string), $method, $hashPassword, OPENSSL_RAW_DATA, $iv);
}
private static readonly byte[] FIELDS_KEY = SHA256.Create().ComputeHash(Encoding.ASCII.GetBytes("PASSWORDHERE"));
private static readonly byte[] FIELDS_IV = new byte[16] { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 };
public static string EncryptString(string plainText)
{
// Instantiate a new Aes object to perform string symmetric encryption
Aes encryptor = Aes.Create();
encryptor.Mode = CipherMode.CBC;
// Set key and IV
byte[] aesKey = new byte[32];
Array.Copy(FIELDS_KEY, 0, aesKey, 0, 32);
encryptor.Key = aesKey;
encryptor.IV = FIELDS_IV;
// Instantiate a new MemoryStream object to contain the encrypted bytes
MemoryStream memoryStream = new MemoryStream();
// Instantiate a new encryptor from our Aes object
ICryptoTransform aesEncryptor = encryptor.CreateEncryptor();
// Instantiate a new CryptoStream object to process the data and write it to the
// memory stream
CryptoStream cryptoStream = new CryptoStream(memoryStream, aesEncryptor, CryptoStreamMode.Write);
// Convert the plainText string into a byte array
byte[] plainBytes = Encoding.ASCII.GetBytes(plainText);
// Encrypt the input plaintext string
cryptoStream.Write(plainBytes, 0, plainBytes.Length);
// Complete the encryption process
cryptoStream.FlushFinalBlock();
// Convert the encrypted data from a MemoryStream to a byte array
byte[] cipherBytes = memoryStream.ToArray();
// Close both the MemoryStream and the CryptoStream
memoryStream.Close();
cryptoStream.Close();
// Convert the encrypted byte array to a base64 encoded string
string cipherText = Convert.ToBase64String(cipherBytes, 0, cipherBytes.Length);
// Return the encrypted data as a string
return cipherText;
}
老实说,我真的不确定我在这里遗漏了什么,当我检查输入的密码、检查哈希密码和IV时,似乎都是从c#
函数的加密端进行检查。方法在当前PHP代码中以十六进制字符串的形式返回结果。但是,-方法要求密钥作为二进制数据,因此需要进行转换,例如使用:
$hashPassword = hex2bin(hash('sha256', $password));
或者,可以将-method的第三个参数(raw_output
)设置为TRUE
。这将直接以二进制数据的形式返回结果:
$hashPassword = hash('sha256', $password, $raw_output = true);
陛下为什么要使用哈希密码解密?加密没有使用散列密码,是吗?在当前PHP代码中,-方法以十六进制字符串的形式返回结果。由于需要二进制数据,因此必须先用
hex2bin
转换此字符串。或者,可以将散列
-方法中的第三个参数$raw\u output
设置为TRUE
,以便直接将结果作为二进制数据返回。无法再现问题。代码https://pastebin.com/JFCHqtLi
运行,例如在https://repl.it/languages/php_cli
Holy smokes@Topaco真管用<代码>$hashPassword=hash('sha256',$password,$raw\u output=true)是$raw\u output=true
完成的;我刚才说的是true
!非常感谢,如果你能给我一个答案,我很乐意接受,再次感谢!记住@Topaco,评论可能会消失,但答案永远不会消失。我们喜欢这里的答案,暗示,暗示;)