JAVA是php的可靠等价物';s MCRYPT_RIJNDAEL_256

JAVA是php的可靠等价物';s MCRYPT_RIJNDAEL_256,java,php,encryption,rijndael,Java,Php,Encryption,Rijndael,我需要访问一些使用PHP加密的数据。PHP加密是这样的 base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($cipher), $text, MCRYPT_MODE_ECB)); 作为$text的值,它们传递time()函数值,该值在每次调用该方法时都会不同。我已经用Java实现了这一点。像这样, public static String md5(String string) { byte[] hash; try {

我需要访问一些使用PHP加密的数据。PHP加密是这样的

base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($cipher), $text, MCRYPT_MODE_ECB));
作为$text的值,它们传递time()函数值,该值在每次调用该方法时都会不同。我已经用Java实现了这一点。像这样,

public static String md5(String string) {
    byte[] hash;

    try {
        hash = MessageDigest.getInstance("MD5").digest(string.getBytes("UTF-8"));
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException("Huh, MD5 should be supported?", e);
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException("Huh, UTF-8 should be supported?", e);
    }

    StringBuilder hex = new StringBuilder(hash.length * 2);

    for (byte b : hash) {
        int i = (b & 0xFF);
        if (i < 0x10) hex.append('0');
        hex.append(Integer.toHexString(i));
    }

    return hex.toString();
}

public static byte[] rijndael_256(String text, byte[] givenKey) throws DataLengthException, IllegalStateException, InvalidCipherTextException, IOException{
    final int keysize;

    if (givenKey.length <= 192 / Byte.SIZE) {
        keysize = 192;
    } else {
        keysize = 256;
    }

    byte[] keyData = new byte[keysize / Byte.SIZE];
    System.arraycopy(givenKey, 0, keyData, 0, Math.min(givenKey.length, keyData.length));
    KeyParameter key = new KeyParameter(keyData);
    BlockCipher rijndael = new RijndaelEngine(256);
    ZeroBytePadding c = new ZeroBytePadding();
    PaddedBufferedBlockCipher pbbc = new PaddedBufferedBlockCipher(rijndael, c);
    pbbc.init(true, key);

    byte[] plaintext = text.getBytes(Charset.forName("UTF8"));
    byte[] ciphertext = new byte[pbbc.getOutputSize(plaintext.length)];
    int offset = 0;
    offset += pbbc.processBytes(plaintext, 0, plaintext.length, ciphertext, offset);
    offset += pbbc.doFinal(ciphertext, offset);
    return ciphertext;
}


public static String encrypt(String text, String secretKey) throws Exception {


    byte[] givenKey = String.valueOf(md5(secretKey)).getBytes(Charset.forName("ASCII"));

    byte[] encrypted = rijndael_256(text,givenKey);

    return new String(Base64.encodeBase64(encrypted));
}
问题是有时输出与PHP不相似,但有时工作正常。 我认为我的MCRYPT_RIJNDAEL_256方法不可靠。
我想知道哪里出了问题,并找到一种可靠的方法,这样我就可以始终获得与PHP类似的加密字符串。

问题可能是
ZeroBytePadding
。Bouncy中的一个总是添加/删除至少一个值为零的字节(la PKCS5Padding,1到16个字节的填充),但PHP中的一个只填充,直到遇到第一个块边界(0到15个字节的填充)。我已经和Bouncy Castle军团的David讨论过这个问题,但是PHP零字节填充非常不适合Bouncy填充的方式,所以现在你必须自己做,并且使用不带填充的密码


当然,作为一个真正的解决方案,重写PHP部分以使用AES(
MCRYPT_RIJNDAEL_128
)、CBC模式加密、HMAC身份验证、基于真实密码的密钥派生函数(PBKDF,例如PBKDF2或bcrypt)和PKCS#7兼容填充,而不是使用这种不安全、不兼容的代码。或者,选择OpenSSL兼容性或已知的安全容器格式

我曾经尝试创建一个类似的类,试图模仿所有的
mcrypt\u encrypt
,但最后我厌恶地放弃了。不过,我确实重写了
mcrypt_encrypt
的示例代码,以防止人们落入相同的陷阱。我感谢您所做的一切。我正在创建一个类似于现有PHP框架的java框架。有许多客户机已经在使用PHP框架。我将检查我能做些什么来将其更改为MCRYPT_RIJNDAEL_128。谢谢。
long time= System.currentTimeMillis()/1000;
String encryptedTime = EncryptionUtils.encrypt(String.valueOf(time), secretkey);