1、開發環境java
import java.math.BigInteger; import java.security.KeyFactory; import java.security.PublicKey; import java.security.spec.RSAPublicKeySpec; import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; import com.sun.org.apache.xml.internal.security.utils.Base64; /* * This Java source file was generated by the Gradle 'init' task. */ public class Library { public boolean someLibraryMethod() { return true; } static SecretKeySpec sk = new SecretKeySpec("propsoft".getBytes(), "DES"); public static String dencry(String password) { try { byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(password); Cipher c1 = Cipher.getInstance("DES"); c1.init(Cipher.DECRYPT_MODE, sk); byte[] cipherByte = c1.doFinal(dec); return new String(cipherByte, "utf-8"); } catch (Exception e) { } return null; } public static String encryptByPublicKey(String str, String module, String exponentString) { try { byte[] modulusBytes = com.sun.org.apache.xml.internal.security.utils.Base64.decode(module.getBytes("UTF-8")); byte[] exponentBytes = com.sun.org.apache.xml.internal.security.utils.Base64.decode(exponentString.getBytes("UTF-8")); BigInteger modulus = new BigInteger(1, modulusBytes); BigInteger exponent = new BigInteger(1, exponentBytes); RSAPublicKeySpec rsaPubKey = new RSAPublicKeySpec(modulus, exponent); KeyFactory fact = KeyFactory.getInstance("RSA"); PublicKey publicKey = fact.generatePublic(rsaPubKey); Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.ENCRYPT_MODE, publicKey); byte[] cipherData = cipher.doFinal(str.getBytes("UTF-8")); return Base64.encode(cipherData); } catch (Exception e) { e.printStackTrace(); } return null; } }
在開發工具,如eclipse中調用時,報錯apache
Access restriction: The type 'Base64' is not API (restriction on required library 'D:\Program Files\jdk1.8.0_72\jre\lib\rt.jar')eclipse
解決方法:maven
方法一、項目右鍵->Build Path ->Libraries -> 加載默認JDK便可工具
方法二、將Error 改成 Warning開發工具
2、編譯環境gradle
一、使用gradle編譯,build會報錯ui
D:\develop\workspace\demo\src\main\java\Library.java:9: 錯誤: 程序包com.sun.org.apache.xml.internal.security.utils不存在 import com.sun.org.apache.xml.internal.security.utils.Base64; ^ D:\develop\workspace\demo\src\main\java\Library.java:23: 警告: BASE64Decoder是內部專用 API, 可能會在將來發行版中刪除 byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(password); ^ D:\develop\workspace\demo\src\main\java\Library.java:36: 錯誤: 找不到符號 byte[] modulusBytes = Base64.decode(module.getBytes("UTF-8")); ^ 符號: 變量 Base64 位置: 類 Library D:\develop\workspace\demo\src\main\java\Library.java:37: 錯誤: 找不到符號 byte[] exponentBytes = Base64.decode(exponentString.getBytes("UTF-8")); ^
解決方法:spa
在build.gradle中添加rest
import org.apache.tools.ant.taskdefs.condition.Os tasks.withType(JavaCompile) { def sep = ':' if (Os.isFamily(Os.FAMILY_WINDOWS)) { sep = ';' } options.bootClasspath = "$System.env.JAVA_HOME/jre/lib/rt.jar"+sep+"$System.env.JAVA_HOME/jre/lib/jce.jar" }
二、在maven中報錯
解決方法:
pom.xml中添加編譯參數
<build> <plugins> <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>${project.build.sourceEncoding}</encoding> <compilerArguments> <verbose /> <bootclasspath>${java.home}/lib/rt.jar;${java.home}/lib/jce.jar</bootclasspath> </compilerArguments> </configuration> </plugin> </plugins> </build>