This file contains hidden or 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
val message = "Very large message, bigger then 250 symblos..." | |
// Simple Shared Preferences wrapper, will be used to save wrapped key | |
val storage = Storage(context) | |
// Creates Android Key Store and provides manage functions | |
val keyStoreWrapper = KeyStoreWrapper(context) | |
// Running M and later, use one symmetric key | |
if (SystemServices.hasMarshmallow()) { |
This file contains hidden or 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
companion object { | |
var TRANSFORMATION_SYMMETRIC = "AES/CBC/PKCS7Padding" | |
} |
This file contains hidden or 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
fun unWrapKey(wrappedKeyData: String, algorithm: String, wrappedKeyType: Int, keyToUnWrapWith: Key?): Key { | |
val encryptedKeyData = Base64.decode(wrappedKeyData, Base64.DEFAULT) | |
cipher.init(Cipher.UNWRAP_MODE, keyToUnWrapWith) | |
return cipher.unwrap(encryptedKeyData, algorithm, wrappedKeyType) | |
} |
This file contains hidden or 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
fun wrapKey(keyToBeWrapped: Key, keyToWrapWith: Key?): String { | |
cipher.init(Cipher.WRAP_MODE, keyToWrapWith) | |
val decodedData = cipher.wrap(keyToBeWrapped) | |
return Base64.encodeToString(decodedData, Base64.DEFAULT) | |
} |
This file contains hidden or 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
@TargetApi(23) | |
fun createAndroidKeyStoreSymmetricKey(alias: String): SecretKey { | |
val keyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore") | |
val builder = KeyGenParameterSpec.Builder(alias, KeyProperties.PURPOSE_ENCRYPT or KeyProperties.PURPOSE_DECRYPT) | |
.setBlockModes(KeyProperties.BLOCK_MODE_CBC) | |
.setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_PKCS7) | |
keyGenerator.init(builder.build()) | |
return keyGenerator.generateKey() | |
} |
This file contains hidden or 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
fun generateDefaultSymmetricKey(): SecretKey { | |
val keyGenerator = KeyGenerator.getInstance("AES", "BC") | |
return keyGenerator.generateKey() | |
} |
This file contains hidden or 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
var message = "Hello Word" | |
// Creates Android Key Store and provides manage functions | |
private val keyStoreWrapper = KeyStoreWrapper(context) | |
// Create and Save asymmetric key | |
keyStoreWrapper.createAndroidKeyStoreAsymmetricKey("MASTER_KEY") | |
// Get key from keyStore | |
var masterKey = keyStoreWrapper.getAndroidKeyStoreAsymmetricKeyPair("MASTER_KEY") |
This file contains hidden or 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
.setCertificateNotBefore(startDate) // By default, this date is Jan 1 1970. | |
.setCertificateNotAfter(endDate) // By default, this date is Jan 1 2048. | |
.setCertificateSerialNumber(number) // By default, the serial number is 1. | |
.setCertificateSubject(x500Principal) // By default, the subject is CN=fake. |
This file contains hidden or 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
fun decrypt(data: String, key: Key?): String { | |
cipher.init(Cipher.DECRYPT_MODE, key) | |
val encryptedData = Base64.decode(data, Base64.DEFAULT) | |
val decodedData = cipher.doFinal(encryptedData) | |
return String(decodedData) | |
} |
This file contains hidden or 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
fun encrypt(data: String, key: Key?): String { | |
cipher.init(Cipher.ENCRYPT_MODE, key) | |
val bytes = cipher.doFinal(data.toByteArray()) | |
return Base64.encodeToString(bytes, Base64.DEFAULT) | |
} |