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

时间:2011-07-01 11:13:48

标签: java php android mcrypt

我正在尝试在我的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);

加密方法工作正常,但第二个引发异常。

3 个答案:

答案 0 :(得分:3)

最后!我做到了!谢谢你的所有建议。我想分享这些代码,万一有人像我一样陷入困境:

<强> 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);

答案 1 :(得分:1)

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

答案 2 :(得分:1)

我看了另一个答案中的评论。我遇到了类似的问题,尝试使用PHP中的开放SSL加密大块文本(两侧)。我想Java中会出现同样的问题。

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

另请注意,http可能无法与非ASCII加密数据一起使用。我在传输之前和之后对它进行了base64编码/解码(另外你还要担心base64更改的额外urlencoding,但很容易)。

我不确定您的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));
   }
   /**#@-*/
}