From 1a4ac65923d311a41927b2f995992c33eea37208 Mon Sep 17 00:00:00 2001 From: Brian Plummer Date: Thu, 27 Nov 2014 23:03:01 -0500 Subject: [PATCH] stopping point --- .../app/AESEncryptDecrypt.java | 72 +++++++++++++++---- .../app/EncryptionActivity.java | 4 +- .../encryptionsexample/EncryptionTest.java | 19 +++-- 3 files changed, 73 insertions(+), 22 deletions(-) diff --git a/app/src/main/java/com/codemonkeylabs/encryptionexample/app/AESEncryptDecrypt.java b/app/src/main/java/com/codemonkeylabs/encryptionexample/app/AESEncryptDecrypt.java index 4eb081b..664e73c 100644 --- a/app/src/main/java/com/codemonkeylabs/encryptionexample/app/AESEncryptDecrypt.java +++ b/app/src/main/java/com/codemonkeylabs/encryptionexample/app/AESEncryptDecrypt.java @@ -25,38 +25,77 @@ public class AESEncryptDecrypt { public static final String NOT_SECRET_ENCRYPTION_KEY = "1234567812345678"; //Must be 16 bytes long....getBytes defaults to utf-8 public static final String IVS = "1234567812345678"; - - public static final String AES_CIPHER = "AES/CTR/NoPadding"; public static final String AES = "AES"; - public String encrypt(String inData, byte[] key, byte[] ivs) + public enum AESCipherType { + AES_CIPHER_CTR_NOPADDING("AES/CTR/NOPADDING"), + AES_CIPHER_ECB_PKCS5PADDING("AES/ECB/PKCS5PADDING"); + + private final String value; + + AESCipherType(String value) { + this.value = value; + } + + public String getValue() { + return value; + } + } + + + + public String encryptCTR(String inData, byte[] key, byte[] ivs) { byte[] encryptedData = aesEncrypt(inData.getBytes(), key, - ivs); + ivs, + AESCipherType.AES_CIPHER_CTR_NOPADDING); return new String(Base64.encode(encryptedData)); } - public String decrypt(String inData, byte[] key, byte[] ivs) + public String decryptCTR(String inData, byte[] key, byte[] ivs) { byte[] decryptData = aesDecrypt(Base64.decode(inData.toCharArray()), key, - ivs); + ivs, + AESCipherType.AES_CIPHER_CTR_NOPADDING); + return new String(decryptData); + } + + public String encryptECB(String inData, byte[] key) + { + byte[] encryptedData = aesEncrypt(inData.getBytes(), + key, + null, + AESCipherType.AES_CIPHER_ECB_PKCS5PADDING); + return new String(Base64.encode(encryptedData)); + } + + public String decryptECB(String inData, byte[] key) + { + byte[] decryptData = aesDecrypt(Base64.decode(inData.toCharArray()), + key, + null, + AESCipherType.AES_CIPHER_ECB_PKCS5PADDING); return new String(decryptData); } public static final int BYTE_BUFFER_SIZE = 1024 * 100;//100k - public static byte[] aesEncrypt(byte[] data, byte[] key, byte[] ivs) + public static byte[] aesEncrypt(byte[] data, byte[] key, byte[] ivs, AESCipherType aesCipherType) { CipherOutputStream cos = null; ByteArrayOutputStream bos = null; try { - Cipher cipher = Cipher.getInstance(AES_CIPHER); + Cipher cipher = Cipher.getInstance(aesCipherType.getValue()); SecretKeySpec secretKeySpec = new SecretKeySpec(key, AES); - IvParameterSpec ivps = new IvParameterSpec(ivs); - cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivps); + if (ivs != null) { + IvParameterSpec ivps = new IvParameterSpec(ivs); + cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivps); + } else { + cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec); + } bos = new ByteArrayOutputStream(BYTE_BUFFER_SIZE); cos = new CipherOutputStream(bos,cipher); @@ -81,17 +120,20 @@ public static byte[] aesEncrypt(byte[] data, byte[] key, byte[] ivs) } } - public static byte[] aesDecrypt(byte[] data, byte[] key, byte[] ivs) + public static byte[] aesDecrypt(byte[] data, byte[] key, byte[] ivs, AESCipherType aesCipherType) { byte[] retData = null; CipherInputStream cis = null; try { - Cipher cipher = Cipher.getInstance(AES_CIPHER); + Cipher cipher = Cipher.getInstance(aesCipherType.getValue()); SecretKeySpec secretKeySpec = new SecretKeySpec(key, AES); - IvParameterSpec ivps = new IvParameterSpec(ivs); - cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivps); - + if (ivs != null) { + IvParameterSpec ivps = new IvParameterSpec(ivs); + cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivps); + } else { + cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec); + } ByteArrayInputStream bis = new ByteArrayInputStream(data); cis = new CipherInputStream(bis,cipher); ByteArrayOutputStream bos = new ByteArrayOutputStream(BYTE_BUFFER_SIZE); diff --git a/app/src/main/java/com/codemonkeylabs/encryptionexample/app/EncryptionActivity.java b/app/src/main/java/com/codemonkeylabs/encryptionexample/app/EncryptionActivity.java index a9a741d..4cd581c 100644 --- a/app/src/main/java/com/codemonkeylabs/encryptionexample/app/EncryptionActivity.java +++ b/app/src/main/java/com/codemonkeylabs/encryptionexample/app/EncryptionActivity.java @@ -98,7 +98,7 @@ private void decryptButton(){ byte[] aesKey = Arrays.copyOfRange(decryptedAESKeyIVS, 0, 16); byte[] ivs = Arrays.copyOfRange(decryptedAESKeyIVS, 16, 32); - this.decryptedText.setText(this.aesEncryptDecrypt.decrypt(encText, aesKey, ivs)); + this.decryptedText.setText(this.aesEncryptDecrypt.decryptCTR(encText, aesKey, ivs)); } } @@ -121,7 +121,7 @@ private void encryptButton() } //encrypt the inputted text using AES - String encryptedText = aesEncryptDecrypt.encrypt(inputtedUnencryptedText, + String encryptedText = aesEncryptDecrypt.encryptCTR(inputtedUnencryptedText, AESEncryptDecrypt.NOT_SECRET_ENCRYPTION_KEY.getBytes(), AESEncryptDecrypt.IVS.getBytes()); diff --git a/app/src/test/java/com/codemonkeylabs/encryptionsexample/EncryptionTest.java b/app/src/test/java/com/codemonkeylabs/encryptionsexample/EncryptionTest.java index b2f04a7..5588b90 100644 --- a/app/src/test/java/com/codemonkeylabs/encryptionsexample/EncryptionTest.java +++ b/app/src/test/java/com/codemonkeylabs/encryptionsexample/EncryptionTest.java @@ -36,18 +36,27 @@ public void tearDown() throws Exception } @Test - public void testAESEncryption() + public void testAESEncryptionCTR() { AESEncryptDecrypt aesEncryptDecrypt = new AESEncryptDecrypt(); - String encryptedString = aesEncryptDecrypt.encrypt(testText,AESEncryptDecrypt.NOT_SECRET_ENCRYPTION_KEY.getBytes(), AESEncryptDecrypt.IVS.getBytes()); - String unencryptedString = aesEncryptDecrypt.decrypt(encryptedString,AESEncryptDecrypt.NOT_SECRET_ENCRYPTION_KEY.getBytes(), AESEncryptDecrypt.IVS.getBytes()); + String encryptedString = aesEncryptDecrypt.encryptCTR(testText, AESEncryptDecrypt.NOT_SECRET_ENCRYPTION_KEY.getBytes(), AESEncryptDecrypt.IVS.getBytes()); + String unencryptedString = aesEncryptDecrypt.decryptCTR(encryptedString, AESEncryptDecrypt.NOT_SECRET_ENCRYPTION_KEY.getBytes(), AESEncryptDecrypt.IVS.getBytes()); assertTrue(unencryptedString.startsWith("All this while Tashtego, Daggoo, and Queequeg")); } @Test - public void testRSAandAESEncryption() + public void testAESEncryptionECB() { AESEncryptDecrypt aesEncryptDecrypt = new AESEncryptDecrypt(); + String encryptedString = aesEncryptDecrypt.encryptECB(testText, AESEncryptDecrypt.NOT_SECRET_ENCRYPTION_KEY.getBytes()); + String unencryptedString = aesEncryptDecrypt.decryptECB(encryptedString, AESEncryptDecrypt.NOT_SECRET_ENCRYPTION_KEY.getBytes()); + assertTrue(unencryptedString.startsWith("All this while Tashtego, Daggoo, and Queequeg")); + } + + @Test + public void testRSAandAESEncryption() + { + /* AESEncryptDecrypt aesEncryptDecrypt = new AESEncryptDecrypt(); RSAEncryptDecrypt rsaEncryptDecrypt = new RSAEncryptDecrypt(); String encryptedString = aesEncryptDecrypt.encrypt(testText,AESEncryptDecrypt.NOT_SECRET_ENCRYPTION_KEY.getBytes(), AESEncryptDecrypt.IVS.getBytes()); @@ -61,7 +70,7 @@ public void testRSAandAESEncryption() byte[] ivs = Arrays.copyOfRange(unencryptedAESKey, 16, 32); String unencryptedString = aesEncryptDecrypt.decrypt(encryptedString, aesKey, ivs); - assertTrue(unencryptedString.startsWith("All this while Tashtego, Daggoo, and Queequeg")); + assertTrue(unencryptedString.startsWith("All this while Tashtego, Daggoo, and Queequeg"));*/ } //helper function that concats two byte arrays