2016-10-04 78 views
0

我看到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); 
    } 
} 
+0

“不起作用”不是一个好的错误描述。请向我们展示您的代码和确切的故障(例如,堆栈跟踪以及在哪条线路上出现的指示)。我们不在这里为你写你的解决方案。 –

+0

@MaartenBodewes我是新来的。感谢您的提示。我已经更新了我的问题的描述。我不想要解决方案;我很好奇,我想明白我的错误。 – CipherX

回答

2

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

在你的函数中,你定义了一个类似“输出参数”String outputstr的东西。然而,字符串在Java中是不可变的,因此您不能以这种方式使用它并更改它的内容。改为使用它作为返回值。

第三永远不会尝试使用new String(<byte array>)转换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()); 
    } 
} 
+0

伟大的@罗伯特,谢谢你的解释。我明白所有,我已经写了解密功能:)。 – CipherX

+0

除非你知道字节数组包含使用特定于平台的字符集的编码文本,否则第三次尝试使用'new String()'将'byte []'转换为'String'。在现代密码密文的情况下,情况决不会是这样,因为通常认为这与密码无法区分。 –