Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Cipher cipher = null;
- String transformation = KeyProperties.KEY_ALGORITHM_AES + "/"
- + KeyProperties.BLOCK_MODE_CBC + "/"
- + KeyProperties.ENCRYPTION_PADDING_PKCS7;
- try {
- cipher = Cipher.getInstance(transformation);
- } catch (NoSuchAlgorithmException e) {
- e.printStackTrace();
- } catch (NoSuchPaddingException e) {
- e.printStackTrace();
- }
- try {
- String message = "This is the message i want send";
- byte[] messageByte = message.getBytes();
- //get the keystore instance, do we can get a key from it
- keyStore.load(null);
- SecretKey secretKey = (SecretKey) keyStore.getKey(KEY_STORE_NAME, null);
- //get the cipher to use this secret key to encrypt the message
- if (cipher != null) {
- cipher.init(Cipher.ENCRYPT_MODE, secretKey);
- cipher.doFinal(messageByte);
- }
- } catch (CertificateException e) {
- e.printStackTrace();
- } catch (NoSuchAlgorithmException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } catch (UnrecoverableKeyException e) {
- e.printStackTrace();
- } catch (KeyStoreException e) {
- e.printStackTrace();
- } catch (InvalidKeyException e) {
- e.printStackTrace();
- } catch (BadPaddingException e) {
- e.printStackTrace();
- } catch (IllegalBlockSizeException e) {
- e.printStackTrace();
- }
- return cipher;
Add Comment
Please, Sign In to add comment