encryption - AES in java can run in windows but not in linux -


this question has answer here:

i finished aes class decrypt or encrpyt ,and runs on windows can't run on linux throwing errors following :

given final block not padded

the full code following :

/**  * aestest.java  *   * @author liuyincan  * @time 2013-12-12 下午1:25:44  */ public class aes {   public static string generatekey(int len) {     try {         keygenerator keygen = keygenerator.getinstance("aes");         keygen.init(len);         key key = keygen.generatekey();          return parserstringutils.tohexstring(key.getencoded());     } catch (exception e) {         return null;     } }   /**  * 加密  *   * @param content  *            待加密内容  * @param key  *            加密的密钥  * @return  */ public static string encode(string content, string key) {     try {         keygenerator kgen = keygenerator.getinstance("aes");         kgen.init(128, new securerandom(key.getbytes()));         secretkey secretkey = kgen.generatekey();         byte[] encodeformat = secretkey.getencoded();         secretkeyspec secretkeyspec = new secretkeyspec(encodeformat, "aes");         cipher cipher = cipher.getinstance("aes");         byte[] bytecontent = content.getbytes("utf-8");         cipher.init(cipher.encrypt_mode, secretkeyspec);         byte[] byterresult = cipher.dofinal(bytecontent);         stringbuffer sb = new stringbuffer();         (int = 0; < byterresult.length; i++) {             string hex = integer.tohexstring(byterresult[i] & 0xff);             if (hex.length() == 1) {                 hex = '0' + hex;             }             sb.append(hex.touppercase());         }         return sb.tostring();     } catch (nosuchalgorithmexception e) {         e.printstacktrace();     } catch (nosuchpaddingexception e) {         e.printstacktrace();     } catch (invalidkeyexception e) {         e.printstacktrace();     } catch (unsupportedencodingexception e) {         e.printstacktrace();     } catch (illegalblocksizeexception e) {         e.printstacktrace();     } catch (badpaddingexception e) {         e.printstacktrace();     }     return null; }  /**  * 解密  *   * @param content  *            待解密内容  * @param key  *            解密的密钥  * @return  */ public static string decode(string content, string key) {     if (content.length() < 1)         return null;     byte[] byterresult = new byte[content.length() / 2];     (int = 0; < content.length() / 2; i++) {         int high = integer.parseint(content.substring(i * 2, * 2 + 1), 16);         int low = integer.parseint(content.substring(i * 2 + 1, * 2 + 2), 16);         byterresult[i] = (byte) (high * 16 + low);     }     try {         keygenerator kgen = keygenerator.getinstance("aes");         kgen.init(128, new securerandom(key.getbytes()));         secretkey secretkey = kgen.generatekey();         byte[] encodeformat = secretkey.getencoded();         secretkeyspec secretkeyspec = new secretkeyspec(encodeformat, "aes");         cipher cipher = cipher.getinstance("aes");         cipher.init(cipher.decrypt_mode, secretkeyspec);         byte[] result = cipher.dofinal(byterresult);         return new string(result);     } catch (nosuchalgorithmexception e) {         e.printstacktrace();     } catch (nosuchpaddingexception e) {         e.printstacktrace();     } catch (invalidkeyexception e) {         e.printstacktrace();     } catch (illegalblocksizeexception e) {         e.printstacktrace();     } catch (badpaddingexception e) {         e.printstacktrace();     }     return null; } 

please me fix problem or else boss fire me ,thanks lot

cipher.getinstance("aes"); - gives default implementation of aes.

somewhere between oracle java 6 , late java 7 changed form aes/ecb/nopadding aes/ecb/pkcs5padding.

change such lines to:

  • cipher.getinstance("aes/ecb/pkcs5padding");

Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -