Skip to content

Commit

Permalink
add byte encode.
Browse files Browse the repository at this point in the history
remove string encode.
builder pattern refactoring.
  • Loading branch information
Kensuke Tamura committed Jan 19, 2017
1 parent 1bc9586 commit fa96068
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 98 deletions.
10 changes: 10 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/inspectionProfiles/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.2'
classpath 'com.android.tools.build:gradle:2.2.3'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
2 changes: 1 addition & 1 deletion library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apply plugin: 'com.android.library'

android {
compileSdkVersion 25
buildToolsVersion "25.0.0"
buildToolsVersion "25.0.2"

defaultConfig {
minSdkVersion 18
Expand Down
16 changes: 7 additions & 9 deletions library/src/main/java/com/kazakago/cryptore/AESCryptore.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import android.os.Build;
import android.security.keystore.KeyGenParameterSpec;
import android.security.keystore.KeyProperties;
import android.util.Base64;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
Expand Down Expand Up @@ -63,37 +62,36 @@ public void initCipher() throws NoSuchPaddingException, NoSuchAlgorithmException
}

@Override
public String encryptString(String plainText) throws UnrecoverableKeyException, NoSuchAlgorithmException, KeyStoreException, InvalidKeyException, IOException {
public byte[] encrypt(byte[] plainByte) throws UnrecoverableKeyException, NoSuchAlgorithmException, KeyStoreException, InvalidKeyException, IOException {
SecretKey secretKey = (SecretKey) keyStore.getKey(alias, null);

cipher.init(Cipher.ENCRYPT_MODE, secretKey);
cipherIV = cipher.getIV();

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
CipherOutputStream cipherOutputStream = new CipherOutputStream(outputStream, cipher);
cipherOutputStream.write(plainText.getBytes("UTF-8"));
cipherOutputStream.write(plainByte);
cipherOutputStream.close();

byte[] bytes = outputStream.toByteArray();
return Base64.encodeToString(bytes, Base64.DEFAULT);
return outputStream.toByteArray();
}

@Override
public String decryptString(String encryptedText) throws UnrecoverableKeyException, NoSuchAlgorithmException, KeyStoreException, InvalidAlgorithmParameterException, InvalidKeyException, IOException {
public byte[] decrypt(byte[] encryptedByte) throws UnrecoverableKeyException, NoSuchAlgorithmException, KeyStoreException, InvalidAlgorithmParameterException, InvalidKeyException, IOException {
SecretKey secretKey = (SecretKey) keyStore.getKey(alias, null);

cipher.init(Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(cipherIV));
cipherIV = cipher.getIV();

CipherInputStream cipherInputStream = new CipherInputStream(new ByteArrayInputStream(Base64.decode(encryptedText, Base64.DEFAULT)), cipher);

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
CipherInputStream cipherInputStream = new CipherInputStream(new ByteArrayInputStream(encryptedByte), cipher);
int b;
while ((b = cipherInputStream.read()) != -1) {
outputStream.write(b);
}
outputStream.close();
return outputStream.toString("UTF-8");

return outputStream.toByteArray();
}

@Override
Expand Down
114 changes: 60 additions & 54 deletions library/src/main/java/com/kazakago/cryptore/Cryptore.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,20 @@ public interface Cryptore {
void initCipher() throws NoSuchPaddingException, NoSuchAlgorithmException, NoSuchProviderException;

/**
* Encrypt string text.
* Encrypt byte.
*
* @param plainText string to be encrypted
* @return base64 encoded cipher text
* @param plainByte byte to be encrypted
* @return cipher byte
*/
String encryptString(String plainText) throws KeyStoreException, NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, IOException, NoSuchProviderException, InvalidAlgorithmParameterException, UnrecoverableEntryException;
byte[] encrypt(byte[] plainByte) throws KeyStoreException, NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, IOException, NoSuchProviderException, InvalidAlgorithmParameterException, UnrecoverableEntryException;

/**
* Decrypt base64 encoded cipher text.
* Decrypt byte.
*
* @param encryptedText base64 encoded cipher text
* @return plain text string
* @param encryptedByte cipher byte
* @return plain byte
*/
String decryptString(String encryptedText) throws KeyStoreException, NoSuchProviderException, NoSuchAlgorithmException, UnrecoverableEntryException, NoSuchPaddingException, InvalidKeyException, IOException, InvalidAlgorithmParameterException;
byte[] decrypt(byte[] encryptedByte) throws KeyStoreException, NoSuchProviderException, NoSuchAlgorithmException, UnrecoverableEntryException, NoSuchPaddingException, InvalidKeyException, IOException, InvalidAlgorithmParameterException;

/**
* Create new key pair.
Expand All @@ -71,80 +71,86 @@ public interface Cryptore {

class Builder {

private static final CipherAlgorithm CIPHER_ALGORITHM_DEFAULT = CipherAlgorithm.RSA;
private static final String BLOCK_MODE_DEFAULT__AES = CipherProperties.BLOCK_MODE_CBC;
private static final String ENCRYPTION_PADDING_DEFAULT__AES = CipherProperties.ENCRYPTION_PADDING_PKCS7;
private static final String BLOCK_MODE_DEFAULT__RSA = CipherProperties.BLOCK_MODE_ECB;
private static final String ENCRYPTION_PADDING_DEFAULT__RSA = CipherProperties.ENCRYPTION_PADDING_RSA_PKCS1;

protected String alias;
protected CipherAlgorithm type;
protected Context context;
protected String blockMode;
protected String encryptionPadding;
protected String alias;
protected Context context;

public Builder type(CipherAlgorithm type) {
public Builder(String alias) {
this(alias, CIPHER_ALGORITHM_DEFAULT);
}

public Builder(String alias, CipherAlgorithm type) {
this.alias = alias;
this.type = type;
switch (type) {
case RSA:
blockMode = BLOCK_MODE_DEFAULT__RSA;
encryptionPadding = ENCRYPTION_PADDING_DEFAULT__RSA;
break;
case AES:
blockMode = BLOCK_MODE_DEFAULT__AES;
encryptionPadding = ENCRYPTION_PADDING_DEFAULT__AES;
break;
default:
throw new IllegalArgumentException("Unsupported Algorithm.");
}
}

public Builder alias(String alias) {
this.alias = alias;
return this;
}

public Builder blockMode(String blockMode) {
this.blockMode = blockMode;
public Builder type(CipherAlgorithm type) {
this.type = type;
return this;
}

public Builder encryptionPadding(String encryptionPadding) {
this.encryptionPadding = encryptionPadding;
public Builder context(Context context) {
this.context = context;
return this;
}

public Builder alias(String alias) {
this.alias = alias;
public Builder blockMode(String blockMode) {
this.blockMode = blockMode;
return this;
}

public Builder context(Context context) {
this.context = context;
public Builder encryptionPadding(String encryptionPadding) {
this.encryptionPadding = encryptionPadding;
return this;
}

public Cryptore build() throws CertificateException, NoSuchAlgorithmException, KeyStoreException, NoSuchProviderException, InvalidAlgorithmParameterException, IOException, NoSuchPaddingException {
if (alias == null || alias.length() == 0) {
throw new NullPointerException("Need \"alias\".");
}
if (type == null) {
throw new NullPointerException("Need \"type\".");
} else if (type == CipherAlgorithm.AES) {
if (blockMode == null) {
blockMode = BLOCK_MODE_DEFAULT__AES;
}
if (encryptionPadding == null) {
encryptionPadding = ENCRYPTION_PADDING_DEFAULT__AES;
}
if (Build.VERSION_CODES.M <= Build.VERSION.SDK_INT) {
return new AESCryptore(this);
} else {
throw new NoSuchAlgorithmException("AES is support only above API Lv23.");
}
} else if (type == CipherAlgorithm.RSA) {
if (blockMode == null) {
blockMode = BLOCK_MODE_DEFAULT__RSA;
}
if (encryptionPadding == null) {
encryptionPadding = ENCRYPTION_PADDING_DEFAULT__RSA;
}
if (Build.VERSION_CODES.M <= Build.VERSION.SDK_INT) {
return new RSACryptoreM(this);
} else if (Build.VERSION_CODES.JELLY_BEAN_MR2 <= Build.VERSION.SDK_INT) {
if (context == null) {
throw new NullPointerException("Need \"Context\" for RSA on below API Lv22");
switch (type) {
case AES:
if (Build.VERSION_CODES.M <= Build.VERSION.SDK_INT) {
return new AESCryptore(this);
} else {
throw new NoSuchAlgorithmException("AES is support only above API Lv23.");
}
case RSA:
if (Build.VERSION_CODES.M <= Build.VERSION.SDK_INT) {
return new RSACryptoreM(this);
} else if (Build.VERSION_CODES.JELLY_BEAN_MR2 <= Build.VERSION.SDK_INT) {
if (context != null) {
return new RSACryptore(this);
} else {
throw new NullPointerException("Need \"Context\" for RSA on below API Lv22");
}
} else {
return new RSACryptore(this);
throw new NoSuchAlgorithmException("RSA is support only above API Lv18.");
}
} else {
throw new NoSuchAlgorithmException("RSA is support only above API Lv18.");
}
} else {
throw new IllegalArgumentException("Unsupported Algorithm.");
default:
throw new IllegalArgumentException("Unsupported Algorithm.");
}
}

Expand Down
16 changes: 7 additions & 9 deletions library/src/main/java/com/kazakago/cryptore/RSACryptore.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import android.content.Context;
import android.os.Build;
import android.security.KeyPairGeneratorSpec;
import android.util.Base64;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
Expand Down Expand Up @@ -68,37 +67,36 @@ public void initCipher() throws NoSuchPaddingException, NoSuchAlgorithmException
}

@Override
public String encryptString(String plainText) throws KeyStoreException, InvalidKeyException, IOException {
public byte[] encrypt(byte[] plainByte) throws KeyStoreException, InvalidKeyException, IOException {
PublicKey publicKey = keyStore.getCertificate(alias).getPublicKey();

cipher.init(Cipher.ENCRYPT_MODE, publicKey);
cipherIV = cipher.getIV();

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
CipherOutputStream cipherOutputStream = new CipherOutputStream(outputStream, cipher);
cipherOutputStream.write(plainText.getBytes("UTF-8"));
cipherOutputStream.write(plainByte);
cipherOutputStream.close();

byte[] bytes = outputStream.toByteArray();
return Base64.encodeToString(bytes, Base64.DEFAULT);
return outputStream.toByteArray();
}

@Override
public String decryptString(String encryptedText) throws UnrecoverableKeyException, NoSuchAlgorithmException, KeyStoreException, InvalidKeyException, IOException {
public byte[] decrypt(byte[] encryptedByte) throws UnrecoverableKeyException, NoSuchAlgorithmException, KeyStoreException, InvalidKeyException, IOException {
PrivateKey privateKey = (PrivateKey) keyStore.getKey(alias, null);

cipher.init(Cipher.DECRYPT_MODE, privateKey);
cipherIV = cipher.getIV();

CipherInputStream cipherInputStream = new CipherInputStream(new ByteArrayInputStream(Base64.decode(encryptedText, Base64.DEFAULT)), cipher);

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
CipherInputStream cipherInputStream = new CipherInputStream(new ByteArrayInputStream(encryptedByte), cipher);
int b;
while ((b = cipherInputStream.read()) != -1) {
outputStream.write(b);
}
outputStream.close();
return outputStream.toString("UTF-8");

return outputStream.toByteArray();
}

@Override
Expand Down
16 changes: 7 additions & 9 deletions library/src/main/java/com/kazakago/cryptore/RSACryptoreM.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import android.os.Build;
import android.security.keystore.KeyGenParameterSpec;
import android.security.keystore.KeyProperties;
import android.util.Base64;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
Expand Down Expand Up @@ -63,37 +62,36 @@ public void initCipher() throws NoSuchPaddingException, NoSuchAlgorithmException
}

@Override
public String encryptString(String plainText) throws KeyStoreException, InvalidKeyException, IOException {
public byte[] encrypt(byte[] plainByte) throws KeyStoreException, InvalidKeyException, IOException {
PublicKey publicKey = keyStore.getCertificate(alias).getPublicKey();

cipher.init(Cipher.ENCRYPT_MODE, publicKey);
cipherIV = cipher.getIV();

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
CipherOutputStream cipherOutputStream = new CipherOutputStream(outputStream, cipher);
cipherOutputStream.write(plainText.getBytes("UTF-8"));
cipherOutputStream.write(plainByte);
cipherOutputStream.close();

byte[] bytes = outputStream.toByteArray();
return Base64.encodeToString(bytes, Base64.DEFAULT);
return outputStream.toByteArray();
}

@Override
public String decryptString(String encryptedText) throws UnrecoverableKeyException, NoSuchAlgorithmException, KeyStoreException, InvalidKeyException, IOException {
public byte[] decrypt(byte[] encryptedByte) throws UnrecoverableKeyException, NoSuchAlgorithmException, KeyStoreException, InvalidKeyException, IOException {
PrivateKey privateKey = (PrivateKey) keyStore.getKey(alias, null);

cipher.init(Cipher.DECRYPT_MODE, privateKey);
cipherIV = cipher.getIV();

CipherInputStream cipherInputStream = new CipherInputStream(new ByteArrayInputStream(Base64.decode(encryptedText, Base64.DEFAULT)), cipher);

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
CipherInputStream cipherInputStream = new CipherInputStream(new ByteArrayInputStream(encryptedByte), cipher);
int b;
while ((b = cipherInputStream.read()) != -1) {
outputStream.write(b);
}
outputStream.close();
return outputStream.toString("UTF-8");

return outputStream.toByteArray();
}

@Override
Expand Down
6 changes: 3 additions & 3 deletions sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apply plugin: 'com.android.application'

android {
compileSdkVersion 25
buildToolsVersion "25.0.0"
buildToolsVersion "25.0.2"

defaultConfig {
applicationId "com.kazakago.cryptore"
Expand All @@ -22,7 +22,7 @@ android {
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:25.0.1'
compile 'com.android.support:design:25.0.1'
compile 'com.android.support:appcompat-v7:25.1.0'
compile 'com.android.support:design:25.1.0'
compile project(path: ':library')
}
Loading

0 comments on commit fa96068

Please sign in to comment.