java安全特性

首先,java安全特性为不同的使用者提供了不同的解决方式

1.运行java程序的用户

    内置安全功能保护您的文件和信息保密性,并且证实每位代码提供者的身分。

2.开发者

    可以使用API方法,将其纳入你的程序的安全功能,包括加密服务和安全检查。

3.系统管理者,运行商,及用户

    JDK工具管理你的密钥库;为jar文件生成签名;验证签名的真实性和完整性;创建和修改的政策文件定义安装的安全政策。

 

其次,需要注意

如果没有明确的在代码中引用或在启动时设置相关的参数,java安全管理默认并没有介入。这时java的运行程序不受安全控制。

java安全管理介入的方式有以下两种

  1. 在启动参数中添加 -Djava.security.manager
  2. 在程序中调用
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
    sm.checkPermission(new HighScorePermission(gameName));
}

 

默认系统安全政策文件存放位置:

  Windows:
    java.home\lib\security\java.policy  
  UNIX:
    java.home/lib/security/java.policy

注意:这里的java.home一般指的是JDK下的jre路径

应用方式:

1.授予Applet和应用所需的权限,使用JDK政策工具policytool(具体使用方法参考java tutorial 中的试例),生成授权政策文件(最好以.policy为后缀名)。

要使用此政策授权文件有以下两种方式:

  1. 启动参数引用。 例如:java -Djava.security.manager -Djava.security.policy=example.policy GetProps
  2. 在系统安全政策文件中引用。 找到java.policy文件中的policy.url.2 位置在其后添加 policy.url.3=file:/C:/Test/examplepolicy 注意填写policy.url.*的序号自增

2.应用工具实现代码安全和文件交换

1)代码签名。例如:发送者susan

    • 准备要签名的代码。例如: Count.java,将它编译成 Count.class
    • 将代码打入jar包。例如: jar cvf Count.jar Count.class
    • 生成密钥。例如: keytool -genkey -alias signFiles -keystore examplestore
    • 签名jar文件。例如: jarsigner -keystore examplestore -signedjar sCount.jar Count.jar signFiles
    • 导出公钥。例如: keytool -export -keystore examplestore -alias signFiles -file Example.cer
    • 将签名后的jar文件 sCount.jar 和公钥文件 Example.cer 一起发送给接收方

2)代码接收。例如:接收者ray

    • 查看受限应用程序。例如: java -Djava.security.manager -cp sCount.jar Count C:\TestData\data
    • 查看证书信息。例如: keytool -printcert -file Example.cer
    • 导入信任证书。例如: keytool -import -alias susan -file Example.cer -keystore exampleraystore
    • 设置政策文件授予应用需要的权限。例如:生成 exampleraypolicy (注意:设置密钥库。例如: exampleraystore;设置 CodeBase时使用'/'。例如: file:/C:/Test/*;设置 TargetName时使用'\'。例如: C:\TestData\*
    • 检查政策文件效果。例如: java -Djava.security.manager -Djava.security.policy=exampleraypolicy -cp sCount.jar Count C:\TestData\data 或者写入系统政策文件例如: policy.url.3=file:/C:/Test/exampleraypolicy
    • 如果是文件交换可以验证授权。例如: jarsigner -verify -verbose -keystore exampleruthstore sContract.jar

3.应用代码实现安全签名和文件交换

例如:

签名文件

import java.io.*;
import java.security.*;
  
class GenSig {
  
     public static void main(String[] args) {
  
         /* Generate a DSA signature */
  
         if (args.length != 1 ) {
             System.out.println( "Usage: GenSig nameOfFileToSign" );
             }
         else try {
  
             /* Generate a key pair */
  
             KeyPairGenerator keyGen = KeyPairGenerator.getInstance( "DSA" , "SUN" );
             SecureRandom random = SecureRandom.getInstance( "SHA1PRNG" , "SUN" );
  
             keyGen.initialize( 1024 , random);
  
             KeyPair pair = keyGen.generateKeyPair();
             PrivateKey priv = pair.getPrivate();
             PublicKey pub = pair.getPublic();
  
  
             /* Create a Signature object and initialize it with the private key */
  
             Signature dsa = Signature.getInstance( "SHA1withDSA" , "SUN" ); 
  
             dsa.initSign(priv);
  
             /* Update and sign the data */
  
             FileInputStream fis = new FileInputStream(args[ 0 ]);
             BufferedInputStream bufin = new BufferedInputStream(fis);
             byte [] buffer = new byte [ 1024 ];
             int len;
             while (bufin.available() != 0 ) {
                 len = bufin.read(buffer);
                 dsa.update(buffer, 0 , len);
                 };
  
             bufin.close();
  
             /* Now that all the data to be signed has been read in, 
                     generate a signature for it */
  
             byte [] realSig = dsa.sign();
  
          
             /* Save the signature in a file */
             FileOutputStream sigfos = new FileOutputStream( "sig" );
             sigfos.write(realSig);
  
             sigfos.close();
  
  
             /* Save the public key in a file */
             byte [] key = pub.getEncoded();
             FileOutputStream keyfos = new FileOutputStream( "suepk" );
             keyfos.write(key);
  
             keyfos.close();
  
         } catch (Exception e) {
             System.err.println( "Caught exception " + e.toString());
         }
  
     };
  
}


认证签名文件

import java.io.*;

import java.security.*;
import java.security.spec.*;
  
class VerSig {
  
     public static void main(String[] args) {
  
         /* Verify a DSA signature */
  
         if (args.length != 3 ) {
             System.out.println( "Usage: VerSig publickeyfile signaturefile datafile" );
             }
         else try {
  
             /* import encoded public key */
  
             FileInputStream keyfis = new FileInputStream(args[ 0 ]);
             byte [] encKey = new byte [keyfis.available()];  
             keyfis.read(encKey);
  
             keyfis.close();
  
             X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(encKey);
  
             KeyFactory keyFactory = KeyFactory.getInstance( "DSA" , "SUN" );
             PublicKey pubKey = keyFactory.generatePublic(pubKeySpec);
  
             /* input the signature bytes */
             FileInputStream sigfis = new FileInputStream(args[ 1 ]);
             byte [] sigToVerify = new byte [sigfis.available()]; 
             sigfis.read(sigToVerify );
  
             sigfis.close();
  
             /* create a Signature object and initialize it with the public key */
             Signature sig = Signature.getInstance( "SHA1withDSA" , "SUN" );
             sig.initVerify(pubKey);
  
             /* Update and verify the data */
  
             FileInputStream datafis = new FileInputStream(args[ 2 ]);
             BufferedInputStream bufin = new BufferedInputStream(datafis);
  
             byte [] buffer = new byte [ 1024 ];
             int len;
             while (bufin.available() != 0 ) {
                 len = bufin.read(buffer);
                 sig.update(buffer, 0 , len);
                 };
  
             bufin.close();
  
  
             boolean verifies = sig.verify(sigToVerify);
  
             System.out.println( "signature verifies: " + verifies);
  
  
         } catch (Exception e) {
             System.err.println( "Caught exception " + e.toString());
};
  
     }
  
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值