Android中DatatypeConverter的替代方法

编程入门 行业动态 更新时间:2024-10-26 05:34:25
本文介绍了Android中DatatypeConverter的替代方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我尝试在Android中实现算法AES 128,但无法正常工作,问题是 import javax.xml.bind.DatatypeConverter;

I trying implement algorithm AES 128 in Android but it doesn't work, the problem is import javax.xml.bind.DatatypeConverter;

DatatypeConverter.parseHexBinary(key) 和 DatatypeConverter.printBase64Binary(finalData)

是否存在替代方法?

我的方法:

private static final String ALGORIT = "AES"; public static String encryptHackro(String plaintext, String key) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, IOException, DecoderException { byte[] raw = DatatypeConverter.parseHexBinary(key); SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); Cipher cipher = Cipher.getInstance(ALGORITMO); cipher.init(Cipher.ENCRYPT_MODE, skeySpec); byte[] cipherText = cipher.doFinal(plaintext.getBytes("")); byte[] iv = cipher.getIV(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); outputStream.write(iv); outputStream.write(cipherText); byte[] finalData = outputStream.toByteArray(); String encodedFinalData = DatatypeConverter.printBase64Binary(finalData); return encodedFinalData; }

我看到其他人答案,但我无法实现解决方案。

I see others answers, but I can't implement a solution.

推荐答案

解决方案

我使用

compile 'commons-codec:commons-codec:1.3'

,而我在Android上使用android.util.Base64

and I use android.util.Base64 for Android

不兼容 / 替换

DatatypeConverter.parseHexBinary org.apachemons.codec.binary.Hex.decodeHex(key.toCharArray()); DatatypeConverter.printBase64Binary(finalData); android.util.Base64.encodeToString(finalData, 16) DatatypeConverter.parseBase64Binary(encodedInitialData); org.apachemons.codec.binary.Hex.decodeHex(key.toCharArray());

更多推荐

Android中DatatypeConverter的替代方法

本文发布于:2023-11-15 17:28:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1596495.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方法   Android   DatatypeConverter

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!