读书人

转:JAVA 上加密算法的兑现用例

发布时间: 2012-07-01 13:15:00 作者: rapoo

转:JAVA 上加密算法的实现用例

?

通常 , 使用的加密算法 比较简便高效 , 密钥简短,加解密速度快,破译极其困难。本文介绍了 MD5/SHA1,DSA,DESede/DES,Diffie-Hellman 的使用。


常用 API

java.security.KeyPairGenerator 密钥生成器类?public static KeyPairGenerator getInstance(String algorithm) throws NoSuchAlgorithmException?以指定的算法返回一个 KeyPairGenerator 对象?参数 : algorithm 算法名 . 如 :"DSA","RSA"

public void initialize(int keysize)

以指定的长度初始化 KeyPairGenerator 对象 , 如果没有初始化系统以 1024 长度默认设置

参数 :keysize 算法位长 . 其范围必须在 512 到 1024 之间,且必须为 64 的倍数

public void initialize(int keysize, SecureRandom random)?以指定的长度初始化和随机发生器初始化 KeyPairGenerator 对象?参数 :keysize 算法位长 . 其范围必须在 512 到 1024 之间,且必须为 64 的倍数?random 一个随机位的来源 ( 对于 initialize(int keysize) 使用了默认随机器

public abstract KeyPair generateKeyPair()?产生新密钥对

java.security.KeyPair 密钥对类?public PrivateKey getPrivate()?返回私钥

public PublicKey getPublic()?返回公钥

java.security.Signature 签名类?public static Signature getInstance(String algorithm) throws NoSuchAlgorithmException?返回一个指定算法的 Signature 对象?参数 algorithm 如 :"DSA"

public final void initSign(PrivateKey privateKey)?throws InvalidKeyException?用指定的私钥初始化?参数 :privateKey 所进行签名时用的私钥

public final void update(byte data)?throws SignatureException?public final void update(byte[] data)?throws SignatureException?public final void update(byte[] data, int off, int len)?throws SignatureException?添加要签名的信息

public final byte[] sign()?throws SignatureException?返回签名的数组 , 前提是 initSign 和 update

public final void initVerify(PublicKey publicKey)?throws InvalidKeyException?用指定的公钥初始化?参数 :publicKey 验证时用的公钥

public final boolean verify(byte[] signature)?throws SignatureException?验证签名是否有效 , 前提是已经 initVerify 初始化?参数 : signature 签名数组

/* 安全程序 DESede/DES 测试 */ import java.security.*; import javax.crypto.*; public class testdes { public static void main(String[] args){ testdes my=new testdes(); my.run(); } public void run() { // 添加新安全算法 , 如果用 JCE 就要把它添加进去 Security.addProvider(new com.sun.crypto.provider.SunJCE()); String Algorithm="DES"; // 定义 加密算法 , 可用 DES,DESede,Blowfish String myinfo="要加密的信息"; try { // 生成密钥 KeyGenerator keygen = KeyGenerator.getInstance(Algorithm); SecretKey deskey = keygen.generateKey(); // 加密 System.out.println("加密前的二进串 :"+byte2hex(myinfo.getBytes())); System.out.println("加密前的信息 :"+myinfo); Cipher c1 = Cipher.getInstance(Algorithm); c1.init(Cipher.ENCRYPT_MODE,deskey); byte[] cipherByte=c1.doFinal(myinfo.getBytes()); System.out.println("加密后的二进串 :"+byte2hex(cipherByte)); // 解密 c1 = Cipher.getInstance(Algorithm); c1.init(Cipher.DECRYPT_MODE,deskey); byte[] clearByte=c1.doFinal(cipherByte); System.out.println("解密后的二进串 :"+byte2hex(clearByte)); System.out.println("解密后的信息 :"+(new String(clearByte))); } catch (java.security.NoSuchAlgorithmException e1) {e1.printStackTrace();} catch (javax.crypto.NoSuchPaddingException e2) {e2.printStackTrace();} catch (java.lang.Exception e3) {e3.printStackTrace();} } public String byte2hex(byte[] b) // 二行制转字符串 { String hs=""; String stmp=""; for (int n=0;n<b.length;n++) { stmp=(java.lang.Integer.toHexString(b[n] & 0XFF)); if (stmp.length()==1) hs=hs+"0"+stmp; else hs=hs+stmp; if (n<b.length-1) hs=hs+":"; } return hs.toUpperCase(); } }

System.out.println("ALICE: 产生 DH 对 ..."); KeyPairGenerator aliceKpairGen = KeyPairGenerator.getInstance("DH"); aliceKpairGen.initialize(512); KeyPair aliceKpair = aliceKpairGen.generateKeyPair();

alice 生成公钥发送组 bob

byte[] alicePubKeyEnc = aliceKpair.getPublic().getEncoded();

bob 从 alice 发送来的公钥中读出 DH 密钥对的初始参数生成 bob 的 DH 密钥对

注意这一步一定要做 , 要保证每个用户用相同的初始参数生成的

DHParameterSpec dhParamSpec = ((DHPublicKey)alicePubKey).getParams(); KeyPairGenerator bobKpairGen = KeyPairGenerator.getInstance("DH"); bobKpairGen.initialize(dhParamSpec); KeyPair bobKpair = bobKpairGen.generateKeyPair();

bob 根据 alice 的公钥生成本地的 DES 密钥

KeyAgreement bobKeyAgree = KeyAgreement.getInstance("DH"); bobKeyAgree.init(bobKpair.getPrivate()); bobKeyAgree.doPhase(alicePubKey, true); SecretKey bobDesKey = bobKeyAgree.generateSecret("DES");

bob 已经生成了他的 DES 密钥 , 他现把他的公钥发给 alice,

byte[] bobPubKeyEnc = bobKpair.getPublic().getEncoded();

alice 根据 bob 的公钥生成本地的 DES 密钥

,,,,,, 解码 KeyAgreement aliceKeyAgree = KeyAgreement.getInstance("DH"); aliceKeyAgree.init(aliceKpair.getPrivate()); aliceKeyAgree.doPhase(bobPubKey, true); SecretKey aliceDesKey = aliceKeyAgree.generateSecret("DES");

bob 和 alice 能过这个过程就生成了相同的 DES 密钥 , 在这种基础就可进行安全能信

常用 API

java.security.KeyPairGenerator 密钥生成器类?
public static KeyPairGenerator getInstance(String algorithm)?
throws NoSuchAlgorithmException?
以指定的算法返回一个 KeyPairGenerator 对象?
参数 : algorithm 算法名 . 如 : 原来是 DSA, 现在添加了 DiffieHellman(DH)

public void initialize(int keysize)?
以指定的长度初始化 KeyPairGenerator 对象 , 如果没有初始化系统以 1024 长度默认设置?
参数 :keysize 算法位长 . 其范围必须在 512 到 1024 之间,且必须为 64 的倍数?
注意 : 如果用 1024 生长的时间很长 , 最好生成一次后就保存 , 下次就不用生成了

public void initialize(AlgorithmParameterSpec params)?
throws InvalidAlgorithmParameterException?
以指定参数初始化

javax.crypto.interfaces.DHPublicKey?
public DHParameterSpec getParams()?
返回?
java.security.KeyFactory

public static KeyFactory getInstance(String algorithm)?
throws NoSuchAlgorithmException?
以指定的算法返回一个 KeyFactory?
参数 : algorithm 算法名 :DSH,DH

public final PublicKey generatePublic(KeySpec keySpec)?
throws InvalidKeySpecException?
根据指定的 key 说明 , 返回一个 PublicKey 对象

java.security.spec.X509EncodedKeySpec?
public X509EncodedKeySpec(byte[] encodedKey)?
根据指定的二进制编码的字串生成一个 key 的说明?
参数 :encodedKey 二进制编码的字串 ( 一般能过 PublicKey.getEncoded() 生成 )?
javax.crypto.KeyAgreement 密码一至类

public static final KeyAgreement getInstance(java.lang.String algorithm)?
throws java.security.NoSuchAlgorithmException?
返回一个指定算法的 KeyAgreement 对象?
参数 :algorithm 算法名 , 现在只能是 DiffieHellman(DH)

public final void init(java.security.Key key)?
throws java.security.InvalidKeyException?
用指定的私钥初始化?
参数 :key 一个私钥

public final java.security.Key doPhase(java.security.Key key,?
boolean lastPhase)?
throws java.security.InvalidKeyException,?
java.lang.IllegalStateException?
用指定的公钥进行定位 ,lastPhase 确定这是否是最后一个公钥 , 对于两个用户的?
情况下就可以多次定次 , 最后确定?
参数 :key 公钥?
lastPhase 是否最后公钥

public final SecretKey generateSecret(java.lang.String algorithm)?
throws java.lang.IllegalStateException,?
java.security.NoSuchAlgorithmException,?
java.security.InvalidKeyException?
根据指定的算法生成密钥?
参数 :algorithm 加密算法 ( 可用 DES,DESede,Blowfish)

*/ import java.io.*; import java.math.BigInteger; import java.security.*; import java.security.spec.*; import java.security.interfaces.*; import javax.crypto.*; import javax.crypto.spec.*; import javax.crypto.interfaces.*; import com.sun.crypto.provider.SunJCE; public class testDHKey { public static void main(String argv[]) { try { testDHKey my= new testDHKey(); my.run(); } catch (Exception e) { System.err.println(e); } } private void run() throws Exception { Security.addProvider(new com.sun.crypto.provider.SunJCE()); System.out.println("ALICE: 产生 DH 对 ..."); KeyPairGenerator aliceKpairGen = KeyPairGenerator.getInstance("DH"); aliceKpairGen.initialize(512); KeyPair aliceKpair = aliceKpairGen.generateKeyPair(); // 生成时间长 // 张三 (Alice) 生成公共密钥 alicePubKeyEnc 并发送给李四 (Bob) , // 比如用文件方式 ,socket..... byte[] alicePubKeyEnc = aliceKpair.getPublic().getEncoded(); //bob 接收到 alice 的编码后的公钥 , 将其解码 KeyFactory bobKeyFac = KeyFactory.getInstance("DH"); X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec (alicePubKeyEnc); PublicKey alicePubKey = bobKeyFac.generatePublic(x509KeySpec); System.out.println("alice 公钥 bob 解码成功"); // bob 必须用相同的参数初始化的他的 DH KEY 对 , 所以要从 Alice 发给他的公开密钥 , // 中读出参数 , 再用这个参数初始化他的 DH key 对 // 从 alicePubKye 中取 alice 初始化时用的参数 DHParameterSpec dhParamSpec = ((DHPublicKey)alicePubKey).getParams(); KeyPairGenerator bobKpairGen = KeyPairGenerator.getInstance("DH"); bobKpairGen.initialize(dhParamSpec); KeyPair bobKpair = bobKpairGen.generateKeyPair(); System.out.println("BOB: 生成 DH key 对成功"); KeyAgreement bobKeyAgree = KeyAgreement.getInstance("DH"); bobKeyAgree.init(bobKpair.getPrivate()); System.out.println("BOB: 初始化本地 key 成功"); // 李四 (bob) 生成本地的密钥 bobDesKey bobKeyAgree.doPhase(alicePubKey, true); SecretKey bobDesKey = bobKeyAgree.generateSecret("DES"); System.out.println("BOB: 用 alice 的公钥定位本地 key, 生成本地 DES 密钥成功"); // Bob 生成公共密钥 bobPubKeyEnc 并发送给 Alice, // 比如用文件方式 ,socket....., 使其生成本地密钥 byte[] bobPubKeyEnc = bobKpair.getPublic().getEncoded(); System.out.println("BOB 向 ALICE 发送公钥"); // alice 接收到 bobPubKeyEnc 后生成 bobPubKey // 再进行定位 , 使 aliceKeyAgree 定位在 bobPubKey KeyFactory aliceKeyFac = KeyFactory.getInstance("DH"); x509KeySpec = new X509EncodedKeySpec(bobPubKeyEnc); PublicKey bobPubKey = aliceKeyFac.generatePublic(x509KeySpec); System.out.println("ALICE 接收 BOB 公钥并解码成功"); ; KeyAgreement aliceKeyAgree = KeyAgreement.getInstance("DH"); aliceKeyAgree.init(aliceKpair.getPrivate()); System.out.println("ALICE: 初始化本地 key 成功"); aliceKeyAgree.doPhase(bobPubKey, true); // 张三 (alice) 生成本地的密钥 aliceDesKey SecretKey aliceDesKey = aliceKeyAgree.generateSecret("DES"); System.out.println("ALICE: 用 bob 的公钥定位本地 key, 并生成本地 DES 密钥"); if (aliceDesKey.equals(bobDesKey)) System.out.println("张三和李四的密钥相同"); // 现在张三和李四的本地的 deskey 是相同的所以 , 完全可以进行发送加密 , 接收后解密 , 达到 // 安全通道的的目的 /* * bob 用 bobDesKey 密钥加密信息 */ Cipher bobCipher = Cipher.getInstance("DES"); bobCipher.init(Cipher.ENCRYPT_MODE, bobDesKey); String bobinfo= "这是李四的机密信息"; System.out.println("李四加密前原文 :"+bobinfo); byte[] cleartext =bobinfo.getBytes(); byte[] ciphertext = bobCipher.doFinal(cleartext); /* * alice 用 aliceDesKey 密钥解密 */ Cipher aliceCipher = Cipher.getInstance("DES"); aliceCipher.init(Cipher.DECRYPT_MODE, aliceDesKey); byte[] recovered = aliceCipher.doFinal(ciphertext); System.out.println("alice 解密 bob 的信息 :"+(new String(recovered))); if (!java.util.Arrays.equals(cleartext, recovered)) throw new Exception("解密后与原文信息不同"); System.out.println("解密后相同"); } }

第 3 章小结

在加密术中生成密钥对时,密钥对的当然是越长越好,但费时也越多,请从中从实际出发选取合适的长度,大部分例码中的密钥是每次运行就从新生成,在实际的情况中是生成后在一段时间保存在文件中,再次运行直接从文件中读入,从而加快速度。当然定时更新和加强密钥保管的安全性也是必须的。

?

转自:

http://blog.csdn.net/litton_van/article/details/7101464

?

读书人网 >移动开发

热点推荐