如何将byth []的“ AES”密钥转换为UTF-8字符串

问题描述 投票:-1回答:1

我需要将secretKey.getEncoded()从byte []转换为String,以便将密钥保存到db并通过消息将密钥发送给另一方。

现在我想在Base64中做到这一点(24字节),但另一方只能以UTF-8格式读取它((16字节)] >>

我尝试这样做:String str = new String(bytes, Charsets.UTF_8); ,但是我得到了带有如下引号的乱码乱码:k��$��v/~M�6�L�

正确的方法是什么?

 public static String generateAESKey() throws Exception {

    KeyGenerator kgen = KeyGenerator.getInstance("AES"); 
    kgen.init(128); 
    SecretKey secretKey = kgen.generateKey();

    return new String(Base64.encode(secretKey.getEncoded(), Base64.NO_WRAP));

}

我需要将secretKey.getEncoded()从byte []转换为String,以便将密钥保存到db并通过消息将密钥发送给另一方。现在我想到要在Base64(24个字节)中执行此操作,但是...

java android encryption public-key-encryption
1个回答
1
投票

以字节数组的形式生成密钥,可用于加密和解密过程。


0
投票

您不应该尝试将一个字符集转换为另一个字符集,否则将无法正常工作。而是应将字符串直接转换为该字符集。这个概念很好解释here

© www.soinside.com 2019 - 2024. All rights reserved.