一、开发环境
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中调用时,报错
Access restriction: The type 'Base64' is not API (restriction on required library 'D:\Program Files\jdk1.8.0_72\jre\lib\rt.jar')
解决方法:
方法1、项目右键->Build Path ->Libraries -> 加载默认JDK即可
方法2、将Error 改为 Warning
二、编译环境
1、使用gradle编译,build会报错
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"));
^
解决方法:
在build.gradle中添加
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"
}
2、在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>