Last active
February 8, 2021 20:06
-
-
Save gsandaru/b758abe3ebd6b24e599db43c2cbce1f1 to your computer and use it in GitHub Desktop.
AES and Blowfish file encryption for Android
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.os.Environment; | |
import org.junit.Test; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.security.InvalidAlgorithmParameterException; | |
import java.security.InvalidKeyException; | |
import java.security.Key; | |
import java.security.NoSuchAlgorithmException; | |
import javax.crypto.BadPaddingException; | |
import javax.crypto.Cipher; | |
import javax.crypto.IllegalBlockSizeException; | |
import javax.crypto.NoSuchPaddingException; | |
import javax.crypto.spec.IvParameterSpec; | |
import javax.crypto.spec.SecretKeySpec; | |
public class CryptoUtilsTest { | |
private static void doCryptoInAES(int cipherMode, String key, File inputFile, | |
File outputFile) throws CryptoException { | |
try { | |
Key secretKey = new SecretKeySpec(key.getBytes(), "AES"); | |
Cipher cipher = Cipher.getInstance("AES"); | |
cipher.init(cipherMode, secretKey); | |
FileInputStream inputStream = new FileInputStream(inputFile); | |
byte[] inputBytes = new byte[(int) inputFile.length()]; | |
inputStream.read(inputBytes); | |
byte[] outputBytes = cipher.doFinal(inputBytes); | |
FileOutputStream outputStream = new FileOutputStream(outputFile); | |
outputStream.write(outputBytes); | |
inputStream.close(); | |
outputStream.close(); | |
} catch (NoSuchPaddingException | NoSuchAlgorithmException | |
| InvalidKeyException | BadPaddingException | |
| IllegalBlockSizeException | IOException ex) { | |
throw new CryptoException("Error encrypting/decrypting file", ex); | |
} | |
} | |
private static void doCryptoInBlowFish(int cipherMode,String KEY,File inputFile,File outputFile) throws CryptoException{ | |
String ALGORITHM = "Blowfish"; | |
String MODE = "Blowfish/CBC/PKCS5Padding"; | |
String IV = "!a3edr45"; | |
try { | |
Key secretKey = new SecretKeySpec(KEY.getBytes(), ALGORITHM); | |
Cipher cipher = Cipher.getInstance(MODE); | |
cipher.init(cipherMode, secretKey, new IvParameterSpec(IV.getBytes())); | |
FileInputStream inputStream = new FileInputStream(inputFile); | |
byte[] inputBytes = new byte[(int) inputFile.length()]; | |
inputStream.read(inputBytes); | |
byte[] outputBytes = cipher.doFinal(inputBytes); | |
FileOutputStream outputStream = new FileOutputStream(outputFile); | |
outputStream.write(outputBytes); | |
inputStream.close(); | |
outputStream.close(); | |
} catch (NoSuchPaddingException | NoSuchAlgorithmException | |
| InvalidKeyException | BadPaddingException | |
| IllegalBlockSizeException | IOException ex) { | |
ex.fillInStackTrace(); | |
} catch (InvalidAlgorithmParameterException e) { | |
e.printStackTrace(); | |
} | |
} | |
@Test | |
public void encryptFileinAES() throws CryptoException { | |
final File saveFile = new File(Environment.getExternalStorageDirectory(), "intruder.jpg"); | |
doCryptoInAES(Cipher.ENCRYPT_MODE,"ak$#54%^RtF%g^Hf",saveFile,saveFile); | |
doCryptoInAES(Cipher.DECRYPT_MODE,"ak$#54%^RtF%g^Hf",saveFile,saveFile); | |
} | |
@Test | |
public void encryptFileinBlowfish() throws CryptoException { | |
final File saveFile = new File(Environment.getExternalStorageDirectory(), "intruder.jpg"); | |
doCryptoInBlowFish(Cipher.ENCRYPT_MODE,"SAMPLEKEY",saveFile,saveFile); | |
doCryptoInBlowFish(Cipher.DECRYPT_MODE,"SAMPLEKEY",saveFile,saveFile); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
CryptoException is just a custom exception class