我看到JCPABE项目,但类中的方法使我能够加密文件或InputStream,但不是简单的Java字符串。我如何使用这些方法来加密/解密字符串?我试图在字节数组的字符串转换,但它不工作(即String.getBytes("UTF_8");
相同的,如果我在InputStream
转换String
我怎样才能加密/解密简单的字符串JCPABE Encypt解密字符串
例:? 我简单的代码:
String test="Message";
policy="newyork or losangeles";
Cpabe.encrypt(publickey, policy, test, test);
我有这样的信息:在该类型的方法,加密(文件,字符串,文件,档案)Cpabe不适用于参数(文件,字符串,字符串,字符串)
功能加密是这样的:
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执行java.lang.AutoCloseable和串出;而在加密时,我有这些消息:类型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);
}
}
“不起作用”不是一个好的错误描述。请向我们展示您的代码和确切的故障(例如,堆栈跟踪以及在哪条线路上出现的指示)。我们不在这里为你写你的解决方案。 –
@MaartenBodewes我是新来的。感谢您的提示。我已经更新了我的问题的描述。我不想要解决方案;我很好奇,我想明白我的错误。 – CipherX