\r
private static final boolean debug = true;\r
private static String TAG = "CryptoHelper";\r
+ \r
+ public static final String OISAFE_EXTENSION = ".oisafe";\r
+ \r
protected static PBEKeySpec pbeKeySpec;\r
protected static PBEParameterSpec pbeParamSpec;\r
protected static SecretKeyFactory keyFac;\r
InputStream is;\r
if (fileUri.getScheme().equals("file")) {\r
is = new java.io.FileInputStream(fileUri.getPath());\r
- outputPath = fileUri.getPath() + ".oisafe";\r
+ outputPath = fileUri.getPath() + OISAFE_EXTENSION;\r
} else {\r
is = contentResolver.openInputStream(fileUri);\r
- outputPath = Environment\r
- .getExternalStorageDirectory().toString() + "/tmp.oisafe";\r
+ outputPath = getTemporaryFileName();\r
}\r
\r
FileOutputStream os = new FileOutputStream(outputPath);\r
if (status==false) {\r
return null;\r
}\r
- return Uri.parse("file://" + outputPath); // TODO: UUEncode\r
+ return Uri.fromFile(new File(outputPath)); //Uri.parse("file://" + outputPath); // TODO: UUEncode\r
}\r
+ /**\r
+ * @return\r
+ */\r
+ private String getTemporaryFileName() throws CryptoHelperException {\r
+ String randomPart;\r
+ try {\r
+ // create a random session name\r
+ randomPart=generateSalt();\r
+ } catch (NoSuchAlgorithmException e1) {\r
+ e1.printStackTrace();\r
+ String msg = "Decrypt error: "+e1.getLocalizedMessage();\r
+ throw new CryptoHelperException(msg);\r
+ }\r
+ \r
+ return Environment\r
+ .getExternalStorageDirectory().toString() + "/tmp-" + randomPart;\r
+ }\r
\r
/**\r
- * Unencrypt a file previously encrypted with\r
+ * Dencrypt a file previously encrypted with\r
* encryptFileWithSessionKey().\r
* \r
+ * Creates a new file without the .oisafe extension.\r
+ * \r
* @author Peli\r
* \r
* @param ctx Context of activity in order to store temp file\r
* @param fileUri Uri to either a stream or a file to read from\r
- * @param useContentProvider true for using Content Provider,\r
- * false for creating a file without ".oisafe" extension and\r
- * deleting the original file.\r
* @return If decryption is successful, returns Uri of a content \r
* provider to read the plaintext file. Upon failure,\r
* return null.\r
* @throws Exception\r
*/\r
public Uri decryptFileWithSessionKey(Context ctx, Uri fileUri) throws CryptoHelperException {\r
+ Log.d(TAG, "fileUri="+fileUri.toString());\r
+ ContentResolver contentResolver = ctx.getContentResolver();\r
+\r
+ String outputPath = null;\r
+ Uri resultUri = null;\r
+ boolean result = false;\r
+ \r
+ try {\r
+ InputStream is;\r
+ if (fileUri.getScheme().equals("file")) {\r
+ String inputPath = fileUri.getPath();\r
+ is = new java.io.FileInputStream(inputPath);\r
+ if (debug) Log.d(TAG, "Decrypt: Input from " + inputPath);\r
+ if (inputPath.endsWith(OISAFE_EXTENSION)) {\r
+ outputPath = inputPath.substring(0, inputPath.length() - OISAFE_EXTENSION.length());\r
+ }\r
+ } else {\r
+ is = contentResolver.openInputStream(fileUri);\r
+ if (debug) Log.d(TAG, "Decrypt: Input from " + fileUri.toString());\r
+ }\r
+\r
+ if (outputPath == null) {\r
+ outputPath = getTemporaryFileName();\r
+ }\r
+ \r
+ FileOutputStream os = new FileOutputStream(outputPath);\r
+ \r
+ // after writing the decrypted content to a temporary file,\r
+ // pass back a Uri that can be used to read back the contents\r
+ resultUri = Uri.fromFile(new File(outputPath)); //Uri.parse("file://" + outputPath); // TODO: UUEncode?\r
+ \r
+ result = decryptStreamWithSessionKey(ctx, is, os);\r
+ \r
+ // Close the input stream\r
+ is.close();\r
+ os.close();\r
+\r
+ } catch (FileNotFoundException e) {\r
+ Log.e(TAG, "File not found", e);\r
+ } catch (IOException e) {\r
+ Log.e(TAG, "IOException", e);\r
+ }\r
+\r
+\r
+ if (result == true) {\r
+ // Successful\r
+\r
+ // Securely delete the original file:\r
+ \r
+ SecureDelete.delete(new File(fileUri.getPath()));\r
+ } else {\r
+ resultUri = null;\r
+ \r
+ // Unsuccessful. Clean up\r
+ //ctx.deleteFile(sessionFile);\r
+ }\r
+ \r
+ return resultUri;\r
+ }\r
+ \r
+ /**\r
+ * Dencrypt a file previously encrypted with\r
+ * encryptFileWithSessionKey().\r
+ * \r
+ * The original file is not modified\r
+ * \r
+ * @author Peli\r
+ * \r
+ * @param ctx Context of activity in order to store temp file\r
+ * @param fileUri Uri to either a stream or a file to read from\r
+ * @return If decryption is successful, returns Uri of a content \r
+ * provider to read the plaintext file. Upon failure,\r
+ * return null.\r
+ * @throws Exception\r
+ */\r
+ public Uri decryptFileWithSessionKeyThroughContentProvider(Context ctx, Uri fileUri) throws CryptoHelperException {\r
Log.d(TAG, "fileUri="+fileUri.toString());\r
ContentResolver contentResolver = ctx.getContentResolver();\r
\r