【问题标题】:String to Byte[] and back to String字符串到 Byte[] 并返回到 String
【发布时间】:2015-08-02 04:14:58
【问题描述】:

我有一个

String X = 0110100001100101011011000110110001101111530940929e959001f70dd4da5f5cc3b373165781

我首先通过 X.getBytes() 将字符串 X 变成一个字节 []; 我使用这个进行了 RC4 加密..

public static byte[] RC4(byte[] x,byte[] keyBytes)   
{   
    byte[] e = null; 

    try   
    {   
        SecureRandom sr = new SecureRandom(keyBytes);
        KeyGenerator kg = KeyGenerator.getInstance(algorithm);
        kg.init(sr);
        SecretKey sk = kg.generateKey();  
        Cipher enCipher = Cipher.getInstance("RC4");   
        enCipher.init(Cipher.ENCRYPT_MODE,sk);   
        e = enCipher.doFinal(plaintext);              
    }   
    catch(Exception ex)   
    {   
        ex.printStackTrace();   
    }   
    return e;   
} 

编辑** 加密后我使用cipher = encrpyted.toString(); 这将返回一个值cipher = [B@a1c582

之后我尝试使用 RC4 运行解密,并使用 toString 函数尝试取回上面列出的字符串 X 的原始值,但无济于事..

我做了什么.. 编辑**

//the reason i send using string is due to me having a client server architecture restriction on buffered writer, and this is the receiving side

            message = stdIn.readLine();
            System.out.println("Message received : " + message);

            byte [] kc = key.getBytes();
            byte [] decrypt = message.getBytes();
            byte [] decryptC = EncryptionScheme.decrypt(decrypt, kc);
            X = new String(decryptC);
            System.out.println("String X = " + X);

Message received : [B@a1c582

String X = ����,��

有什么办法可以解决这个问题吗?我想取回String X = 0110100001100101011011000110110001101111530940929e959001f70dd4da5f5cc3b373165781的字符串

decryption algorithm

    public static byte[] decrypt(byte[] ciphertext,byte[] keyBytes)   
    {   
        byte de[] = null;   
        try   
        {   
            SecureRandom sr = new SecureRandom(keyBytes);
            KeyGenerator kg = KeyGenerator.getInstance(algorithm);
            kg.init(sr);
            SecretKey sk = kg.generateKey();    
            Cipher deCipher = Cipher.getInstance("RC4");   
            deCipher.init(Cipher.DECRYPT_MODE,sk);   
            de = deCipher.doFinal(ciphertext);   
        }   
        catch(Exception e)   
        {   
            e.printStackTrace();   
        }    
        return de;   

    }  

【问题讨论】:

  • 将其更改为Arrays.toString(test) 工作吗?
  • System.out.println("test = " + new String(test)); 怎么样
  • “无济于事”不是问题描述。您在询问您尚未发布的代码的行为。 cipher = [B@a1c582test = [B@dcb6b4 是调用 byte[].toString() 的结果,而不是数组的内容。
  • @DavidEhrmann 不,它没有。与 abv 相同,请阅读我的编辑。谢谢!
  • 我指的是EncryptionScheme.decrypt()。没有那个源代码就不可能回答这个问题。但是,您的问题可能是由于将密文作为字符串发送。 String 不是二进制数据的容器。您需要对其进行 base64 编码,或对其进行十六进制编码等。这也可能是由于发送 [B@a1c582 而不是数组内容。此处引用和暗示的代码太多尚未发布。在这种情况下,您不会接受客户的问题报告。

标签: java string encryption bytearray rc4-cipher


【解决方案1】:

如果您找不到更好的解决方案,您可以执行以下操作

    StringBuilder s = new StringBuilder();
    for(byte b : arr) {
        s.append(Integer.toBinaryString(0x100 | 0xFF & b).substring(1, 9));
    }

【讨论】:

  • 抱歉,这对我不起作用。它似乎返回了一个完全不同的表示:) 谢谢。
【解决方案2】:

加密后我用cipher = encrpyted.toString()

这是无效的。所有这一切都是调用Object.toString() 的结果,它是对象类(byte[],编码为[B)及其System.identityHashCode() 的组合。如果这是您通过网络发送的内容,这是我可以从您的帖子中得出的唯一结论,那么您不可能正确解密它,因为它首先不包含密文。

由于String 不是二进制数据的容器,因此您必须发送密文的base-64 或base-16 或base-36 编码,在接收端解码,然后解密。

【讨论】:

  • 你能给我举个例子吗?关于发送方的编码和接收方的解码?..我不太明白那部分..非常感谢
  • 我建议你给自己找一个 base64-codec 类。我认为在 JDK 7 中有一个,当然是 8,在 Apache Commons 中也是如此。
  • 谢谢我让它工作。但由于我的 java 是 6/7 byte[] message = "hello world".getBytes("UTF-8"); 我使用了以下内容字符串编码 = DatatypeConverter.printBase64Binary(message); byte[] 解码 = DatatypeConverter.parseBase64Binary(encoded); System.out.println(编码); System.out.println(new String(decoded, "UTF-8"));
  • 啊。我赞成。但它没有公开显示,因为我的代表很低。 :D 再次感谢!
猜你喜欢
  • 2013-03-08
  • 1970-01-01
  • 1970-01-01
  • 2023-03-20
  • 1970-01-01
  • 2021-10-21
  • 1970-01-01
  • 1970-01-01
  • 2013-02-15
相关资源
最近更新 更多