Reputation: 371
I see the JCPABE project, but the methods that are in classes enable me to encrypt file or InputStream but not a simple Java string. How can I use these methods to encrypt/decrypt a string ? I have tried to convert the string in byte array but it doesn't work (i.e. String.getBytes("UTF_8");
same if i convert the String
in an InputStream
. How can I encrypt/decrypt a simple string?
Example: My simple code:
String test="Message";
policy="newyork or losangeles";
Cpabe.encrypt(publickey, policy, test, test);
I have this message: The method encrypt(File, String, File, File) in the type Cpabe is not applicable for the arguments (File, String, String, String).
The function encrypt is this:
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);
}
I have changed the function in:
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);
}
}
but I have other message: The resource type String does not implement java.lang.AutoCloseable on the String in and String out; while on the encrypt I have these message: The method encrypt(AbePublicKey, String, InputStream, OutputStream) in the type Cpabe is not applicable for the arguments (AbePublicKey, String, String, String).
This is the function with the 2 InputStream parameters:
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);
}
and this is the writeEncryptedData method:
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);
}
}
Upvotes: 0
Views: 167
Reputation: 42605
Your code can not because of various reasons. First you need an InputStream and OutputStream. To use a String you have to first convert it to byte[]
and then to a stream.
In your function you defined something like a "out parameter" String outputstr
. However Strings are immutable in Java, therefore you can not use it that way and change it's content. Use it as return value instead.
Third never ever try to convert byte[]
to String
using new String(<byte array>)
. This does not return a String with printable characters but a String with binary non-printable content. You have to encode it e.g. using base64. Before decrypting it you have to apply base64 decode.
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());
}
}
Upvotes: 2