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;
}