Skip to content
Permalink
Browse files

cipher.update() can return null, checking for this prevents NullPoint…

…erExceptions

Null is returned when all decrypted bytes are still in the buffer
  • Loading branch information...
Stan Behrens
Stan Behrens committed Aug 13, 2015
1 parent 6a695a7 commit 0900b3b95c060d584f06714614fd93f7a0ed0c1f
Binary file not shown.
@@ -8,6 +8,7 @@
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.Arrays;

import javax.crypto.Cipher;

@@ -65,8 +66,15 @@ protected void onActivityResult(int requestCode, int resultCode,

cipher.init(Cipher.DECRYPT_MODE, KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(key)));

String decrypted = new String(cipher.update(encrypted), "UTF-8")
+ new String(cipher.doFinal(), "UTF-8");
byte[] decrypted0 = cipher.update(encrypted);
byte[] decrypted1 = cipher.doFinal();
if(decrypted0 == null)
decrypted0 = decrypted1;
else {
decrypted0 = Arrays.copyOf(decrypted0, decrypted0.length + decrypted1.length);
System.arraycopy(decrypted1, 0, decrypted0, decrypted0.length - decrypted1.length, decrypted1.length);
}
String decrypted = new String(decrypted0, "UTF-8");
secret.setText(decrypted);
} catch (Exception e) {
Log.e("", "Decrypting error", e);

0 comments on commit 0900b3b

Please sign in to comment.
You can’t perform that action at this time.