KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I'm trying to encrypt/decrypt a String in Java. No problem concerning the encryption then stored a in sqlite table. But I always get the same error trying to decrypt it : "java.security.InvalidKeyException : no IV set when one expected" Here is my code snippet : public String encrypt(String password){ try { String key = "mysecretpassword"; SecretKeySpec keySpec = null; keySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding"); cipher.init(Cipher.ENCRYPT_MODE, keySpec); return new String(cipher.doFinal(password.getBytes())); } catch (Exception e) { return null; } } public String decrypt(String password){ try { String key = "mysecretpassword"; SecretKeySpec keySpec = null; keySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding"); cipher.init(Cipher.DECRYPT_MODE,keySpec); return new String(cipher.doFinal(password.getBytes())); } catch (Exception e) { System.out.println(e); return null; } } What am I doing wrong?
Tags (comma-separated)
Save Edits
Cancel