2016-03-31 29 views
0

pngファイルからinputStreamを暗号化して暗号化ストリームとして送信し、png画像ファイルとして保存できますか?Androidのpngとしてファイルに入力ストリームを書き込む

暗号化した後、ファイルを復号化して表示しますが、復号化されたストリームから写真を見ることはできません。

OutputStreamを保存すると、ファイルを暗号化または復号化できません。私は例外を投げているわけではありません。私は、暗号化されたバージョンを解読した後、写真を見ることができません。

ファイルを復号化しようとすると、次のようになります。

public void testDecryptionOfPhoto() throws Exception{ 
    File file = new File(getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "encryptedTest.png"); 
    InputStream inputStream = new FileInputStream(file); 
    InputStream decryptedPhoto = decryption.decryptInputStream(inputStream); 
    File file2 = new File(getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "photo.png"); 
    OutputStream outputStream = new FileOutputStream(file2); 
    IOUtils.copy(decryptedPhoto,outputStream); 
    outputStream.close(); 

暗号化

public InputStream encryptInputStream(InputStream inputStream) throws Exception{ 
     KeyCipher keyCiper = new KeyCipher(); 
     String streamContent = CharStreams.toString(new InputStreamReader(inputStream, "UTF-8")); 
     Cipher cipher = Cipher.getInstance("Blowfish"); 
     cipher.init(ENCRYPT_MODE, keyCipher.getSecretSpecKey(), keyCipher.getIvParameterSpec()); 

    InputStream encryptedStream = new ByteArrayInputStream(encodeToString(cipher.doFinal(streamContent.getBytes("UTF-8")), DEFAULT).getBytes()); 
    return encryptedStream; 
} 

復号

public InputStream decryptInputStream(InputStream inputStream) throws Exception{ 
    KeyCipher keyCipher = new keyCipher(); 
    String streamContents = CharStreams.toString(new InputStreamReader(inputStream, "UTF-8")); 
    byte[] encrypted = Base64.decode(streamContents, DEFAULT); 

    Cipher cipher = Cipher.getInstance("Blowfish"); 
    cipher.init(Cipher.DECRYPT_MODE, keyCipher.getSecretSpecKey(), keyCipher.getIvParameterSpec()); 

    byte[] decryptedBytes = cipher.doFinal(encrypted); 
    InputStream decryptedStream = new ByteArrayInputStream(decryptedBytes); 
    return decryptedStream; 
+0

「写真を見る」とはどういう意味ですか?あなたが見たいと思っていることを正確に見ていないのですか? –

+0

暗号化された写真を復号化すると写真が表示されません。そこで私は写真を暗号化し、元の写真を復号化して見ることができません。 –

+0

エラーが発生しましたか?あなたは何を正確に見ていますか?具体的にしてください。 –

答えて

0

あなたが暗号化されたストリームを処理するために、charまたはtoString()を使用することはできません。そして、utf-8も決してありません。あなたがストリーミングしているテキストではありません。暗号化されたバイトをストリームし、バイトとして受け取るだけです。

関連する問題