Java AES CBC解密 [英] Java AES CBC Decryption

查看:284
本文介绍了Java AES CBC解密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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==

当我尝试解密这个导致Java使用以下功能,所有我回来的是ì š@ÔBKxnfÈ〜¯Ô'M,而我期待测试字符串。有什么想法我错了吗?感谢

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". 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;
    }   
}


推荐答案

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

看起来错了。相反,请使用 apache commons编解码器类或 Harder base64 类。同样,mcrypt,零填充使用的默认填充可能是错误的,因此使用其他语言的结果变得困难。 mcrypt_encrypt网页的用户评论部分有例如何

looks wrong. Instead, use the apache commons codec classes or the Harder base64 class. Also the default padding used by mcrypt, zero padding, is arguably wrong and makes it difficult to use the results in other languages. The users comments section for the mcrypt_encrypt web pages has examples of how to do this.

这里是使用apache commons类解密字符串的小例子。

Here is small example that uses the apache commons classes to decrypt your string.

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

}

这将产生以下输出: / p>

this produces the following output:

5465737420737472696e670000000000

当以ASCII解码并删除尾随零时,您可以测试字符串

which when decoded as ASCII and removing the trailing zeros gives you Test string

这篇关于Java AES CBC解密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆