Skip to content

Instantly share code, notes, and snippets.

@yakivmospan
Created November 7, 2017 15:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yakivmospan/2cc2c56d240955241ab2ed8b64324407 to your computer and use it in GitHub Desktop.
Save yakivmospan/2cc2c56d240955241ab2ed8b64324407 to your computer and use it in GitHub Desktop.
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")
// Creates Cipher with given transformation and provides encrypt and decrypt functions
var cipherWrapper = CipherWrapper("RSA/ECB/PKCS1Padding")
// Encrypt message with the key, using public key
var encryptedData = cipherWrapper.encrypt(data, masterKey?.public)
// Output is:
// aB9Ce9d5oM0/yloLQikOz8RovWHLmoQf3ovlCiz+D9+0/y7ZDfx6SpPYsKFIK3df079DNVIGVXIW
// 63CIUrrc7zLPMCCHCnzoeNJMqj2z0mFclluXzr5mCDJYfU/63yPeUpCPuo3y1SfXPPPNYJKhz2pq
// TugVE+rWoql9019BwTKtBy80nOE4RDQnMe6M9FWcSv/k6NyFtml9iwwtGVuRGXpSgh9humMWT0Cu
// MxzHusdIaRaviY4mQLFS+iIyRC3Riu0OxbkgTWpDs937Vfv3LSslJSo2CvwqFEnMGhkGvMdjtNhJ
// vGnpzMYN/rYWt/cer8nreURscXN7o3IR8ZtPkA==
// Decrypt message with the key, using private key
var decryptedData = cipherWrapper.decrypt(data, masterKey?.private)
// Output is:
// Hello Word
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment