Access restriction错误的处理

一、开发环境

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')

154929_R7fm_2345654.png

解决方法:

方法1、项目右键->Build Path ->Libraries -> 加载默认JDK即可

方法2、将Error 改为 Warning

155703_r2Gl_2345654.png

 

二、编译环境

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"));
                                               ^

160112_aFJw_2345654.png

解决方法:

在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中报错

161655_m77u_2345654.png

解决方法:

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>

 

 

 

 

 

 

转载于:https://my.oschina.net/u/2345654/blog/1645005

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值