【问题标题】:AES algorithm differs in PHP from Android and iOSPHP 中的 AES 算法与 Android 和 iOS 不同
【发布时间】:2013-08-21 12:47:12
【问题描述】:

您好,我目前正在使用 PHP 和 Android 中的 AES 算法对字符串进行加密和解密。我在 iOS 和 Android 中得到了类似的值。但我无法在 PHP 中获得相同的输出。它显示了一些其他加密字符串。我想在所有 iOS、Android 和 PHP 中实现相同的结果。目前 iOS 和 Android 运行良好。但我无法在 PHP 中修复。

请检查屏幕截图并比较值。我使用“Android”作为值,使用“abcdef”作为键。

<?php

$Pass = "abcdef";
$Clear = "android";        

$crypted = mc_encrypt($Clear, $Pass);
echo "Encrypred: ".$crypted."</br>";

$newClear = mc_decrypt($crypted, $Pass);
echo "Decrypred: ".$newClear."</br>";  



function mc_encrypt($encrypt, $mc_key) {
    $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB), MCRYPT_RAND);
    $passcrypt = trim(mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $mc_key, trim($encrypt), MCRYPT_MODE_ECB, $iv));
    $encode = base64_encode($passcrypt);
    return $encode;
}

function mc_decrypt($decrypt, $mc_key) {
    $decoded = base64_decode($decrypt);
    $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB), MCRYPT_RAND);
    $decrypted = trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $mc_key, trim($decoded), MCRYPT_MODE_ECB, $iv));
    return $decrypted;
}

?>

我得到以下输出 加密:+NzljOmN0msNkWr/cst11Q==

解密:安卓

以下代码用于Android

package com.example.aesalg;

import java.security.MessageDigest;
import java.security.spec.AlgorithmParameterSpec;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import android.util.Base64;

public class AESCrypt {

private final Cipher cipher;
private final SecretKeySpec key;
private AlgorithmParameterSpec spec;


public AESCrypt(String password) throws Exception
{
    // hash password with SHA-256 and crop the output to 128-bit for key
    MessageDigest digest = MessageDigest.getInstance("SHA-256");
    digest.update(password.getBytes("UTF-8"));
    byte[] keyBytes = new byte[32];
    System.arraycopy(digest.digest(), 0, keyBytes, 0, keyBytes.length);

    cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
    key = new SecretKeySpec(keyBytes, "AES");
    spec = getIV();
}       

public AlgorithmParameterSpec getIV()
{
    byte[] iv = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, };
    IvParameterSpec ivParameterSpec;
    ivParameterSpec = new IvParameterSpec(iv);

    return ivParameterSpec;
}

public String encrypt(String plainText) throws Exception
{
    cipher.init(Cipher.ENCRYPT_MODE, key, spec);
    byte[] encrypted = cipher.doFinal(plainText.getBytes("UTF-8"));
    String encryptedText = new String(Base64.encode(encrypted, Base64.DEFAULT), "UTF-8");
    System.out.println("Encrypt Data"+ encryptedText);
    return encryptedText;
}

public String decrypt(String cryptedText) throws Exception
{
    cipher.init(Cipher.DECRYPT_MODE, key, spec);
    byte[] bytes = Base64.decode(cryptedText, Base64.DEFAULT);
    byte[] decrypted = cipher.doFinal(bytes);
    String decryptedText = new String(decrypted, "UTF-8");
    System.out.println("Encrypt Data"+ decryptedText);
    return decryptedText;
}
}

【问题讨论】:

  • 你在 php 中的输出是什么?
  • 手机版有多少位?尝试一种不同的 PHP 加密类型 - 也许它不是您想要的 128:php.net/manual/en/mcrypt.ciphers.php
  • 用的是什么android代码?
  • 每次运行 PHP / android / iOS 代码时得到相同的加密输出吗?
  • @cuewizchris 是的,我每次都得到相同的输出。

标签: php android ios aes


【解决方案1】:

您在 Android 应用程序中使用 CBC,在 PHP 代码中使用 ECB。详情请见wikipedia

尝试将 mcrypt 参数更改为MCRYPT_MODE_CBC。另外我相信 mcrypt 总是使用零填充(我不是 PHP 专家)所以在 Android 端你必须使用"AES/CBC/ZeroBytePadding"

【讨论】:

  • 您能解释一下 Java 代码中显示这种情况的原因(还是需要更改的默认值)?你能解释一下这两种模式之间的区别,并建议最好改变哪一种吗? (这可能是正确的答案,但这里鼓励大量答案:=))。
【解决方案2】:

在php中试试这段代码

  public function encrypt($string, $key)
  {
      $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_ECB, '');
      $block = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB);
      $pad = $block - (strlen($string) % $block);
      $string .= str_repeat(chr($pad), $pad);
      mcrypt_generic_init($td, $key, 'fedcba9876543210');
      $encrypted = mcrypt_generic($td, $string);
      mcrypt_generic_deinit($td);
      mcrypt_module_close($td);

      return $encrypted;
  }

  function decrypt($string, $key) 
  {
      $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_ECB, '');
      mcrypt_generic_init($td, $key, 'fedcba9876543210');
      $decrypted = mdecrypt_generic($td, $string);
      mcrypt_generic_deinit($td); 
      mcrypt_module_close($td);

      return $decrypted;
  }

【讨论】:

  • 你好,Dinesh 你能分享你的iOS代码细节吗?如果它生成与 Android 相同的加密字符串,如果您有解决当前问题的方法,还生成 php 代码。
猜你喜欢
  • 2017-03-18
  • 1970-01-01
  • 2012-10-14
  • 2021-10-27
  • 2015-11-30
  • 2022-01-18
  • 1970-01-01
  • 2017-12-19
  • 2013-07-27
相关资源
最近更新 更多