0

任何人都可以为以下 C# Aes 加密提供等效的 PHP 代码,我尝试了不同的 php AES 加密,但没有用,输出不匹配,在此先感谢。

这里使用的 key 和 iv 值是十六进制的,预期的输出是十六进制的。

C#:

public class AESAlgorithm
    {

    public static string GeneratePrivateKey()
    {
        string str;

        RijndaelManaged rijndaelManaged = null;
        try
        {
            rijndaelManaged = new RijndaelManaged();
            rijndaelManaged.KeySize = 128;
            rijndaelManaged.GenerateKey();
            str = ConvertByteArrayToHexString(rijndaelManaged.Key);
        }
        finally
        {
            if (rijndaelManaged == null)
            {
                rijndaelManaged.Clear();
            }
        }
        return str;
    }

    public static string GenerateAuthKey(string timestamp, string appCode, string attUid, string privateKey)
    {
        return Encrypt(String.Format("{0}|{1}|{2}", timestamp, appCode, attUid), privateKey);
    }

    public static string Encrypt(string textToEncrypt, string hexStringKey)
    {
        string str1;

        RijndaelManaged rijndaelManaged = null;
        try
        {
            rijndaelManaged = new RijndaelManaged();
            rijndaelManaged.Mode = CipherMode.CBC;
            rijndaelManaged.Padding = PaddingMode.PKCS7;
            rijndaelManaged.KeySize = 128;
            rijndaelManaged.BlockSize = 128;
            byte[] bs1 = ConvertHexStringToByteArray(hexStringKey);
            rijndaelManaged.Key = bs1;
            rijndaelManaged.IV = bs1;
            ICryptoTransform iCryptoTransform = rijndaelManaged.CreateEncryptor();
            byte[] bs2 = Encoding.UTF8.GetBytes(textToEncrypt);
            str1 = ConvertByteArrayToHexString(iCryptoTransform.TransformFinalBlock(bs2, 0, (int)bs2.Length));
        }
        finally
        {
            if (rijndaelManaged == null)
            {
                rijndaelManaged.Clear();
            }
        }
        return str1;
    }

    public static string Decrypt(string hexStringToDecrypt, string hexStringKey)
    {
        string str1;

        RijndaelManaged rijndaelManaged = null;
        try
        {
            rijndaelManaged = new RijndaelManaged();
            rijndaelManaged.Mode = CipherMode.CBC;
            rijndaelManaged.Padding = PaddingMode.PKCS7;
            rijndaelManaged.KeySize = 128;
            rijndaelManaged.BlockSize = 128;
            byte[] bs1 = ConvertHexStringToByteArray(hexStringToDecrypt);
            byte[] bs2 = ConvertHexStringToByteArray(hexStringKey);
            rijndaelManaged.Key = bs2;
            rijndaelManaged.IV = bs2;
            byte[] bs3 = rijndaelManaged.CreateDecryptor().TransformFinalBlock(bs1, 0, (int)bs1.Length);
            str1 = Encoding.UTF8.GetString(bs3);
        }
        finally
        {
            if (rijndaelManaged == null)
            {
                rijndaelManaged.Clear();
            }
        }
        return str1;
    }

    private static string ConvertByteArrayToHexString(byte[] input)
    {
        bool flag;

        StringBuilder stringBuilder = new StringBuilder();
        byte[] bs = input;
        int i = 0;
        do
        {
            byte b = bs[i];
            stringBuilder.AppendFormat("{0:x2}", b);
            i++;

            flag = i < (int)bs.Length;
        }
        while (flag);
        return stringBuilder.ToString();
    }

    private static byte[] ConvertHexStringToByteArray(string hexString)
    {
        bool flag = (hexString.Length & 1) == 0;
        if (!flag)
        {
            throw new ArgumentOutOfRangeException("hexString", hexString, "hexString must contain an even number of characters.");
        }
        byte[] bs1 = new byte[hexString.Length / 2];
        int i = 0;
        do
        {
            bs1[i / 2] = Byte.Parse(hexString.Substring(i, 2), NumberStyles.HexNumber);
            i += 2;

            flag = i < hexString.Length;
        }
        while (flag);
        return bs1;
    }
}
4

1 回答 1

0

您的示例代码非常糟糕,您使用密钥字节作为 IV,对手肯定可以使用选择的密文攻击轻松找出密钥字节。那会很糟糕你的身份验证密钥。

对于 AES-CBC,IV 需要是唯一且不可预测的。

我对您的建议,因为这似乎是某种凭据,您应该使用Authenticated Encryption

我有一些剪切和粘贴代码,我试图保持审查,字符串 C# 的对称身份验证加密的现代示例

我的示例的.NET 内置加密 (AES)-Then-MAC(HMAC)版本在使用nonSecretPayload = new byte[]{2,X};X没有0密码,1使用密码)加密和使用nonSecretPayloadLength = 2.

尽管RNCryptor是 Objective-c,但我相信RNCryptor有 PHP 示例。

于 2013-01-31T14:04:53.733 回答