侧边栏壁纸
博主头像
侯秀荣

贪婪和恐惧是人性的两大弱点,
人类几万年,人性也没进步1厘米。

  • 累计撰写 172 篇文章
  • 累计收到 3 条评论

maven打包编译的错误:sun.misc.BASE64Decoder是Sun的专用API,可能会在未来版本中删除

2017-11-13 / 0 评论 / 6924 阅读

今天遇到 sun.misc.BASE64Decoder是Sun的专用API,可能会在未来版本中删除的错误!
算是maven的一个bug吧。maven-compiler-plugin 2.3.2 发布以后把这个错误改成了告警。所以只要将这个插件升级一下就好了。

<plugin> 
 <groupId>org.apache.maven.plugins</groupId>  
 <artifactId>maven-compiler-plugin</artifactId>  
 <version>2.3.2</version>  
</plugin>

不料有出现了程序包com.sun.image.codec.jpeg不存在的问题,继续查,这次需要给环境变量加一个jar包。
最终解决办法:

 <plugin> 
 <groupId>org.apache.maven.plugins</groupId>  
 <artifactId>maven-compiler-plugin</artifactId>  
 <version>2.3.2</version>  
 <configuration>  
 <source>1.7</source>  
 <target>1.7</target>  
 <encoding>UTF-8</encoding>  
 <compilerArguments>  
 <verbose/>  
 <bootclasspath>${java.home}/lib/rt.jar</bootclasspath>  
 </compilerArguments>  
 </configuration>  
</plugin>

最后更加完美的解决方案是替换 sun 的这个BASE64Decoder BASE64Encoder
在pom.xml文件中导入apache的包:

 <groupId>commons-codec</groupId> 
 <artifactId>commons-codec</artifactId> 
 <version>1.8</version>
</dependency>

程序修改如下

import org.apache.commons.codec.binary.Base64; 
import org.apache.commons.codec.digest.DigestUtils; 
import javax.crypto.Cipher; 
import javax.crypto.spec.SecretKeySpec;

/**
 * Created by houxiurong on 2016/8/26. 
 */

public class AESUtils {

 public static final String ECB_CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";

 public static void main(String[] args) throws Exception {

 String content = "这是一个测试字符串";

 System.out.println("加密前:" + content); 
 String key = "982922d3680911e6"; 
 System.out.println("加密密钥:" + key); 
 String encrypt = aesEncrypt(content, key); 
 System.out.println("加密后:" + encrypt); 
 String decrypt = aesDecrypt(encrypt, key); 
 System.out.println("解密后:" + decrypt); 
 }

 public static String base64Encode(byte[] bytes) { 
 return new Base64().encodeBase64String(bytes); 
 }

 public static byte[] base64Decode(String base64Code) throws Exception { 
 return new Base64().decodeBase64(base64Code); 
 }

 /** 
 * AES/ECB/PKCS5Padding 
 * 
 * @param input 待加密的明文 
 * @param encryptKey 加密密钥 
 * @return 加密后的密文 base64编码 
 */
 public static String aesEncrypt(String input, String encryptKey) throws Exception {
    SecretKeySpec skey = new SecretKeySpec(encryptKey.getBytes(), "AES");
    Cipher cipher = Cipher.getInstance(ECB_CIPHER_ALGORITHM);
    cipher.init(Cipher.ENCRYPT_MODE, skey);
    return base64Encode(cipher.doFinal(input.getBytes()));
 }

 /**
 * 解密方法暂时不需要使用,仅作验证加密结果用途
 **/
 public static String aesDecrypt(String encryptStr, String decryptKey) throws Exception {
 byte[] input = base64Decode(encryptStr);
 SecretKeySpec skey = new SecretKeySpec(decryptKey.getBytes(), "AES");
 Cipher cipher = Cipher.getInstance(ECB_CIPHER_ALGORITHM);
 cipher.init(Cipher.DECRYPT_MODE, skey);
 return new String(cipher.doFinal(input));
 }

 /**
 * md5加密
 * @param content
 * @return
 */
 public static String md5Encrypt(String content) {
  try {
     return DigestUtils.md5Hex(content);
  } catch (Exception e) {
     throw new RuntimeException(e);
  }
 }
}
/**
* uuid: 8b7e48cc65df11e6a60202eca976a4e9
* Key: 6f79c9ee6bb07cce
*/

评论一下?

OωO
取消