Encryption of strings using AES 128 in Java/grails -


i encrypt 3 strings using aes 128 in java / grails, , using code below, error "an error occurred when encrypting", can tell me wrong code, how fix it. in advance , stackoverflow.

 string url = "https://someurl.com"   string token = createtoken(booknumber, invnumber, cusnumber)   url += '?ref=' + token  class aesencryptor {      static byte[] encrypt(string cleartext) {             byte[] encrypted = null             try {                 byte[] iv = new byte[16]                 arrays.fill(iv, (byte) 0)                  cipher cipher = cipher.getinstance("aes/cbc/pkcs5padding")                 encrypted = cipher.dofinal(cleartext.getbytes("utf-8"))             }             catch (exception e) {                 log.error "an error occurred when encrypting", e             }             encrypted         }        /**          * creates token.          * @return          */         static string createtoken(final string booknumber, final string invnumber, final string cusnumber) {             string data =  booknumber + invnumber + cusnumber             string token = urlencoder.encode(base64.encodebase64string(encrypt(data)), "utf-8")             token         } } 

the error get:

java.lang.illegalstateexception: cipher not initialized     @ javax.crypto.cipher.checkcipherstate(cipher.java:1672)     @ javax.crypto.cipher.dofinal(cipher.java:2079)     @ javax.crypto.cipher$dofinal$1.call(unknown source) 

cipher.init method call missed in code. check below code.

public byte[] encrypt(byte[] data, byte[] key) {   cipher cipher = cipher.getinstance("aes/cbc/pkcs5padding");   cipher.init(cipher.encrypt_mode, new secretkeyspec(key, "aes"));   return cipher.dofinal(data); } 

for decrypt have change mode cipher.decrypt_mode


Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

firemonkey - How do I make a beep sound in Android using Delphi and the API? -