如何将php AES加密代码转换为c#
我正在通过调用Api创建一个C#MVC应用程序,它的令牌加密方法是PHP,我想把它转换成C#代码。有人能帮我把下面的PHP代码转换成C#吗 PHP代码如何将php AES加密代码转换为c#,c#,php,encryption,aes,sha256,C#,Php,Encryption,Aes,Sha256,我正在通过调用Api创建一个C#MVC应用程序,它的令牌加密方法是PHP,我想把它转换成C#代码。有人能帮我把下面的PHP代码转换成C#吗 PHP代码 <?php //ini_set("display_errors", 1); //ini_set("display_startup_errors", 1); function encryptToken($token) { $cipher_method = 'aes-128-ctr'; $enc_key = openss
<?php
//ini_set("display_errors", 1);
//ini_set("display_startup_errors", 1);
function encryptToken($token)
{
$cipher_method = 'aes-128-ctr';
$enc_key = openssl_digest
('*****************************', 'SHA256', TRUE);
$enc_iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length($cipher_method));
$crypted_token = openssl_encrypt($token, $cipher_method, $enc_key, 0, $enc_iv) . "::" . bin2hex($enc_iv);
unset($token, $cipher_method, $enc_key, $enc_iv);
return $crypted_token;
}
function createAccessToken(){
//$date = new DateTime("now", new DateTimeZone('Asia/Kolkata') );
//$now = $date('YmdHis');
$now = date("YmdHis");
$secret ='********************************';
$plainText = $now."::".$secret;
$encrypted = encryptToken($plainText);
return $encrypted;
}
$value="";
if($_POST){
if(isset($_POST['test'])){
$value= createAccessToken();
}
}
?>
到目前为止,您对C#做了哪些尝试?我在上面添加了C#代码。但是生成的令牌没有在API中验证。但使用PHP代码生成的令牌正在API中验证。我遗漏了什么吗?我知道C#甚至不包括CTR模式是非常令人恼火的,但只是尝试ECB然后抱怨它不起作用对我来说是不合适的。尝试BouncyCastle(请注意,其中可能称为SIC模式(分段整数计数器)。一如既往,请尝试将IV设置为与ECB模式兼容的值。为了解决此问题,几年前我添加了for循环,并尝试了各种属性组合(模式、填充、块大小)要找到答案。在这种情况下,我认为这是填充模式。您使用的是什么版本的Net?人们在Net 4.7中遇到了加密问题。我认为这是一个32/64的问题。不要相信PHP注释。它们可能不正确。如果您使用Fiddler is,它将尝试各种选项组合,并使用wo选项为您提供输出rked。所以试着用fiddler捕捉php输出。@jdweng是php代码中使用的CTR模式特有的东西,还是跳过了它?因为CTR甚至不使用填充。
public string GenerateToken()
{
var Date = DateTime.Now.ToString("yyyyMMddHHmmss");
var secret = "#############################";
string plainText = Date + "::" + secret;
var accessToken = EncryptString(plainText);
return accessToken;
}
public string EncryptString(string plainText)
{
try
{
string password = "************************";
// Create sha256 hash
SHA256 mySHA256 = SHA256Managed.Create();
byte[] key = mySHA256.ComputeHash(Encoding.ASCII.GetBytes(password));
// Instantiate a new Aes object to perform string symmetric encryption
Aes encryptor = Aes.Create();
encryptor.Mode = CipherMode.ECB;
encryptor.Padding = PaddingMode.None;
encryptor.BlockSize = 128;
// Create secret IV
var iv = generateIV();
// Set key and IV
byte[] aesKey = new byte[32];
Array.Copy(key, 0, aesKey, 0, 32);
encryptor.Key = aesKey;
encryptor.IV = 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) + "::" + ByteArrayToString(iv);
// Return the encrypted data as a string
return cipherText;
}
catch (Exception)
{
throw;
}
}
private static byte[] generateIV()
{
using (RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider())
{
byte[] nonce = new byte[IV_LENGTH];
rng.GetBytes(nonce);
return nonce;
}
}