Encryption Source Code

Here is the source code for both Cryptic Mail and The Crypt

Knowing this is of no use to hackers.

private void crypto(final String file, String folderdirectory, String pass1, final AlertDialog progbar) {

if (file.endsWith(".enc")) {
File pathname = new File(folderdirectory + "/" + file);


try {

FileInputStream in = new FileInputStream(pathname);
byte[] salt = new byte[16], iv = new byte[16];
in.read(salt);
in.read(iv);

SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
KeySpec spec = new PBEKeySpec(pass1.toCharArray(), salt, 78134, 256);
SecretKey tmp = factory.generateSecret(spec);
SecretKeySpec skey = new SecretKeySpec(tmp.getEncoded(), "AES");

Cipher ci = Cipher.getInstance("AES/CBC/PKCS5Padding");
ci.init(Cipher.DECRYPT_MODE, skey, new IvParameterSpec(iv));
String encname = file;
encname = encname.replace(".enc", "");
try (FileOutputStream out = new FileOutputStream(folderdirectory + "/" + encname)) {
processFile(ci, in, out, file,folderdirectory,progbar);
}
} catch (InvalidKeySpecException | NoSuchAlgorithmException | InvalidAlgorithmParameterException | InvalidKeyException | IOException | NoSuchPaddingException e) {
e.printStackTrace();
String errorname = file;
errorname = errorname.replace(".enc", "");
File errors = new File(folderdirectory + "/" + errorname);
errors.delete();
passworderrors(file,progbar);
}
final String afile = file.replace(".enc","");
//final File bfile = new File (folderdirectory+"/"+afile);
//if (bfile.exists()){
//pathname.delete();
//}


runOnUiThread(new Runnable() {
@Override
public void run() {
progbar.dismiss();
ShowSavedFiles();

}
});
}


else {


File name = new File(folderdirectory + "/" + file);
try {
byte[] salt = new byte[16];
SecureRandom srandom = new SecureRandom();
srandom.nextBytes(salt);

SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");

KeySpec spec = new PBEKeySpec(pass1.toCharArray(), salt, 78134, 256);
SecretKey tmp = factory.generateSecret(spec);
SecretKeySpec skey = new SecretKeySpec(tmp.getEncoded(), "AES");

byte[] iv = new byte[16];
srandom.nextBytes(iv);
IvParameterSpec ivspec = new IvParameterSpec(iv);

FileOutputStream out = new FileOutputStream(name + ".enc");
out.write(salt);
out.write(iv);

Cipher ci = Cipher.getInstance("AES/CBC/PKCS5Padding");
ci.init(Cipher.ENCRYPT_MODE, skey, ivspec);

try (FileInputStream in = new FileInputStream(name)) {
processFile(ci, in, out,file,folderdirectory,progbar);
}

} catch ( IOException | NoSuchPaddingException | InvalidAlgorithmParameterException | InvalidKeyException | NoSuchAlgorithmException | InvalidKeySpecException e) {
e.printStackTrace();
String errorname = file +".enc";
File errors = new File(folderdirectory+"/"+errorname);
errors.delete();
passworderrors(file,progbar);
}
name.delete();

runOnUiThread(new Runnable() {
@Override
public void run() {
progbar.dismiss();
ShowSavedFiles();
}
});
}
}




private void processFile (Cipher ci, InputStream in, OutputStream out, final String file, final String folderdirectory, final AlertDialog progbar)
{

try {
File token_folder = getApplicationContext().getDir("token", getApplicationContext().MODE_PRIVATE);
String token = "token";
File token_file = new File(token_folder, token);
String tokeninteger = "1";
DataOutputStream DOStream = new DataOutputStream(new FileOutputStream(token_file));
DOStream.write(tokeninteger.getBytes());
DOStream.close();
} catch (IOException e1) {
e1.printStackTrace();
}

try{ byte[] ibuf = new byte[1024*14];
int len;
while ((len = in.read(ibuf)) != -1) {
byte[] obuf = ci.update(ibuf, 0, len);
if (obuf != null) out.write(obuf);
}
byte[] obuf = ci.doFinal();
if (obuf != null) out.write(obuf);

} catch (BadPaddingException | IOException | IllegalBlockSizeException e) {
e.printStackTrace();
if (file.endsWith(".enc")){
String afile = file. replace(".enc","");
File bfile = new File (folderdirectory+"/"+afile);
bfile.delete();
}