Skip to content

Instantly share code, notes, and snippets.

@jetrubyshared
Created October 31, 2018 14:48
Show Gist options
  • Save jetrubyshared/51d7fc31e1a4f9f8124b1e62e5b58607 to your computer and use it in GitHub Desktop.
Save jetrubyshared/51d7fc31e1a4f9f8124b1e62e5b58607 to your computer and use it in GitHub Desktop.
private static boolean initCipherMode(int mode) {
try {
sKeyStore.load(null);
switch (mode) {
case Cipher.ENCRYPT_MODE:
PublicKey key = sKeyStore.getCertificate(KEY_ALIAS).getPublicKey();
PublicKey unrestricted = KeyFactory.getInstance(key.getAlgorithm()).generatePublic(new X509EncodedKeySpec(key.getEncoded()));
OAEPParameterSpec spec = new OAEPParameterSpec("SHA-256", "MGF1", MGF1ParameterSpec.SHA1, PSource.PSpecified.DEFAULT);
sCipher.init(mode, unrestricted, spec);
break;
case Cipher.DECRYPT_MODE:
PrivateKey privateKey = (PrivateKey) sKeyStore.getKey(KEY_ALIAS, null);
sCipher.init(mode, privateKey);
break;
default:
return false;
}
return true;
} catch (KeyStoreException | CertificateException | UnrecoverableKeyException | IOException | NoSuchAlgorithmException | InvalidKeyException | InvalidAlgorithmParameterException | InvalidKeySpecException e) {
e.printStackTrace();
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment