问题描述
importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.InputStream;importjava.security.InvalidKeyException;importjava.security.Key;importjava.security.NoSuchAlgorithmException;importjava.util.Iterator;importjava.util.Properties;importjavax.crypto.BadPaddingException;importjavax.crypto.Cipher;importjavax.crypto.IllegalBlockSizeException;importjavax.crypto.NoSuchPaddingException;importjavax.crypto.spec.SecretKeySpec;importorg.apache.commons.codec.binary.Hex;/***AES加密工具*@authorBany*@version创建时间:**/publicclassSecUtil{privatestaticfinalbyte[]keybytes={0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38};publicstaticvoidmain(String[]args)throwsException{encrypt("db.properties");decrypt("db.properties");}/***加密*@paramvalue*@return*@throwsIOException*@throwsNoSuchPaddingException*@throwsNoSuchAlgorithmException*@throwsInvalidKeyException*@throwsBadPaddingException*@throwsIllegalBlockSizeException*/publicstaticvoidencrypt(Stringfilepath)throwsIOException,InvalidKeyException,NoSuchAlgorithmException,NoSuchPaddingException,IllegalBlockSizeException,BadPaddingException{InputStreaminput=SecUtil.class.getClassLoader().getResourceAsStream(filepath);Propertiesproperties=newProperties();properties.load(input);Iterator<Object>it=properties.keySet().iterator();FileOutputStreamout=newFileOutputStream(filepath);while(it.hasNext()){Stringkey=String.valueOf(it.next());byte[]value=properties.getProperty(key).getBytes();intmode=Cipher.ENCRYPT_MODE;Ciphercipher=initCipher(mode);Stringdatas=String.valueOf(Hex.encodeHex(cipher.doFinal(value)));System.out.println(key+"="+datas+'r'+'n');out.write((key+"="+datas+'r'+'n').getBytes());}}/***解密*@paramvalue*@return*@throwsException*@throwsBadPaddingException*@throwsIllegalBlockSizeException***/publicstaticvoiddecrypt(Stringfilepath)throwsIllegalBlockSizeException,BadPaddingException,Exception{InputStreaminput=SecUtil.class.getClassLoader().getResourceAsStream(filepath);Propertiesproperties=newProperties();properties.load(input);Iterator<Object>it=properties.keySet().iterator();FileOutputStreamout=newFileOutputStream(filepath);while(it.hasNext()){Stringkey=String.valueOf(it.next());byte[]value=properties.getProperty(key).getBytes();System.out.println(value);intmode=Cipher.DECRYPT_MODE;Ciphercipher=initCipher(mode);char[]datas=String.valueOf(Hex.encodeHex(cipher.doFinal(value))).toCharArray();//char[]a=cipher.doFinal(value).toString().toCharArray();Stringdatass=newString(cipher.doFinal(Hex.decodeHex(datas)));System.out.println(key+"="+datass+'r'+'n');out.write((key+"="+datass+'r'+'n').getBytes());}}/***加密解密的算法(指定私钥解密的数据)**/privatestaticCipherinitCipher(intmode)throwsNoSuchAlgorithmException,NoSuchPaddingException,InvalidKeyException{Ciphercipher=Cipher.getInstance("AES/ECB/PKCS5Padding");Keykey=newSecretKeySpec(keybytes,"AES");cipher.init(mode,key);returncipher;}}**********************************错误信息*****************************Exceptioninthread"main"java.security.InvalidParameterException:Invalidoperationmodeatjavax.crypto.Cipher.a(DashoA13*..)atjavax.crypto.Cipher.init(DashoA13*..)atjavax.crypto.Cipher.init(DashoA13*..)atconfig.SecUtil.initCipher(SecUtil.java:106)atconfig.SecUtil.decrypt(SecUtil.java:90)atconfig.SecUtil.main(SecUtil.java:35)