有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在使用DatatypeConverter时要做什么。printHexBinary使bytearray中的字节数加倍?

我正试图建立一个与安全服务器的通信协议。服务器在某种程度上等待我的长度为128字节的非对称加密对称密钥字节数组,但在我使用DatatypeConverter之后。printHexBinary将字节数组转换为字符串,使用它后,数组中的字节数将翻倍。我想知道为什么会发生这种情况,以及如何解决它

使用不同类型的方法将字节[]解析为字符串,但服务器无法识别它们

以下是我正在使用的代码:

**注意:公钥由服务器在发送数字证书时发送

对于密码:

private static byte[] cifradoAsimetrico(SecretKey key, PublicKey publicKey) {
    byte[] cipheredText;
    try {           
        Cipher cipher = Cipher.getInstance(asimetrico);         
        byte[] clearText = key.getEncoded();            
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        cipheredText = cipher.doFinal(clearText);
        return cipheredText;
    } catch (Exception e) {
        System.out.println("[Exception] Method cifradoAsimetrico( ): " + e.getMessage());
        return null;
    }
}

对于通信协议:

keyGenerator = KeyGenerator.getInstance(simetrico,new BouncyCastleProvider());
keyGenerator.init(128);
    desKey = keyGenerator.generateKey();

byte[] llaveCifrada = cifradoAsimetrico(desKey, publicaServidor);

System.out.println("Tamaño cifrada: " + llaveCifrada.length);
    String llaveCifradaHexa = DatatypeConverter.printHexBinary(llaveCifrada);

    System.out.println("Tamaño hexa: "+llaveCifradaHexa.getBytes().length);

    socketPrintWriter.println(llaveCifradaHexa);

//关于环境数据256字节的算法和问题, //这是我的服务。没有普迪莫斯·拉兹恩

//服务器在此停止并显示illegalblocksizeexception数据不能超过128字节


共 (0) 个答案