Java解密错误:数据块大小未对齐


Java decrypt error: data not block size aligned

我正在尝试加密android应用程序和PHP Web服务之间的数据。

我在这个网站上找到了下一段代码:http://schneimi.wordpress.com/2008/11/25/aes-128bit-encryption-between-java-and-php/

但当我试图解密时,我得到了标题"数据块大小未对齐"的异常

这是我MCrypt类中的方法

public String encrypt(String text) throws Exception
{
    if(text == null || text.length() == 0)
        throw new Exception("Empty string");
    Cipher cipher;
    byte[] encrypted = null;
    try {
        cipher = Cipher.getInstance("AES/CBC/NoPadding");
        cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
        encrypted = cipher.doFinal(padString(text).getBytes());
    } catch (Exception e)
    {           
        throw new Exception("[encrypt] " + e.getMessage());
    }
    return new String( encrypted );
}
public String decrypt(String code) throws Exception
{
    if(code == null || code.length() == 0)
        throw new Exception("Empty string");
    Cipher cipher;
    byte[] decrypted = null;
    try {
        cipher = Cipher.getInstance("AES/CBC/NoPadding");
        cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);
        decrypted = cipher.doFinal(hexToBytes(code));
    } catch (Exception e)
    {
        throw new Exception("[decrypt] " + e.getMessage());
    }
    return new String( decrypted );
}

private static byte[] hexToBytes(String hex) {
  String HEXINDEX = "0123456789abcdef";
  int l = hex.length() / 2;
  byte data[] = new byte[l];
  int j = 0;
  for (int i = 0; i < l; i++) {
    char c = hex.charAt(j++);
    int n, b;
    n = HEXINDEX.indexOf(c);
    b = (n & 0xf) << 4;
    c = hex.charAt(j++);
    n = HEXINDEX.indexOf(c);
    b += (n & 0xf);
    data[i] = (byte) b;
  }
  return data;
}
private static String padString(String source)
{
  char paddingChar = ' ';
  int size = 16;
  int x = source.length() % size;
  int padLength = size - x;
  for (int i = 0; i < padLength; i++)
  {
      source += paddingChar;
  }
  return source;
}

这就是我在活动中使用它进行测试的方式:

String encrypted = mcrypt.encrypt(jsonUser.toString());
String decrypted = mcrypt.decrypt(encrypted);

encrypt方法运行良好,但第二个方法抛出异常。

终于!我成功了!谢谢你的建议。我想分享代码,以防有人像我一样陷入困境:

JAVA

import java.security.NoSuchAlgorithmException;
import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
public class MCrypt {
    private String iv = "fedcba9876543210";//Dummy iv (CHANGE IT!)
    private IvParameterSpec ivspec;
    private SecretKeySpec keyspec;
    private Cipher cipher;
    private String SecretKey = "0123456789abcdef";//Dummy secretKey (CHANGE IT!)
    public MCrypt()
    {
        ivspec = new IvParameterSpec(iv.getBytes());
        keyspec = new SecretKeySpec(SecretKey.getBytes(), "AES");
        try {
            cipher = Cipher.getInstance("AES/CBC/NoPadding");
        } catch (NoSuchAlgorithmException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    public byte[] encrypt(String text) throws Exception
    {
        if(text == null || text.length() == 0)
            throw new Exception("Empty string");
        byte[] encrypted = null;
        try {
            cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
            encrypted = cipher.doFinal(padString(text).getBytes());
        } catch (Exception e)
        {           
            throw new Exception("[encrypt] " + e.getMessage());
        }
        return encrypted;
    }
    public byte[] decrypt(String code) throws Exception
    {
        if(code == null || code.length() == 0)
            throw new Exception("Empty string");
        byte[] decrypted = null;
        try {
            cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);
            decrypted = cipher.doFinal(hexToBytes(code));
        } catch (Exception e)
        {
            throw new Exception("[decrypt] " + e.getMessage());
        }
        return decrypted;
    }

    public static String bytesToHex(byte[] data)
    {
        if (data==null)
        {
            return null;
        }
        int len = data.length;
        String str = "";
        for (int i=0; i<len; i++) {
            if ((data[i]&0xFF)<16)
                str = str + "0" + java.lang.Integer.toHexString(data[i]&0xFF);
            else
                str = str + java.lang.Integer.toHexString(data[i]&0xFF);
        }
        return str;
    }

    public static byte[] hexToBytes(String str) {
        if (str==null) {
            return null;
        } else if (str.length() < 2) {
            return null;
        } else {
            int len = str.length() / 2;
            byte[] buffer = new byte[len];
            for (int i=0; i<len; i++) {
                buffer[i] = (byte) Integer.parseInt(str.substring(i*2,i*2+2),16);
            }
            return buffer;
        }
    }

    private static String padString(String source)
    {
      char paddingChar = ' ';
      int size = 16;
      int x = source.length() % size;
      int padLength = size - x;
      for (int i = 0; i < padLength; i++)
      {
          source += paddingChar;
      }
      return source;
    }
}

如何使用它(JAVA(

mcrypt = new MCrypt();
/* Encrypt */
String encrypted = MCrypt.bytesToHex( mcrypt.encrypt("Text to Encrypt") );
/* Decrypt */
String decrypted = new String( mcrypt.decrypt( encrypted ) );

========================================

PHP

<?php 
class MCrypt
{
    private $iv = 'fedcba9876543210'; #Same as in JAVA
    private $key = '0123456789abcdef'; #Same as in JAVA

    function __construct()
    {
    }
    function encrypt($str) {
      //$key = $this->hex2bin($key);    
      $iv = $this->iv;
      $td = mcrypt_module_open('rijndael-128', '', 'cbc', $iv);
      mcrypt_generic_init($td, $this->key, $iv);
      $encrypted = mcrypt_generic($td, $str);
      mcrypt_generic_deinit($td);
      mcrypt_module_close($td);
      return bin2hex($encrypted);
    }
    function decrypt($code) {
      //$key = $this->hex2bin($key);
      $code = $this->hex2bin($code);
      $iv = $this->iv;
      $td = mcrypt_module_open('rijndael-128', '', 'cbc', $iv);
      mcrypt_generic_init($td, $this->key, $iv);
      $decrypted = mdecrypt_generic($td, $code);
      mcrypt_generic_deinit($td);
      mcrypt_module_close($td);
      return utf8_encode(trim($decrypted));
    }
    protected function hex2bin($hexdata) {
      $bindata = '';
      for ($i = 0; $i < strlen($hexdata); $i += 2) {
        $bindata .= chr(hexdec(substr($hexdata, $i, 2)));
      }
      return $bindata;
    }
}

如何使用它(PHP(

<?php 
$mcrypt = new MCrypt();
#Encrypt
$encrypted = $mcrypt->encrypt("Text to encrypt");
#Decrypt
$decrypted = $mcrypt->decrypt($encrypted);

我猜您的keyspecivspec对解密无效。我通常将它们转换为PublicKeyPrivateKey实例,然后使用私钥进行解密。

我查看了另一个答案中的注释。我在php中尝试使用开放SSL加密一大块文本时遇到了类似的问题(在两侧(。我想Java中也会出现同样的问题。

如果您有1024位RSA密钥,则必须将传入的文本拆分为117个字节的块(字符是一个字节(,并对每个块进行加密(您可以将它们连接在一起(。另一方面,您必须将加密的数据拆分为128字节的块,并对每个块进行解密。这应该会给你最初的信息。

还要注意,http可能对非ASCII加密数据不友好。我在传输前后对其进行base64编码/解码(另外,您必须担心base64更改的额外url编码,但这很容易(。

我不确定你的AES密钥长度,但如果是1024位,块长度可能是相同的。如果不是,则必须将比特除以8才能得到字节块长度。不幸的是,我实际上不确定如何将其输入(可能乘以117/128?(

以下是一些php代码:

class Crypto {
   public function encrypt($key, $data) {
      $crypto = '';
      foreach (str_split($data, 117) as $chunk) {
         openssl_public_encrypt($chunk, $encrypted, $key);
         $crypto .= $encrypted;
      }
      return $crypto;
   }
   //Decrypt omitted.  Basically the same, change 117 to 128.
   /**#@+
    * Update data for HTTP transmission and retrieval
    * Must be used on encrypted data, but also useful for any binary data
    * (e.g. zip files).
    */
   public function base64_encode($value) {
      return rtrim(strtr(base64_encode($value), '+/', '-_'), '=');
   }
   //String length must be padded for decoding for some reason
   public function base64_decode($value) {
      return base64_decode(str_pad(strtr($value, '-_', '+/')
         , strlen($value) % 4, '=', STR_PAD_RIGHT));
   }
   /**#@-*/
}