SecurityManager USE
import java.io.DataInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; public class BaseT { public static void main(String[] args) throws IOException { System.setSecurityManager(new mySecurityM("ABC","123")); File f=new File("E:\\Test.java"); DataInputStream fis=new DataInputStream(new FileInputStream(f)); String x=""; while((x=fis.readLine())!=null){ System.out.println(x); } fis.close(); } } //---------------------- import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class mySecurityM extends SecurityManager { private String upwd; private String unam; public mySecurityM(String unam,String upwd){ this.upwd=upwd; this.unam=unam; } public boolean okacess(){ System.out.println("请输入用户名和密码,用逗号隔开"); BufferedReader dis = new BufferedReader(new InputStreamReader(System.in)); try { String[] value=dis.readLine().split(","); if (value[0].equals(this.unam)&&value[1].equals(this.upwd)) return true; else return false; } catch (IOException e) { return false; } } public void checkRead(String file){ if(!okacess()){ throw new SecurityException("No Premission."); } } }
数字签名
KEYSTORE
keytool -genkey -alias key.keystore -keyalg RSA -validity 30000 -keystore key.keystore
JARSIGNER
jarsigner -verbose -keystore key.keystore -signedjar ABCsigned.jar ABC.jar key.keystore
如果修改了CLASS文件,执行时会报错:
C:\Users\AD\Desktop>java -jar ABCsigned1.jar
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.SecurityException: SHA-256 digest error for
p2100/BaseT.class
at sun.security.util.ManifestEntryVerifier.verify(Unknown Source)
at java.util.jar.JarVerifier.processEntry(Unknown Source)
at java.util.jar.JarVerifier.update(Unknown Source)
at java.util.jar.JarVerifier$VerifierStream.read(Unknown Source)
at sun.misc.Resource.getBytes(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)