Java JCPABE加密解密字符串

Java JCPABE加密解密字符串,java,string,encryption,cryptography,Java,String,Encryption,Cryptography,我看到了这个项目,但类中的方法使我能够加密文件或InputStream,而不是简单的Java字符串。如何使用这些方法对字符串进行加密/解密?我曾尝试在字节数组中转换字符串,但它不起作用(即,如果我在InputStream中转换string.getBytes(“UTF_8”)字符串,则该字符串的转换方式相同。如何加密/解密简单字符串 例如: 我的简单代码: String test="Message"; policy="newyork or losangeles"; Cpabe.encrypt(pu

我看到了这个项目,但类中的方法使我能够加密文件或InputStream,而不是简单的Java字符串。如何使用这些方法对字符串进行加密/解密?我曾尝试在字节数组中转换字符串,但它不起作用(即,如果我在
InputStream
中转换
string.getBytes(“UTF_8”)
字符串,则该字符串的转换方式相同。如何加密/解密简单字符串

例如: 我的简单代码:

String test="Message";
policy="newyork or losangeles";
Cpabe.encrypt(publickey, policy, test, test);
我有这样一条消息:Cpabe类型中的方法encrypt(File,String,File,File)不适用于参数(File,String,String)

函数encrypt如下所示:

public static void encrypt(File publicKeyFile, String policy, File inputFile, File outputFile) throws IOException, AbeEncryptionException {
    AbePublicKey publicKey = AbePublicKey.readFromFile(publicKeyFile);
    try (BufferedInputStream in = new BufferedInputStream(new FileInputStream(inputFile));
         BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(outputFile))) {
        encrypt(publicKey, policy, in, out);
    }
我已更改了中的功能:

public static void encrypt(File publicKeyFile, String policy, String inputstr, String outputstr) throws IOException, AbeEncryptionException {
    AbePublicKey publicKey = AbePublicKey.readFromFile(publicKeyFile);
    try (String in = new String(inputstr);
         String out = new String(outputstr)) {
        encrypt(publicKey, policy, in, out);
    }
}
但我有其他消息:资源类型String没有在String-in和String-out上实现java.lang.AutoCloseable;而在encrypt上,我有以下消息:Cpabe类型中的方法encrypt(AbePublicKey,String,InputStream,OutputStream)不适用于参数(AbePublicKey,String,String,String)

这是具有2个InputStream参数的函数:

public static void encrypt(AbePublicKey publicKey, String policy, InputStream input, OutputStream output) throws AbeEncryptionException, IOException {
    AbeEncrypted encrypted = encrypt(publicKey, policy, input);
    encrypted.writeEncryptedData(output, publicKey);
}
这是writeEncryptedData方法:

public void writeEncryptedData(OutputStream out, AbePublicKey publicKey) throws IOException {
    AbeOutputStream abeOut = new AbeOutputStream(out, publicKey);
    Version.writeToStream(abeOut);
    cipher.writeToStream(abeOut);
    abeOut.writeInt(iv.length);
    abeOut.write(iv);
    byte[] buffer = new byte[1024];
    int len;
    while ((len = dataStream.read(buffer)) != -1) {
        abeOut.write(buffer, 0, len);
    }
}

由于各种原因,您的代码无法使用。首先,您需要一个InputStream和OutputStream。要使用字符串,您必须先将其转换为
字节[]
,然后再转换为流

在函数中定义了类似“out参数”
String outputstr
。但是字符串在Java中是不可变的,因此不能以这种方式使用它并更改其内容。请将其用作返回值


第三,永远不要尝试使用
new String()
byte[]
转换为
String
。这不会返回包含可打印字符的字符串,而是返回包含二进制不可打印内容的字符串。您必须对其进行编码,例如使用base64。在解密之前,必须应用base64解码

public static String encrypt(File publicKeyFile, String policy, String inputstr) throws IOException, AbeEncryptionException {
    AbePublicKey publicKey = AbePublicKey.readFromFile(publicKeyFile);
    try (InputStream in = new ByteArrayInputStream(inputstr.getBytes(StandardCharsets.UTF_8);
        ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        encrypt(publicKey, policy, in, out);
        return Base64.getEncoder().encodeToString(out.toByteArray());
    }
}

由于各种原因,您的代码无法使用。首先,您需要一个InputStream和OutputStream。要使用字符串,您必须先将其转换为
字节[]
,然后再转换为流

在函数中定义了类似“out参数”
String outputstr
。但是字符串在Java中是不可变的,因此不能以这种方式使用它并更改其内容。请将其用作返回值


第三,永远不要尝试使用
new String()
byte[]
转换为
String
。这不会返回包含可打印字符的字符串,而是返回包含二进制不可打印内容的字符串。您必须对其进行编码,例如使用base64。在解密之前,必须应用base64解码

public static String encrypt(File publicKeyFile, String policy, String inputstr) throws IOException, AbeEncryptionException {
    AbePublicKey publicKey = AbePublicKey.readFromFile(publicKeyFile);
    try (InputStream in = new ByteArrayInputStream(inputstr.getBytes(StandardCharsets.UTF_8);
        ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        encrypt(publicKey, policy, in, out);
        return Base64.getEncoder().encodeToString(out.toByteArray());
    }
}

“它不工作”不是一个很好的错误描述。请向我们显示您的代码和确切的故障(例如,堆栈跟踪和指示它发生在哪一行)。我们不是来为您编写解决方案的。@MaartenBodewes我是新来的。谢谢您的提示。我已经更新了我问题的描述。我不想要解决方案;我是古玩,我想了解我的错误。“它不起作用”不是一个好的错误描述。请向我们展示您的代码和确切的错误(例如,堆栈跟踪和它发生在哪一行的指示)。我们不是来为你写解决方案的。@MaartenBodewes我是新来的。谢谢你的提示。我已经更新了我问题的描述。我不想要解决方案;我是古玩,我想了解我的错误。太好了@Robert,谢谢你的解释。我完全理解,我已经编写了解密函数:).第三,永远不要尝试使用
newstring()将
byte[]
转换为
String
-除非您知道字节数组包含使用特定于平台的字符集的编码文本。当然,对于现代密码的密文,情况永远不会是这样,因为通常认为这与随机密文是无法区分的。太好了@Robert,谢谢您的解释。我完全理解,我已经写下了说明pt函数:)。第三,永远不要尝试使用
new String()
byte[]
转换为
String
——除非您知道字节数组包含使用平台特定字符集的编码文本。对于现代密码的密文来说,情况永远不会是这样,因为人们通常认为密文和随机密文是无法区分的。