将字节数组转换为字符串的问题

发布于 2025-01-24 03:30:50 字数 707 浏览 0 评论 0原文

解密方法未能将接收的字节数组转换为字符串

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

输出日志:

D/Log byte:: [B@4b0fd32
D/Log byte:: [B@32d3a83
D/Log str:: 

cipher.dofinal()返回一个空数组,我认为这就是为什么我得到一个空字符串值

The decryption method fails to convert the received byte array into a string

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

Output logs:

D/Log byte:: [B@4b0fd32
D/Log byte:: [B@32d3a83
D/Log str:: 

cipher.doFinal() returns an empty array, i think that's why I'm getting an empty String value

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

掐死时间 2025-01-31 03:30:50

我正在使用此方法使用AES方法进行解密。

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

I am using this method for decryption with AES method.

 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);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文