java - Decode UTF-8 String -


import java.nio.charset.charset; import java.security.messagedigest; import java.security.security; import javax.crypto.cipher; import javax.crypto.spec.ivparameterspec; import javax.crypto.spec.secretkeyspec; import org.apache.commons.codec.binary.base64;  public class cryptolib {      public cryptolib() {         security.addprovider(new org.bouncycastle.jce.provider.bouncycastleprovider());     }      public string encrypt(string plaintext, string key) throws exception {          // convert key bytes         messagedigest md = messagedigest.getinstance("md5");         md.update(key.getbytes("utf-8"));          byte[] keybytes = md.digest();          // use first 16 bytes (or less if key shorter)         byte[] keybytes16 = new byte[16];         system.arraycopy(keybytes, 0, keybytes16, 0, math.min(keybytes.length, 64));          system.arraycopy(keybytes, 0, keybytes16, 0,                 math.min(keybytes.length, 16));          // convert plain text bytes         byte[] plainbytes = plaintext.getbytes("utf-8");          // setup cipher         secretkeyspec skeyspec = new secretkeyspec(keybytes16, "aes");         cipher cipher = cipher.getinstance("aes/cbc/pkcs7padding");         byte[] iv = new byte[16]; // initialization vector 0         cipher.init(cipher.encrypt_mode, skeyspec, new ivparameterspec(iv));          // encrypt         byte[] encrypted = cipher.dofinal(plainbytes);         string encryptedstring = new string(base64.encodebase64(cipher.dofinal(encrypted)));         // encryptedstring          return encryptedstring;     }      public string decrypt(string encryptedtext, string key) throws exception {         // convert key bytes         messagedigest md = messagedigest.getinstance("md5");         md.update(key.getbytes("utf-8"));          byte[] keybytes = md.digest();         // use first 16 bytes (or less if key shorter)         byte[] keybytes16 = new byte[16];          system.arraycopy(keybytes, 0, keybytes16, 0, math.min(keybytes.length, 64));          // convert plain text bytes         //  byte[] decodebase64 = base64.decodebase64(encryptedtext);         byte[] plainbytes = base64.decodebase64(encryptedtext.getbytes("utf-8"));          // setup cipher         secretkeyspec skeyspec = new secretkeyspec(keybytes16, "aes");         cipher cipher = cipher.getinstance("aes/cbc/pkcs7padding");         byte[] iv = new byte[16]; // initialization vector 0         cipher.init(cipher.decrypt_mode, skeyspec, new ivparameterspec(iv));           byte[] decrypteed = cipher.dofinal(plainbytes);           return new string(decrypteed, "utf-8");     } } 

hello all,sorry silly question have small problem can't find solution. encrypt , decrypt success message decrypt function returns strange string although set utf8 format.the output below

run: yv6ggse2h19kqp/fh8qnl7hdj5zm2dlprnyt6d4ycii= ���`�1����x�� build successful (total time: 0 seconds)   cryptolib crypto=new cryptolib(); string encrypted=crypto.encrypt("message", "key"); system.out.println(encrypted); system.out.println(crypto.decrypt(encrypted, "key")); 

there double encryption following lines:

byte[] encrypted = cipher.dofinal(plainbytes); string encryptedstring = new string(base64.encodebase64(cipher.dofinal(encrypted))); 

this explains length of encrypted data,

base64: yv6ggse2h19kqp/fh8qnl7hdj5zm2dlprnyt6d4ycii=
hex: 62fe868121361f5f64a8ffdf1fcaa797b1c3279cccd8394facd613e9de180882

the encryption should just:

string encryptedstring = new string(base64.encodebase64(cipher.dofinal(plainbytes))); 

this why decryption "strange", binary data bytes displayed string isn't has un-displayable bytes.


Comments

Popular posts from this blog

c - How to retrieve a variable from the Apache configuration inside the module? -

c# - Constructor arguments cannot be passed for interface mocks -

python - malformed header from script index.py Bad header -