2016-10-04 2 views
0

프로젝트에 JCPABE이 표시되지만 클래스에있는 메소드를 사용하여 파일이나 InputStream을 암호화 할 수 있지만 간단한 Java 문자열은 암호화 할 수 없습니다. 이 방법을 사용하여 문자열을 암호화/해독하는 방법은 무엇입니까? 나는 바이트 배열에서 문자열을 변환하는 시도했지만 내가 InputStreamString을 변환 할 경우는 (I 암호화/어떻게 간단한 문자열을 해독JCPABE Encypt Decrypt String

예 예 String.getBytes("UTF_8"); 동일하게 작동하지 않습니다.? 내 간단한 코드를 :

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 유형의 메소드 암호화 (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과 같은 것을 정의했습니다. 그러나 String은 Java에서 변경되지 않으므로이 방법으로 사용할 수 없으며 내용을 변경할 수 없습니다. 대신 반환 값으로 사용하십시오.

세 번째 절대 byte[]String (으)로 변환하려고 시도하면 new String(<byte array>)입니다. 인쇄 가능한 문자가있는 String이 아닌 인쇄 할 수없는 이진 내용이있는 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'으로 변환하려고 절대로 시도하지 마십시오. 현대 암호를위한 암호문의 경우에, 그러나 그것은 결코 무의미한 것과 구별 할 수 없기 때문에 항상 그렇습니다. –

관련 문제