Java等价于PHP的三重DES ECB加密/解密


Java equivalent for PHP Triple DES ECB encryption/decryption

我有一个下面的PHP函数,我想在Java中实现没有运气,我无法获得相同的输出:

function encryptText( $plainText, $key )
{
    $mcopen = mcrypt_module_open (MCRYPT_TripleDES, "", MCRYPT_MODE_ECB,"");
    $iv = mcrypt_create_iv (mcrypt_enc_get_iv_size ($mcopen), MCRYPT_RAND);
    $td = mcrypt_module_open('tripledes', '', 'ecb', '');
    $cryptedHash = '';
    if (mcrypt_generic_init($td, $key, $iv) != -1)
    {
        $cryptedHash = mcrypt_generic($td, $plainText);
        mcrypt_generic_deinit($td);
        mcrypt_module_close($td);
    }
    return base64_encode($cryptedHash);
 }

这是我在这里找到的java代码:

public static String encrypt(String message, String key) throws Exception {
    final MessageDigest md = MessageDigest.getInstance("md5");
    final byte[] digestOfPassword = md.digest(key.getBytes("utf-8"));
    final byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
    for (int j = 0, k = 16; j < 8;) {
        keyBytes[k++] = keyBytes[j++];
    }
    final SecretKey keyz = new SecretKeySpec(keyBytes, "DESede");
    final IvParameterSpec iv = new IvParameterSpec(new byte[8]);
    final Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, keyz, iv);
    final byte[] plainTextBytes = message.getBytes("utf-8");
    final byte[] cipherText = cipher.doFinal(plainTextBytes);
    final String encodedCipherText = new   sun.misc.BASE64Encoder().encode(cipherText);
    return encodedCipherText;
}

你有几个问题。

  • 由于您想使用ECB模式,那么您需要指定ECB模式。CBC是另一种模式

  • 你没有在PHP代码中通过MD5运行你的密钥,所以你不应该在Java中这样做。

  • ECB或CBC模式下的分组密码需要填充来加密任意消息。Mcrypt默认使用零填充,但是您在Java中指定pkcs# 5填充。由于默认的Sun提供程序不实现ZeroPadding,你需要通过指定"NoPadding"来实现零填充,或者使用BouncyCastle提供程序:Cipher.getInstance("DESede/ECB/ZeroPadding", "BC") .


其他注意事项:

永远不要使用ECB模式。它在语义上不安全。如果您使用相同的密钥对同一消息进行两次加密,攻击者很容易看到您再次加密了同一消息(或消息的相同部分)。

解决了,解决了:

public static String cryptBC(String data, String key) throws Exception{
    Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
    byte[] input = data.getBytes();
    byte[] keyBytes = key.getBytes() ;   
    SecretKeySpec skey = new SecretKeySpec(keyBytes, "DESede");
    Cipher cipher = Cipher.getInstance("DESede/ECB/NoPadding", "BC");
    if(input.length % 8 != 0){ 
        byte[] padded = new byte[input.length + 8 - (input.length % 8)];
        System.arraycopy(input, 0, padded, 0, input.length);
        input = padded;
    }
    System.out.println("input : " + new String(input));
    cipher.init(Cipher.ENCRYPT_MODE, skey);
    byte[] cipherText = new byte[cipher.getOutputSize(input.length)];
    int ctLength = cipher.update(input, 0, input.length, cipherText, 0);
    ctLength += cipher.doFinal(cipherText, ctLength);
    return new String(Base64.encodeBase64(cipherText));
}