2016-10-04 9 views
0

私はJCPABEプロジェクトを参照していますが、クラス内のメソッドを使用するとファイルやInputStreamを暗号化できますが、単純なJava文字列は暗号化できません。これらのメソッドを使用して文字列を暗号化/復号化するにはどうすればよいですか? 。どのように私は単純な文字列を暗号化/復号化することができ、私はバイト配列に文字列を変換しようとしているが、私はInputStreamStringを変換する場合には(すなわちString.getBytes("UTF_8");同じように動作しませんJCPABE Encypt Decrypt String

に例:? 私の簡単なコードを:

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); 
    } 
} 

が、私は他のメッセージを持っている:リソースタイプの文字列に文字列に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[]に変換してからストリームに変換する必要があります。

関数では、 "outパラメータ" String outputstrのようなものを定義しました。しかし、文字列はJavaでは不変なので、そのように使用することはできず、その内容を変更することはできません。代わりにそれを戻り値として使用してください。

Stringに換算すると、new String(<byte array>)を使用したことはありません。これは、印刷可能な文字列ではなくバイナリの印刷不可能な内容の文字列を返します。あなたはそれをエンコードする必要があります。 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

素晴らしい@Robert、あなたの説明のおかげで。私はすべてを理解し、私は解読機能を書いています:)。 – CipherX

+0

もちろん、プラットフォーム固有の文字セットを使用するエンコードされたテキストがバイト配列に含まれていることが分かっていない限り、 'new [String()'を使用して 'byte []'を 'String'に変換しようとすることは決してありません。現代の暗号のための暗号文の場合、それは決してそうではありません、それは通常、ランダムから区別できないと考えられるからです。 –

関連する問題