java - Speed up android DES encryption/decryption -


i've got encrypt , decrypt file on android. this, wrote following class:

package blabla.fileencrypter;  import alotofclasses;  /**  * fileencoder class provides interface allow easy encrypting , decrypting of files. use class, first call both {@link #setsalts(string, string)} , {@link #setfolders(string, string)}.  * @author daniƫl van den berg  * @since nov 26, 2015  *  */ public class fileencrypter {     private static string encryptedfolder = "";     private static string decryptedfolder = "";     private static byte[] salt = null;     private static ivparameterspec iv = null;     private static string encryptedpostfix = "";      /**      * sets folders documents have placed in.      * @param encryptedfolder folder encrypted files have placed in.      * @param decryptedfolder folder decrypted files have placed in.      */     public static void setfolders(string encryptedfolder, string decryptedfolder){         fileencrypter.encryptedfolder = encryptedfolder;         fileencrypter.decryptedfolder = decryptedfolder;     }      /**      * postfix give encrypted files. can "" no postfix.      * @param extension postfix append encrypted files.      */     public static void setencryptedpostfix(string extension){         if (extension != null){             fileencrypter.encryptedpostfix = extension;         }else{             fileencrypter.encryptedpostfix = "";         }     }      /**      * salts use when encrypting/decrypting files.      * @param salt salt use.      * @param ivparameterspec buffer iv.      * @throws fileencryptingexception when ivparameterspec smaller 16 bytes.      */     public static void setsalts(string salt, string ivparameterspec) throws fileencryptingexception{         fileencrypter.salt = salt.getbytes();         if (ivparameterspec.length() < 16){             throw new fileencryptingexception("ivparameterspec not long enough. should @ least 16 bytes.");         }         fileencrypter.iv = new ivparameterspec(ivparameterspec.getbytes(),0,16);     }      /**      * encode given inputfile given key.      * @param inputfile file encrypt.      * @param key key use encrypting file.      * @return encrypted file.      * @throws fileencryptingexception      * @throws nosuchalgorithmexception      * @throws nosuchpaddingexception      * @throws invalidkeyexception      * @throws ioexception      * @throws invalidkeyspecexception      * @throws invalidalgorithmparameterexception      */     public static file encrypt(file inputfile, string key) throws fileencryptingexception, nosuchalgorithmexception, nosuchpaddingexception, invalidkeyexception, ioexception, invalidkeyspecexception, invalidalgorithmparameterexception{         if (isencrypted(inputfile)){             throw new fileencryptingexception("file not decrypted: "+inputfile.getabsolutepath());         }          fileinputstream inputstream = new fileinputstream(inputfile);          file outputfile = new file(inputfile.getabsolutepath().replace(decryptedfolder,encryptedfolder) + encryptedpostfix);         outputfile.getparentfile().mkdirs();         fileoutputstream outputstream = new fileoutputstream(outputfile);         processstream(cipher.encrypt_mode,key,inputstream,outputstream);         inputstream.close();         outputstream.close();         return outputfile;     }      /**      * decrypt given inputfile given key.      * @param inputfile file decrypt.      * @param key key use decrypting file.      * @return decrypted file.      * @throws fileencryptingexception      * @throws ioexception      * @throws nosuchalgorithmexception      * @throws nosuchpaddingexception      * @throws invalidkeyexception      * @throws invalidkeyspecexception      * @throws invalidalgorithmparameterexception      */     public static file decrypt(file inputfile, string key) throws fileencryptingexception, ioexception, nosuchalgorithmexception, nosuchpaddingexception, invalidkeyexception, invalidkeyspecexception, invalidalgorithmparameterexception{         if (!isencrypted(inputfile)){             throw new fileencryptingexception("file not encrypted: "+inputfile.getabsolutepath());         }          if (!inputfile.exists() && !inputfile.getabsolutepath().contains(encryptedpostfix)){             inputfile = new file(inputfile.getabsolutepath()+encryptedpostfix);         }          fileinputstream inputstream = new fileinputstream(inputfile);          file outputfile = new file(inputfile.getabsolutepath().replace(encryptedpostfix, "").replace(encryptedfolder,decryptedfolder));         outputfile.getparentfile().mkdirs();         fileoutputstream outputstream = new fileoutputstream(outputfile);         processstream(cipher.decrypt_mode,key,inputstream,outputstream);         inputstream.close();         outputstream.close();         return outputfile;     }      /**      * used generating cipher.      * @param ciphermode cipher mode use. either <code>cipher.decrypt_mode</code> or <code>cipher.encrypt_mode</code>      * @param key key generate cipher with.      * @return generated cipher.      * @throws nosuchalgorithmexception      * @throws invalidkeyspecexception      * @throws nosuchpaddingexception      * @throws invalidkeyexception      * @throws invalidalgorithmparameterexception      */     private static cipher getcipher(int ciphermode, string key) throws nosuchalgorithmexception, invalidkeyspecexception, nosuchpaddingexception, invalidkeyexception, invalidalgorithmparameterexception {         secretkeyfactory factory = secretkeyfactory.getinstance("des");         keyspec spec;         try {             spec = new deskeyspec((key+salt).getbytes("utf8"));         } catch (unsupportedencodingexception e) {             e.printstacktrace();             return null;         }         secretkey secret = factory.generatesecret(spec);          cipher c = cipher.getinstance("des");         c.init(ciphermode, secret,iv);         return c;     }      /**      * process stream. encrypt or decrypt stream, depending on given ciphermode. output available in given {@link outputstream}.      * @param ciphermode cipher mode use. either <code>cipher.decrypt_mode</code> or <code>cipher.encrypt_mode</code>      * @param key key use decryption/encryption.      * @param inputstream stream read from.      * @param outputstream stream write encrypted/decrypted result to.      * @throws invalidkeyexception      * @throws nosuchalgorithmexception      * @throws invalidkeyspecexception      * @throws nosuchpaddingexception      * @throws invalidalgorithmparameterexception      * @throws ioexception      */     public static void processstream(int ciphermode, string key, inputstream inputstream, outputstream outputstream) throws invalidkeyexception, nosuchalgorithmexception, invalidkeyspecexception, nosuchpaddingexception, invalidalgorithmparameterexception, ioexception{         cipher c = getcipher(ciphermode,key);         cipheroutputstream cipheroutputstream = new cipheroutputstream(outputstream, c);         int b = 0;         while ((b = inputstream.read()) !=-1){             cipheroutputstream.write(b);         }         cipheroutputstream.close();     }      /**      * returns file, no matter whether it's encrypted or not. see {@link #isencrypted(file)} detect if file encrypted.      * @param filenamedecrypted filename decrypted file have.      * @return file corresponds given filename.      */     public static file getfile(string filenamedecrypted){         if (!filenamedecrypted.contains(decryptedfolder) && !filenamedecrypted.contains(encryptedfolder)){             filenamedecrypted = decryptedfolder + filenamedecrypted;         }         file file = new file(filenamedecrypted);         if (!file.exists()){             file = new file(filenamedecrypted.replace(decryptedfolder, encryptedfolder)+encryptedpostfix);         }         return file;     }      /**      * checks if file encrypted or not.      * @param file file check.      * @return true if file encrypted, false otherwise.      */     public static boolean isencrypted(file file){         return file.getabsolutepath().contains(encryptedfolder) || (!encryptedpostfix.isempty() && file.getabsolutepath().contains(encryptedpostfix));     } } 

at first implemented class using aes encryption , decryption. however, speed of slow, switched using des. however, still seems terribly slow. file of 1mb takes half minute encrypt.

is there way speed significantly?

credits wagner tsuchiya helping out on one.

the solution laid in 2 things.

  1. using bufferedreader/writer, can seen in functions encrypt() , decrypt. changed speed 3mb/2:16 minutes 3mb/0:59 minutes.
  2. using different encryption algorithm. des turned out slower aes, current requirements simpler algorythm, rc4, sufficient. halved time took encryption , decryption again. comparison of different algorythms can found on http://www.javamex.com/tutorials/cryptography/ciphers.shtml.

this both resulted in following code:

package blabla.fileencrypter;  import lots;  /**  * fileencoder class provides interface allow easy encrypting , decrypting of files. use class, first call both {@link #setsalts(string, string)} , {@link #setfolders(string, string)}.  * @author daniƫl van den berg  * @since nov 26, 2015  *  */ public class fileencrypter {     private static string encryptedfolder = "";     private static string decryptedfolder = "";     private static byte[] salt = null;     private static string encryptedpostfix = "";     private static final hashmap<integer, hashmap<string,cipher>> ciphers = new hashmap<integer, hashmap<string, cipher>>();      /**      * sets folders documents have placed in.      * @param encryptedfolder folder encrypted files have placed in.      * @param decryptedfolder folder decrypted files have placed in.      */     public static void setfolders(string encryptedfolder, string decryptedfolder){         fileencrypter.encryptedfolder = encryptedfolder;         fileencrypter.decryptedfolder = decryptedfolder;     }      /**      * postfix give encrypted files. can "" no postfix.      * @param extension postfix append encrypted files.      */     public static void setencryptedpostfix(string extension){         if (extension != null){             fileencrypter.encryptedpostfix = extension;         }else{             fileencrypter.encryptedpostfix = "";         }     }      /**      * salts use when encrypting/decrypting files.      * @param salt salt use.      * @param ivparameterspec buffer iv.      * @throws fileencryptingexception when ivparameterspec smaller 16 bytes.      */     public static void setsalts(string salt) throws fileencryptingexception{         fileencrypter.salt = salt.getbytes();     }      /**      * encode given inputfile given key.      * @param inputfile file encrypt.      * @param key key use encrypting file.      * @return encrypted file.      * @throws fileencryptingexception      * @throws nosuchalgorithmexception      * @throws nosuchpaddingexception      * @throws invalidkeyexception      * @throws ioexception      * @throws invalidkeyspecexception      * @throws invalidalgorithmparameterexception      */     public static file encrypt(file inputfile, string key) throws fileencryptingexception, nosuchalgorithmexception, nosuchpaddingexception, invalidkeyexception, ioexception, invalidkeyspecexception, invalidalgorithmparameterexception{         if (isencrypted(inputfile)){             throw new fileencryptingexception("file not decrypted: "+inputfile.getabsolutepath());         }          fileinputstream inputstream = new fileinputstream(inputfile);         bufferedinputstream bufferedinputstream = new bufferedinputstream(inputstream);          file outputfile = new file(inputfile.getabsolutepath().replace(decryptedfolder,encryptedfolder) + encryptedpostfix);         outputfile.getparentfile().mkdirs();         fileoutputstream outputstream = new fileoutputstream(outputfile);         bufferedoutputstream bufferedoutputstream = new bufferedoutputstream(outputstream);         processstream(cipher.encrypt_mode,key,bufferedinputstream,bufferedoutputstream);         inputstream.close();         outputstream.close();         bufferedinputstream.close();         bufferedoutputstream.close();         return outputfile;     }      /**      * decrypt given inputfile given key.      * @param inputfile file decrypt.      * @param key key use decrypting file.      * @return decrypted file.      * @throws fileencryptingexception      * @throws ioexception      * @throws nosuchalgorithmexception      * @throws nosuchpaddingexception      * @throws invalidkeyexception      * @throws invalidkeyspecexception      * @throws invalidalgorithmparameterexception      */     public static file decrypt(file inputfile, string key) throws fileencryptingexception, ioexception, nosuchalgorithmexception, nosuchpaddingexception, invalidkeyexception, invalidkeyspecexception, invalidalgorithmparameterexception{         if (!isencrypted(inputfile)){             throw new fileencryptingexception("file not encrypted: "+inputfile.getabsolutepath());         }          if (!inputfile.exists() && !inputfile.getabsolutepath().contains(encryptedpostfix)){             inputfile = new file(inputfile.getabsolutepath()+encryptedpostfix);         }          fileinputstream inputstream = new fileinputstream(inputfile);         bufferedinputstream bufferedinputstream = new bufferedinputstream(inputstream);          file outputfile = new file(inputfile.getabsolutepath().replace(encryptedpostfix, "").replace(encryptedfolder,decryptedfolder));         outputfile.getparentfile().mkdirs();         fileoutputstream outputstream = new fileoutputstream(outputfile);         bufferedoutputstream bufferedoutputstream = new bufferedoutputstream(outputstream);         processstream(cipher.decrypt_mode,key,bufferedinputstream,bufferedoutputstream);         inputstream.close();         outputstream.close();         bufferedinputstream.close();         bufferedoutputstream.close();         return outputfile;     }      /**      * used generating cipher.      * @param ciphermode cipher mode use. either <code>cipher.decrypt_mode</code> or <code>cipher.encrypt_mode</code>      * @param key key generate cipher with.      * @return generated cipher.      * @throws nosuchalgorithmexception      * @throws invalidkeyspecexception      * @throws nosuchpaddingexception      * @throws invalidkeyexception      * @throws invalidalgorithmparameterexception      */     private static cipher getcipher(int ciphermode, string key) throws nosuchalgorithmexception, invalidkeyspecexception, nosuchpaddingexception, invalidkeyexception, invalidalgorithmparameterexception {         if (!ciphers.containskey(ciphermode)){             synchronized (ciphers){                 if (!ciphers.containskey(ciphermode)){                     ciphers.put(ciphermode, new hashmap<string,cipher>());                 }             }         }         hashmap<string, cipher> hashmap = ciphers.get(ciphermode);         if (!hashmap.containskey(key)){             synchronized (hashmap) {                 if (!hashmap.containskey(key)){                     secretkeyfactory factory = secretkeyfactory.getinstance("pbkdf2withhmacsha1");                     keyspec spec = new pbekeyspec(key.tochararray(), salt, 65536, 128);                     secretkey tmp = factory.generatesecret(spec);                     secretkey secret = new secretkeyspec(tmp.getencoded(), "rc4");                      cipher c = cipher.getinstance("rc4");                     c.init(ciphermode, secret);                     hashmap.put(key, c);                 }             }         }         return hashmap.get(key);     }      /**      * process stream. encrypt or decrypt stream, depending on given ciphermode. output available in given {@link outputstream}.      * @param ciphermode cipher mode use. either <code>cipher.decrypt_mode</code> or <code>cipher.encrypt_mode</code>      * @param key key use decryption/encryption.      * @param inputstream stream read from.      * @param outputstream stream write encrypted/decrypted result to.      * @throws invalidkeyexception      * @throws nosuchalgorithmexception      * @throws invalidkeyspecexception      * @throws nosuchpaddingexception      * @throws invalidalgorithmparameterexception      * @throws ioexception      */     public static void processstream(int ciphermode, string key, inputstream inputstream, outputstream outputstream) throws invalidkeyexception, nosuchalgorithmexception, invalidkeyspecexception, nosuchpaddingexception, invalidalgorithmparameterexception, ioexception{         cipher c = getcipher(ciphermode,key);         cipheroutputstream cipheroutputstream = new cipheroutputstream(outputstream, c);         int b = 0;         while ((b = inputstream.read()) !=-1){             cipheroutputstream.write(b);         }         cipheroutputstream.close();     }      /**      * returns file, no matter whether it's encrypted or not. see {@link #isencrypted(file)} detect if file encrypted.      * @param filenamedecrypted filename decrypted file have.      * @return file corresponds given filename.      */     public static file getfile(string filenamedecrypted){         if (!filenamedecrypted.contains(decryptedfolder) && !filenamedecrypted.contains(encryptedfolder)){             filenamedecrypted = decryptedfolder + filenamedecrypted;         }         file file = new file(filenamedecrypted);         if (!file.exists()){             file = new file(filenamedecrypted.replace(decryptedfolder, encryptedfolder)+encryptedpostfix);         }         return file;     }      /**      * checks if file encrypted or not.      * @param file file check.      * @return true if file encrypted, false otherwise.      */     public static boolean isencrypted(file file){         return file.getabsolutepath().contains(encryptedfolder) || (!encryptedpostfix.isempty() && file.getabsolutepath().contains(encryptedpostfix));     } } 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -