Tag: 不对称

Java公钥私钥解密问题

我正在尝试加密和解密消息,如下面的代码所示。 基本上我想用公钥加密消息,并将该加密消息从字节数组转换为String。 并将此字符串解密为原始文本。 以下是这两种方法。 这里加密工作正常,但解密失败(错误是“数据必须从零开始”)。 我认为这是因为我将加密的字节数组转换为String。 我该如何解决这个问题? (我希望将加密的字节数组作为字符串并用于解密)是否有其他方法(使用公钥和私钥) public static String getEncryptedMessage(String publicKeyFilePath, String plainMessage) { byte[] encryptedBytes; try { Cipher cipher = Cipher.getInstance(“RSA”); byte[] publicKeyContentsAsByteArray = getBytesFromFile(publicKeyFilePath); PublicKey publicKey = getPublicKey(publicKeyContentsAsByteArray); cipher.init(Cipher.ENCRYPT_MODE, publicKey); encryptedBytes = cipher.doFinal(plainMessage.getBytes()); return new String(encryptedBytes); } catch (Throwable t) { } } public static String getDecryptedMessage( String privateKeyFilePath, String encryptedMessage) […]