java - Received byte array has different values than sent, AES encryption + IV -


i'm trying encrypt communication using aes, in java.

the key hardcoded, , iv generated randomly through securerandom , sent first 16 bytes of encrypted message. however, when try read first 16 bytes of received message, don't same byte array generated.

here's problematic code:

static byte[] bytes = new byte[16];  public static byte[] encrypt(string key, string message) {     try {         securerandom random = new securerandom();         random.nextbytes(bytes);          system.out.println("outputting generated iv:");         for(int i=0; < bytes.length; i++){             system.out.println(bytes[i]);         }          ivparameterspec iv = new ivparameterspec(bytes);         secretkeyspec skeyspec = new secretkeyspec(key.getbytes("utf-8"), "aes");          cipher cipher = cipher.getinstance("aes/cbc/pkcs5padding");         cipher.init(cipher.encrypt_mode, skeyspec, iv);          byte[] encrypted = base64.encodebase64(cipher.dofinal(message.getbytes()));         system.out.println("encrypted string: "                 + base64.encodebase64string(encrypted));          byte[] sendme = new byte[bytes.length + encrypted.length];         system.arraycopy(bytes, 0, sendme, 0, bytes.length);         system.arraycopy(encrypted, 0, sendme, 0, encrypted.length);          return  sendme;     } catch (exception ex) {         ex.printstacktrace();     }      return null; }  public static string decrypt(string key, byte[] received) {     try {          byte[] initvector = arrays.copyofrange(received, 0, 16);         byte[] encrypted = arrays.copyofrange(received, 16, received.length+1);          system.out.println("outputting received iv:");         for(int = 0; < initvector.length; i++){             system.out.println(initvector[i]);         }          ivparameterspec iv = new ivparameterspec(initvector);         secretkeyspec skeyspec = new secretkeyspec(key.getbytes("utf-8"), "aes");          cipher cipher = cipher.getinstance("aes/cbc/pkcs5padding");         cipher.init(cipher.decrypt_mode, skeyspec, iv);          byte[] original = cipher.dofinal(base64.decodebase64(encrypted));          return new string(original);     } catch (exception ex) {         ex.printstacktrace();     }      return null; } 

running once, example's sake, text "hello world!" yielded following output:

outputting generated iv: -79 -3 102 -103 -13 67 -63 -18 23 -114 74 26 18 -97 77 115 outputting received iv: 36 -118 -87 -72 -119 43 101 55 50 -62 125 -98 65 35 48 -87 

which not same.

any appreciated.

you overwriting iv encrypted data:

 system.arraycopy(bytes, 0, sendme, 0, bytes.length);  system.arraycopy(encrypted, 0, sendme, 0, encrypted.length);  // overwrites iv 

you want:

 system.arraycopy(bytes, 0, sendme, 0, bytes.length);  system.arraycopy(encrypted, 0, sendme, 16, encrypted.length);   

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -