简体   繁体   English

Java AES CBC解密

[英]Java AES CBC Decryption

PHP Encrypt Function PHP加密功能

$privateKey = "1234567812345678";
$iv = "1234567812345678";
$data = "Test string";

$encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $privateKey, $data, MCRYPT_MODE_CBC, $iv);

echo(base64_encode($encrypted));

Result: iz1qFlQJfs6Ycp+gcc2z4w==

When I try to decrypt this result in Java using the function below, all I get back is ì š@ÔBKxnfÈ~¯Ô'M while I am expecting "Test string". 当我尝试使用下面的函数在Java中解密这个结果时,我得到的所有内容都是ì š@ÔBKxnfÈ~¯Ô'M而我期待“测试字符串”。 Any ideas where I am wrong? 我错的任何想法? Thanks 谢谢

public static String decrypt() throws Exception{
    try{
        String Base64EncodedText = "iz1qFlQJfs6Ycp+gcc2z4w==";
        String decodedText = com.sun.xml.internal.messaging.saaj.util.Base64.base64Decode(Base64EncodedText);
        String key = "1234567812345678";
        String iv = "1234567812345678";

        javax.crypto.spec.SecretKeySpec keyspec = new javax.crypto.spec.SecretKeySpec(key.getBytes(), "AES");
        javax.crypto.spec.IvParameterSpec ivspec = new javax.crypto.spec.IvParameterSpec(iv.getBytes());

        javax.crypto.Cipher cipher = javax.crypto.Cipher.getInstance("AES/CBC/NoPadding");
        cipher.init(javax.crypto.Cipher.DECRYPT_MODE, keyspec, ivspec);
        byte[] decrypted = cipher.doFinal(decodedText.getBytes());

        String str = new String(decrypted);

        return str;

    }catch(Exception e){
        return null;
    }   
}

EDIT: As of Java 8 Java now includes an acceptable Base64 class, java.util.Base64 . 编辑:从Java 8开始,Java现在包含一个可接受的Base64类java.util.Base64


This line 这条线

String decodedText = com.sun.xml.internal.messaging.saaj.util.Base64.base64Decode(Base64EncodedText);

looks wrong. 看起来不对劲 Instead, use the apache commons codec classes or the Harder base64 class. 相反,使用apache commons编解码器类或Harder base64类。 Also the default padding used by mcrypt, zero padding, is arguably wrong and makes it difficult to use the results in other languages. 此外,mcrypt使用的默认填充,零填充,可以说是错误的,并且很难在其他语言中使用结果。 The users comments section for the mcrypt_encrypt web pages has examples of how to do this. mcrypt_encrypt网页的用户评论部分提供了如何执行此操作的示例。

Here is small example that uses the apache commons classes to decrypt your string. 这是一个使用apache commons类来解密字符串的小例子。

import java.nio.charset.Charset;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.Hex;

public class AESToy3 {

    private static final Charset ASCII = Charset.forName("US-ASCII");

    public static void main(String[] args) throws Exception {
        String base64Cipher = "iz1qFlQJfs6Ycp+gcc2z4w==";
        byte [] cipherBytes = Base64.decodeBase64(base64Cipher);
        byte [] iv = "1234567812345678".getBytes(ASCII);
        byte [] keyBytes = "1234567812345678".getBytes(ASCII);

        SecretKey aesKey = new SecretKeySpec(keyBytes, "AES");

        Cipher cipher = Cipher.getInstance("AES/CBC/NOPADDING");
        cipher.init(Cipher.DECRYPT_MODE, aesKey, new IvParameterSpec(iv));

        byte[] result = cipher.doFinal(cipherBytes);
        System.out.println(Hex.encodeHexString(result));
    }

}

this produces the following output: 这会产生以下输出:

5465737420737472696e670000000000

which when decoded as ASCII and removing the trailing zeros gives you Test string 当解码为ASCII并删除尾随零时,会给出Test string

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM