Created
October 31, 2018 14:48
-
-
Save jetrubyshared/51d7fc31e1a4f9f8124b1e62e5b58607 to your computer and use it in GitHub Desktop.
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
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