java中的加密,PHP中的解密,反之亦然

时间:2015-05-18 11:27:58

标签: java php encoding blowfish

PHP代码:

<?php
class Crypter {

    private $Key;
    private $Algo;
    private $Mode;
    private $Iv;

    public function __construct() {
        $this->Algo = MCRYPT_BLOWFISH;
        $this->Mode = MCRYPT_MODE_CBC;
        $this->Key = substr('7890', 0, mcrypt_get_key_size($this->Algo, $this->Mode));
    }

    public function Encrypt($data) {

        $iv_size = mcrypt_get_iv_size($this->Algo, $this->Mode);
        $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
        $this->Iv = $iv;

        $blocksize = mcrypt_get_block_size('blowfish', 'cbc'); // get block size
        $pkcs = $blocksize - (strlen($data) % $blocksize); // get pkcs5 pad length
        $data.= str_repeat(chr($pkcs), $pkcs); // append pkcs5 padding to the data

        $crypt = mcrypt_encrypt($this->Algo, $this->Key, $data, $this->Mode, $this->Iv);
        return base64_encode($crypt);
    }


    public function Decrypt($data) {

        $crypt = base64_decode($data);
        $decrypt = mcrypt_decrypt($this->Algo, $this->Key, $crypt, $this->Mode, $this->Iv);
        $block = mcrypt_get_block_size('blowfish', 'cbc');
        $pad = ord($decrypt[($len = strlen($decrypt)) - 1]);
        return substr($decrypt, 0, strlen($decrypt) - $pad);
    }
}
?>

JAVA代码:

package blowfishcbc;
import java.security.MessageDigest;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class BlowfishCBC {

    public static void main(String[] args) throws Exception {

        String keyString = "7890";
        String input = "some data";

        Cipher cipher = Cipher.getInstance("Blowfish/CBC/PKCS5Padding");

        // for IV
        byte[] iv = new byte[cipher.getBlockSize()];
        new SecureRandom().nextBytes(iv);
        IvParameterSpec ivSpec = new IvParameterSpec(iv);

        // for key
        byte[] keyData = (keyString).getBytes();
        SecretKeySpec keySpec = new SecretKeySpec(keyData, "Blowfish");

        // encrypt
        cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);
        byte[] encrypted = cipher.doFinal(input.getBytes("UTF-8"));
        String enc = new BASE64Encoder().encode(encrypted);
        System.out.println("encrypted: " + new String(enc));

        // decrypt
        cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
        byte[] dec = new BASE64Decoder().decodeBuffer(enc);
        byte[] decrypted = cipher.doFinal(dec);
        System.out.println("decrypted: " + new String(decrypted, "UTF-8"));
    }
}

我有一个场景,编码和解码将在两个系统之间,如JAVA和PHP。现在上面的类都是PHP和JAVA的例子。

首先,如果我在CBC模式下使用Blowfish算法,我必须提供参数IV(初始化向量)。哪个是随机生成的,长度等于块大小。块大小取决于正在使用的加密模式和算法。现在,如果我想解码用JAVA编码的PHP中的一些数据,那么首先加密密钥必须与IV(初始化向量)相同。

现在在java代码中,IV是随机生成的,如果我在PHP代码中传递相同的IV以及编码的字符串(用java编码),那么我在PHP系统中得到的错误是IV大小必须相同作为块大小。

现在,

for PHP

$iv_size = mcrypt_get_iv_size(MCRYPT_BLOWFISH, MCRYPT_MODE_CBC);

和JAVA

cipher.getBlockSize();

两者都给出了大小为8.但是当我将JAVA中生成的IV传递给PHP时,它会给出错误Message: mcrypt_encrypt(): The IV parameter must be as long as the blocksize现在我该如何解决这个问题。

到目前为止,我理解两个系统中的IV生成过程必须相同。具体而言,IV长度必须相同。一点帮助将不胜感激。感谢。

0 个答案:

没有答案