mcrypt_encrypt(): The IV parameter must be as long as the blocksize



  • PHP makes a mistake: mcrypt_encrypt(): The IV parameter must be as long as the blocksize

    That he doesn't like the vector?

    // Ключ
    $AES_Key = "93908027539382757893442837120983";
    

    // Вектор
    $AES_IV = "33985771209830270358974938292834";

    // Методы
    function encrypt($string)
    {
    return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $AES_Key, addpadding($string), MCRYPT_MODE_CBC, $AES_IV));
    }

    function addpadding($string, $blocksize = 32)
    {
    $len = strlen($string);
    $pad = $blocksize - ($len % $blocksize);
    $string .= str_repeat(chr($pad), $pad);
    return $string;
    }

    The same code on the charpe works perfectly:

    // Ключ
    private const string AES_Key = "93908027539382757893442837120983";

    // Вектор
    private const string AES_IV = "33985771209830270358974938292834";

    // Зашифровывает строку в AES CBC 256 PKCS7
    internal static string AES_Encrypt(string inputStr)
    {
    byte[] xBuff = null;

         using (var aes = new RijndaelManaged())
         {
              // Настройки
              aes.KeySize = 256;
              aes.BlockSize = 256;
              aes.Padding = PaddingMode.PKCS7;
              aes.Mode = CipherMode.CBC;
              aes.Key = Encoding.UTF8.GetBytes(AES_Key);
              aes.IV = Encoding.UTF8.GetBytes(AES_IV);
    
              using (var ms = new MemoryStream())
              {
                  using (var cs = new CryptoStream(ms, aes.CreateEncryptor(aes.Key, aes.IV), CryptoStreamMode.Write))
                  {
                       byte[] textArr = Encoding.UTF8.GetBytes(inputStr);
                       cs.Write(textArr, 0, textArr.Length);
                   }
    
                    xBuff = ms.ToArray();
               }
          }
    
      return Convert.ToBase64String(xBuff);
    

    }



  • Working option:

    PHP:

    // Методы
    function encrypt($message)
    {
        // Ключ 
        $AES_Key256 = '28937539283712098445739080393827';
    
    // Вектор
    $AES_IV = '8574930392882739';
    
    // Алгоритм шифрования
    $cipher = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
    
    if (mcrypt_generic_init($cipher, $AES_Key256, $AES_IV) != -1)
    {
        $cipherText = mcrypt_generic($cipher, addpadding($message));
        mcrypt_generic_deinit($cipher);
        return base64_encode($cipherText);
    }
    

    }

    function addpadding($string, $blocksize = 16)
    {
    $len = strlen($string);
    $pad = $blocksize - ($len % $blocksize);
    $string .= str_repeat(chr($pad), $pad);
    return $string;
    }

    C#

    // Ключ
    private const string AES_Key = "28937539283712098445739080393827";

    // Вектор
    private const string AES_IV = "8574930392882739";

    // Расшифровывает строку из AES 256 CBC PKCS7 из файла
    internal static string AES_Decrypt(string inputStr)
    {
    byte[] xBuff = null;

            using (var aes = new RijndaelManaged())
            {
                // Настройки
                aes.KeySize = 256;
                aes.BlockSize = 128;
                aes.Mode = CipherMode.CBC;
                aes.Padding = PaddingMode.PKCS7;
                aes.Key = Encoding.UTF8.GetBytes(AES_Key);
                aes.IV = Encoding.UTF8.GetBytes(AES_IV);
    
                using (var ms = new MemoryStream())
                {
                    using (var cs = new CryptoStream(ms, aes.CreateDecryptor(), CryptoStreamMode.Write))
                    {
                        byte[] textArr = Convert.FromBase64String(inputStr);
                        cs.Write(textArr, 0, textArr.Length);
                    }
                    xBuff = ms.ToArray();
                }
            }
    
            return Encoding.UTF8.GetString(xBuff);
    

    }

    Article required to understand the principles of AES in PHP: https://www.chilkatsoft.com/p/php_aes.asp




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2