百度空间 | 百度首页 
 
查看文章
 
1、1 JCA Design and Architecture(二)
2009年09月13日 星期日 18:22

1、1 JCA Design and Architecture(一)

import java.security.Signature;
import java.security.KeyPair;
import java.security.PublicKey;
import java.security.PrivateKey;
import java.security.NoSuchAlgorithmException;
import java.security.InvalidKeyException;
import java.security.SignatureException;

public class SignatureExample {
    public byte[] signData(byte[] data, PrivateKey key)
    {
        try {
            Signature signer = Signature.getInstance("SHA1withDSA");

            signer.initSign(key);

            signer.update(data);

            return(signer.sign());
        } catch(NoSuchAlgorithmException nsae) {
            System.out.println("Exception: " + nsae);
            nsae.printStackTrace();
        } catch(InvalidKeyException ike) {
            System.out.println("Exception: " + ike);
            ike.printStackTrace();
        } catch(SignatureException se) {
            System.out.println("Exception: " + se);
            se.printStackTrace();
        }

        return(null);
    }

    public boolean verifySig(byte[] data, PublicKey key, byte[] sig)
    {
        try {
            Signature signer = Signature.getInstance("SHA1withDSA");

            signer.initVerify(key);

            signer.update(data);

            return(signer.verify(sig));
        } catch(NoSuchAlgorithmException nsae) {
            System.out.println("Exception: " + nsae);
            nsae.printStackTrace();
        } catch(InvalidKeyException ike) {
            System.out.println("Exception: " + ike);
            ike.printStackTrace();
        } catch(SignatureException se) {
            System.out.println("Exception: " + se);
            se.printStackTrace();
        }

        return(false);
    }

public static void main(String args[])
    {
        SignatureExample sigEx = new SignatureExample();
        KeyPairGeneratorExample kpge = new KeyPairGeneratorExample();
        KeyPair keyPair = kpge.generateKeyPair(717);

        byte[] data = {65,66,67,68,69,70,71,72,73,74};
        byte[] digitalSignature = sigEx.signData(data, keyPair.getPrivate());

        boolean verified;

        // This verification will succeed
        verified = sigEx.verifySig(data, keyPair.getPublic(), digitalSignature);
        if(verified) {
            System.out.println("** The digital signature has been verified");
        } else {
            System.out.println("** The digital signature is invalid, the wrong " +
                               "key was used, or the data has been compromised");
        }

        System.out.println("");

        // Generate a new key pair. Guaranteed to be different and incompatible
        // with first set.
        keyPair = kpge.generateKeyPair(517);
        // This verification will fail
        verified = sigEx.verifySig(data, keyPair.getPublic(), digitalSignature);
        if(verified) {
            System.out.println("** The digital signature has been verified");
        } else {
            System.out.println("** The digital signature is invalid, the wrong " +
                               "key was used, or the data has been compromised");
        }
    }
}

KeyPairGeneratorExample类会在后面讨论,它用来获得公钥和密钥。数据使用密钥签名,使用公钥验证。

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Digital Key Creation and Management(电子key的创建和管理)

      Security API2种形式的key存在。

      透明形式的key允许获得key的具体信息,比如计算key的算法参数。

      非透明形式的key保持这些值隐藏并只允许你访问创建key的算法、使用的编码和key本身的编码形式。

      透明形式的key继承叫做KeySpec标记接口。因为是一个标记接口,接口里没有方法定义。

      在java.security.spec包中Key的接口如下表:

和透明形式的key相反,不透明形式继承自Key接口,不像KeySpec接口,key接口定义了3个方法,所有的实体类必须实现。这三个方法的描述如下:

algorithm”方法返回一个string表示使用创建key的算法:

String algorithm()

getEncoded”方法返回一个已编码版本key,遵循标准编码格式,如X.509 or PKCS #8

byte[] getEncoded()

getFormat”方法返回用来编码key的编码格式的名称。

String getFormat()

“java.security.interfaces包含12个接口直接继承自Key interface。在Java API中,有很多类型标准的key。如下表:


     

      KeyFactory引擎类用来转换透明形式key到非透明形式,反之亦然。

       标准的getInstance方法用来创建KeyFactory。有2个方法转换透明key到非透明key

       一种是为公钥一种为密钥。有1个方法定义来反转这些操作。

       这些操作在下面描述:

generatePublicgeneratePrivate方法接受透明形式key(直接或间接继承KeySpecKeySpec并返回非透明的public keyprivate key

PublicKey generatePublic(KeySpec keySpec)

PrivateKey generatePrivate(KeySpec keySpec)

     getKeySpec 方法接受非透明形式的key通过key参数,一个指定哪个key specification 被转换并返回的类:

KeySpec getKeySpec(Key key, Class keySpec)

更多的从客户端的角度考虑,KeyPairKeyPairGenerator以及KeyStore引擎类是用来创建、存储并管理这些public/private key和证书。

KeyPair类定义下面2个方法:

PrivateKey getPrivate()

PublicKey getPublic()

第一个方法返回正在被存储的private key,第二个返回public key

KeyPairGenerator引擎类用来产生public/private key 对,并使用KeyPair类来存储它们。

KeyPairGenerator引擎类产生一对key是与算法无关的,是和keyPairGenerator如何初始化有关。因为所有的算法使用基础size randomness(随机)的概念,初始化存在使得特定的算法不是必须的:

void initialize(int keysize, SecureRandom random)

void initialize(int keysize)

Keysize参数的含义是:对于每一个算法keysize不同。

其他的算法参数也是已给预先配置好的参数。例如:一个DSA算法,基于特定的keysize,会指派它的参数不同的值。如果一个随机数产生器不被传入,radomness通过默认系统产生器产生。

下面形式的initialize实现初始化基于特定的参数,通过params参数传递。如果一个随机数字产生器没有传进,radomness由系统产生:

void initialize(AlgorithmParameterSpec params, SecureRandom random)

void initialize(AlgorithmParameterSpec params)

下面方法创建并返回一个KeyPair对象。每次调用这个方法都会返回单独和不同的key

KeyPair generateKeyPair()

现在有一个例子来实现KeyGenerator类来产生private keypublic key并存在KeyPair对象里:

import java.security.KeyPairGenerator;
import java.security.KeyPair;
import java.security.SecureRandom;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.PublicKey;
import java.security.PrivateKey;
public class KeyPairGeneratorExample {
public KeyPair generateKeyPair(long seed)
{
try {
// Get a DSA key generator from first
// provider that provides it
KeyPairGenerator keyGenerator =
KeyPairGenerator.getInstance(“DSA”);
// Get a random number generator using
// algorithm SHA1PRNG from the SUN provider package.
SecureRandom rng =
SecureRandom.getInstance(“SHA1PRNG”, “SUN”);
// Configure RNG and initialize key pair generator
rng.setSeed(seed);
keyGenerator.initialize(1024, rng);
return(keyGenerator.generateKeyPair());
} catch(NoSuchProviderException nspe) {
System.out.println(“Exception: “ + nspe);
nspe.printStackTrace();
} catch(NoSuchAlgorithmException nsae) {
System.out.println(“Exception: “ + nsae);
nsae.printStackTrace();
}
return(null);
}  

public static void main(String args[])

{

KeyPairGeneratorExample kpge = new KeyPairGeneratorExample();

KeyPair kp = kpge.generateKeyPair(717);

System.out.println(“-- Public Key ----”);

PublicKey pubKey = kp.getPublic();

System.out.println(“ Algorithm=” + pubKey.getAlgorithm());

System.out.println(“ Encoded=” + pubKey.getEncoded());

System.out.println(“ Format=” + pubKey.getFormat());

System.out.println(“\n-- Private Key ----”);

PrivateKey priKey = kp.getPrivate();

System.out.println(“ Algorithm=” + priKey.getAlgorithm());

System.out.println(“ Encoded=” + priKey.getEncoded());

System.out.println(“ Format=” + priKey.getFormat());

}

}

这个类利用特定的随机数产生器-SHA1PRNG(来自SUN provider包)。

你会看到privatepublic key的不同,输出结果:

-- Public Key ----

Algorithm=DSA

Encoded=[B@1a46e30

Format=X.509

-- Private Key ----

Algorithm=DSA

Encoded=[B@3e25a5

Format=PKCS#8

Public key的编码格式是X509,private key的编码格式PKCS#8

下接:1、1 JCA Design and Architecture(三)


类别:java高级 | 添加到搜藏 | 浏览() | 评论 (0)
 
最近读者:
 
网友评论:
发表评论:
姓 名:
网址或邮箱: (选填)
内 容:
验证码: 请点击后输入四位验证码,字母不区分大小写
      

     

©2009 Baidu