exception - Android, why IllegalBlockSizeException occur when i want to encrypt a string? -
i building android apps using fingerprint authentication, source code of generating key , cipher initialization encryption in fingerprint authentication (in fingerprintactivity).
public final void generatekey() { try { keystore = keystore.getinstance("androidkeystore"); } catch (exception e) { e.printstacktrace(); } keygenerator keygenerator; try { keygenerator = keygenerator.getinstance(keyproperties.key_algorithm_aes, "androidkeystore"); } catch (nosuchalgorithmexception | nosuchproviderexception e) { throw new runtimeexception("failed keygenerator instance", e); } try { keystore.load(null); keygenerator.init(new keygenparameterspec.builder(key_name, keyproperties.purpose_encrypt | keyproperties.purpose_decrypt) .setblockmodes(keyproperties.block_mode_cbc) .setuserauthenticationrequired(true) .setencryptionpaddings( keyproperties.encryption_padding_pkcs7) .setkeysize(256) .build()); keygenerator.generatekey(); } catch (nosuchalgorithmexception | invalidalgorithmparameterexception | certificateexception | ioexception e) { throw new runtimeexception(e); } } @targetapi(build.version_codes.m) public boolean cipherinit() { try { cipher = cipher.getinstance(keyproperties.key_algorithm_aes + "/" + keyproperties.block_mode_cbc + "/" + keyproperties.encryption_padding_pkcs7); } catch (nosuchalgorithmexception | nosuchpaddingexception e) { throw new runtimeexception("failed cipher", e); } try { keystore.load(null); secretkey key = (secretkey) keystore.getkey(key_name, null); cipher.init(cipher.encrypt_mode, key); return true; } catch (keypermanentlyinvalidatedexception e) { return false; } catch (keystoreexception | certificateexception | unrecoverablekeyexception | ioexception | nosuchalgorithmexception | invalidkeyexception e) { throw new runtimeexception("failed init cipher", e); } }
then want reuse generated key @ activity (homeactivity, activity after fingerprint authentication succed) , take key this.
private static final string key_name = "androidhive"; private keystore keystore; private cipher cipher; string test = "abc"; byte[] encryptedbyte; textview textview = (textview) findviewbyid(r.id.test); try { keystore = keystore.getinstance("androidkeystore"); } catch (exception e) { e.printstacktrace(); } try { cipher = cipher.getinstance(keyproperties.key_algorithm_aes + "/" + keyproperties.block_mode_cbc + "/" + keyproperties.encryption_padding_pkcs7); } catch (nosuchalgorithmexception | nosuchpaddingexception e) { throw new runtimeexception("failed cipher", e); } try { keystore.load(null); secretkey key = (secretkey) keystore.getkey(key_name, null); cipher.init(cipher.encrypt_mode, key); encryptedbyte = cipher.dofinal(test.getbytes()); } catch (keypermanentlyinvalidatedexception e) { textview.settext("a"); } catch (keystoreexception | certificateexception | unrecoverablekeyexception | ioexception | nosuchalgorithmexception | invalidkeyexception e) { throw new runtimeexception("failed init cipher", e); } catch (badpaddingexception e) { e.printstacktrace(); } catch (illegalblocksizeexception e) { e.printstacktrace(); textview.settext("c"); }
i set textview text know exception program got, , got it's illegal block size exception. know how fix ? credits : androidhive (its code , want develop further)
Comments
Post a Comment