0

Able to encrypt the data but after decryption not able to convert to string. No errors received. Decryption is success only but couldn't get the correct string value getting some decrpyted value.Below is the code

if (MessageVal.contains("encrypt")) {
                byte[] encrypted =result.getCryptoObject().getCipher().doFinal(message.getBytes());
                String newmessage = Base64.encodeToString(encrypted, Base64.URL_SAFE);
                SharedPreferences sharedPreferences = PreferenceManager
                       .getDefaultSharedPreferences(context);
               SharedPreferences.Editor editor = sharedPreferences.edit();
               editor.putString("messageValue", newmessage);
              editor.apply();
              Intent intent = new Intent(context, sampleAcitvity.class);
               context.startActivity(intent);


           } else if (MessageVal.contains("decrypt")) {

               SharedPreferences sharedPreferences = PreferenceManager
                       .getDefaultSharedPreferences(context);
              String MessageFrm = sharedPreferences.getString("messageValue", null);

               Toast.makeText(context, "b4value"+MessageFrm, Toast.LENGTH_LONG).show();
              byte[] dataval=  Base64.decode(MessageFrm, Base64.URL_SAFE);
                byte[] data = result.getCryptoObject().getCipher().doFinal(dataval);
               String stringDecryptyedval = new String(data, "UTF-8");

               Toast.makeText(context, "sucessfully decrpted"+stringDecryptyedval, Toast.LENGTH_LONG).show();

               Intent intent = new Intent(context, sampleAcitvity.class);
               context.startActivity(intent);
           }
ruby
  • 147
  • 2
  • 3
  • 13

1 Answers1

-1

You did not mention how you initial cipher. I guess that you did not initial your cipher properly to decryption. Check initialization of your cipher before encryption/decryption. Check algorithm-parameters of your cipher and specially check operation mode (ENCRYPT_MODE/DECRYPT_MODE). For more information see Android Developer Docs about javax.crypto.Cipher.

hadi.mansouri
  • 828
  • 11
  • 25
  • As per my understanding, we need to pass result.getCryptoObject().getCipher() to encrpt and decrprt function so that our fingerprint can be used for encrption and decrption. Pls correct me if I am wrong. – ruby Jun 19 '17 at 13:06
  • public boolean cipherInit() { cipher = Cipher.getInstance( KeyProperties.KEY_ALGORITHM_AES + "/" + KeyProperties.BLOCK_MODE_CBC + "/" + KeyProperties.ENCRYPTION_PADDING_PKCS7); keyStore.load(null); Log.e("ifbb", "keyStore"); SecretKey key = (SecretKey) keyStore.getKey(KEY_NAME, null); Log.e("ifbb", "SecretKey" + key); cipher.init(Cipher.ENCRYPT_MODE, key); return true; } this is my code for initcipher – ruby Jun 19 '17 at 13:07
  • so as you have mentioned I am giving only ENCRYPT_MODE for cipher. So how can i change the code tried this way but I got invalid key exception- – ruby Jun 19 '17 at 13:10
  • KeyStore keyStore = KeyStore.getInstance("AndroidKeyStore"); keyStore.load(null); SecretKey secretKey = (SecretKey) keyStore.getKey(KEY_NAME, null); Cipher cipher = Cipher.getInstance(KeyProperties.KEY_ALGORITHM_AES + "/" + KeyProperties.BLOCK_MODE_CBC + "/" + KeyProperties.ENCRYPTION_PADDING_PKCS7); cipher.init(Cipher.DECRYPT_MODE, secretKey); byte[] passwordBytes = cipher.doFinal(Base64.decode(MessageFrm, Base64.URL_SAFE)); String stringDecryptyedval = new String(passwordBytes, "UTF-8"); – ruby Jun 19 '17 at 13:11
  • @ruby I am now confused. Are you edit your code to init cipher for decryption? Now is your problem in decryption process or while loading key? If you have some problem about encryption/decryption process (for example getting decrypted string differ than string which was encrypted) I suggest see this tutorial: [Using Cipher to implement Cryptography in Android](http://www.cuelogic.com/blog/using-cipher-to-implement-cryptography-in-android/) – hadi.mansouri Jun 19 '17 at 16:13
  • I have edited the code with the proper decrption but now I am getting badpadding exception while decrypting – ruby Jun 21 '17 at 10:50
  • byte[] encrypted = mCipher.doFinal("Hello World".getBytes()); message = Base64.encodeToString(encrypted, Base64.URL_SAFE); – ruby Jun 21 '17 at 10:51
  • decropt- byte[] data = mCipher.doFinal(Base64.decode(message, Base64.URL_SAFE)); Log.d(TAG, "data: " + new String(data,"UTF-8")); – ruby Jun 21 '17 at 10:51
  • if (opVal.contains("encrypt")) { mCipher.init(Cipher.ENCRYPT_MODE, key); IV = Base64.encodeToString(mCipher.getIV(), Base64.URL_SAFE); }else{ mCipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(Base64.decode(IVvalue, Base64.URL_SAFE))); } – ruby Jun 21 '17 at 10:53
  • @ruby how you create your mCipher instance? When you get instance of cipher you could define padding method. For example: ipher = Cipher.getInstance("AES/CBC/NoPadding"); or cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); – hadi.mansouri Jun 21 '17 at 13:20
  • @ruby Another note that I guess may solve your problem is in converting message in BASE64 and vice versa. So see following link also: https://stackoverflow.com/questions/4580982/javax-crypto-badpaddingexception – hadi.mansouri Jun 21 '17 at 13:28