
编辑:从Java
8开始,Java现在包括可接受的base64类
java.util.base64。
这条线
String depredText = com.sun.xml.internal.messaging.saaj.util.base64.base64Depre(base64EnpredText);
看起来错了。而是使用apache commons编解码器类或Harder
base64类。同样,mcrypt使用的默认填充(零填充)可以说是错误的,并且使其难以使用其他语言显示结果。mcrypt_encrypt网页的用户评论部分包含如何执行此 *** 作的示例。
这是一个使用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.prec.binary.base64;import org.apache.commons.prec.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.deprebase64(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.enpreHexString(result)); }}这将产生以下输出:
5465737420737472696e670000000000
当解码为ASCII并删除尾随零时,
Test string
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)