return false;
};
}
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();
}
}
2.4. DESede/DES對稱(chēng)算法
首先生成密鑰,并保存(這里并沒(méi)的保存的代碼,可參考DSA中的
KeyGenerator keygen = KeyGenerator.getInstance(Algorithm);
SecretKey deskey = keygen.generateKey();
用密鑰加密明文(myinfo),生成密文(cipherByte)
Cipher c1 = Cipher.getInstance(Algorithm);
c1.init(Cipher.ENCRYPT_MODE,deskey);
byte[] cipherByte=c1.doFinal(myinfo.getBytes());
傳送密文和密鑰,本文沒(méi)有相應代碼可參考DSA
.............
用密鑰解密密文
c1 = Cipher.getInstance(Algorithm);
c1.init(Cipher.DECRYPT_MODE,deskey);
byte[] clearByte=c1.doFinal(cipherByte);
相對來(lái)說(shuō)對稱(chēng)密鑰的使用是很簡(jiǎn)單的,對于JCE來(lái)講支技DES,DESede,Blowfish三種加密術(shù)
對于密鑰的保存各傳送可使用對象流或者用二進(jìn)制編碼,相關(guān)參考代碼如下
SecretKey deskey = keygen.generateKey();
byte[] desEncode=deskey.getEncoded();
javax.crypto.spec.SecretKeySpec destmp=new javax.crypto.spec.SecretKeySpec(desEncode,Algorithm);
SecretKey mydeskey=destmp;
相關(guān)API
KeyGenerator 在DSA中已經(jīng)說(shuō)明,在添加JCE后在instance進(jìn)可以如下參數
DES,DESede,Blowfish,HmacMD5,HmacSHA1
javax.crypto.Cipher 加/解密器 public static final Cipher getInstance(java.lang.String transformation)
throws java.security.NoSuchAlgorithmException,
NoSuchPaddingException
返回一個(gè)指定方法的Cipher對象
參數:transformation 方法名(可用 DES,DESede,Blowfish)
public final void init(int opmode, java.security.Key key)
throws java.security.InvalidKeyException
用指定的密鑰和模式初始化Cipher對象
參數:opmode 方式(ENCRYPT_MODE, DECRYPT_MODE, WRAP_MODE,UNWRAP_MODE)
key 密鑰
public final byte[] doFinal(byte[] input)
throws java.lang.IllegalStateException,
IllegalBlockSizeException,
BadPaddingException
對input內的串,進(jìn)行編碼處理,返回處理后二進(jìn)制串,是返回解密文還是加解文由init時(shí)的opmode決定
注意:本方法的執行前如果有update,是對updat和本次input全部處理,否則是本inout的內容
/*
*/
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就要把它添加進(jìn)去
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("加密前的二進(jìn)串:"+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("加密后的二進(jìn)串:"+byte2hex(cipherByte));
//解密
c1 = Cipher.getInstance(Algorithm);
c1.init(Cipher.DECRYPT_MODE,deskey);
byte[] clearByte=c1.doFinal(cipherByte);
System.out.println("解密后的二進(jìn)串:"+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();
}
}
2.5. Diffie-Hellman密鑰一致協(xié)議
公開(kāi)密鑰密碼體制的奠基人Diffie和Hellman所提出的 "指數密鑰一致協(xié)議"(Exponential Key Agreement Protocol),該協(xié)議不要求別的安全性 先決條件,允許兩名用戶(hù)在公開(kāi)媒體上交換信息以生成"一致"的,可以共享的密鑰。在JCE的中實(shí)現用戶(hù)alice生成DH類(lèi)型的密鑰對,如果長(cháng)度用1024生成的
System.out.println("ALICE: 產(chǎn)生 DH 對 ...");
KeyPairGenerator aliceKpairGen = KeyPairGenerator.getInstance("DH");
aliceKpairGen.initialize(512);
KeyPair aliceKpair = aliceKpairGen.generateKeyPair();
alice生成公鑰發(fā)送組bob byte[] alicePubKeyEnc = aliceKpair.getPublic().getEncoded();
bob從alice發(fā)送來(lái)的公鑰中讀出DH密鑰對的初始參數生成bob的DH密鑰對
注意這一步一定要做,要保證每個(gè)用戶(hù)用相同的初始參數生成的
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已經(jīng)生成了他的DES密鑰,他現把他的公鑰發(fā)給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能過(guò)這個(gè)過(guò)程就生成了相同的DES密鑰,在這種基礎就可進(jìn)行安全能信
常用API
java.security.KeyPairGenerator 密鑰生成器類(lèi)
public static KeyPairGenerator getInstance(String algorithm)
throws NoSuchAlgorithmException
以指定的算法返回一個(gè)KeyPairGenerator 對象
參數: algorithm 算法名.如:原來(lái)是DSA,現在添加了 DiffieHellman(DH)
public void initialize(int keysize)
以指定的長(cháng)度初始化KeyPairGenerator對象,如果沒(méi)有初始化系統以1024長(cháng)度默認設置
參數:keysize 算法位長(cháng).其范圍必須在 512 到 1024 之間,且必須為 64 的倍數
注意:如果用1024生長(cháng)的時(shí)間很長(cháng),最好生成一次后就保存,下次就不用生成了
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
以指定的算法返回一個(gè)KeyFactory
參數: algorithm 算法名:DSH,DH
public final PublicKey generatePublic(KeySpec keySpec)
throws InvalidKeySpecException
根據指定的key說(shuō)明,返回一個(gè)PublicKey對象
java.security.spec.X509EncodedKeySpec
public X509EncodedKeySpec(byte[] encodedKey)
根據指定的二進(jìn)制編碼的字串生成一個(gè)key的說(shuō)明
參數:encodedKey 二進(jìn)制編碼的字串(一般能過(guò)PublicKey.getEncoded()生成)
javax.crypto.KeyAgreement 密碼一至類(lèi)
public static final KeyAgreement getInstance(java.lang.String algorithm)
throws java.security.NoSuchAlgorithmException
返回一個(gè)指定算法的KeyAgreement對象
參數:algorithm 算法名,現在只能是DiffieHellman(DH)
public final void init(java.security.Key key)
throws java.security.InvalidKeyException
用指定的私鑰初始化
參數:key 一個(gè)私鑰
public final java.security.Key doPhase(java.security.Key key,
boolean lastPhase)
throws java.security.InvalidKeyException,
java.lang.IllegalStateException
用指定的公鑰進(jìn)行定位,lastPhase確定這是否是最后一個(gè)公鑰,對于兩個(gè)用戶(hù)的
情況下就可以多次定次,最后確定
參數: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: 產(chǎn)生 DH 對 ...");
KeyPairGenerator aliceKpairGen = KeyPairGenerator.getInstance("DH");
aliceKpairGen.initialize(512);
KeyPair aliceKpair = aliceKpairGen.generateKeyPair(); //生成時(shí)間長(cháng)
// 張三(Alice)生成公共密鑰 alicePubKeyEnc 并發(fā)送給李四(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發(fā)給他的公開(kāi)密鑰,
//中讀出參數,再用這個(gè)參數初始化他的 DH key對
//從alicePubKye中取alice初始化時(shí)用的參數
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 并發(fā)送給Alice,
//比如用文件方式,socket.....,使其生成本地密鑰
byte[] bobPubKeyEnc = bobKpair.getPublic().getEncoded();
System.out.println("BOB向ALICE發(fā)送公鑰");
// alice接收到 bobPubKeyEnc后生成bobPubKey
// 再進(jìn)行定位,使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是相同的所以,完全可以進(jìn)行發(fā)送加密,接收后解密,達到
//安全通道的的目的
/*
* 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章 小結
在加密術(shù)中生成密鑰對時(shí),密鑰對的當然是越長(cháng)越好,但費時(shí)也越多,請從中從實(shí)際出發(fā)選取合適的長(cháng)度,大部分例碼中的密鑰是每次運行就從新生成,在實(shí)際的情況中是生成后在一段時(shí)間保存在文件中,再次運行直接從文件中讀入,從而加快速度。當然定時(shí)更新和加強密鑰保管的安全性也是必須的。
聯(lián)系客服