首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将Byte数组转换为String的问题

将Byte数组转换为String的问题
EN

Stack Overflow用户
提问于 2022-04-26 04:42:56
回答 1查看 67关注 0票数 0

解密方法无法将接收到的字节数组转换为字符串。

代码语言:javascript
运行
复制
    public String decryptData(String data, SecretKeySpec key) throws Exception{
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.DECRYPT_MODE, key);
    byte[] decodedValue = Base64.decode(data, Base64.DEFAULT);
    byte[] decValue = cipher.doFinal(decodedValue);
    String outputData = new String(decValue, StandardCharsets.US_ASCII);
    Log.d("Log byte:", decodedValue.toString());
    Log.d("Log byte:", decValue.toString());
    Log.d("Log str:", outputData);
}

输出日志:

代码语言:javascript
运行
复制
D/Log byte:: [B@4b0fd32
D/Log byte:: [B@32d3a83
D/Log str:: 

cipher.doFinal()返回一个空数组,我想这就是我得到空字符串值的原因

EN

回答 1

Stack Overflow用户

发布于 2022-04-26 07:44:12

我用这个方法用AES方法解密。

代码语言:javascript
运行
复制
 private final String characterEncoding = "UTF-8";
private final String cipherTransformation = "AES/CBC/PKCS5Padding";
private final String aesEncryptionAlgorithm = "AES";

public String decrypt(String encryptedText, String key) throws KeyException, GeneralSecurityException, GeneralSecurityException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException, IOException {
        byte [] cipheredBytes = Base64.decode(encryptedText, Base64.DEFAULT);
        byte[] keyBytes = getKeyBytes(key);
        return new String (decrypt(cipheredBytes, keyBytes, keyBytes), characterEncoding);
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72008486

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档