/*
* 密钥交换算法,即非对称加密算法
* */
public
class
DHCoder {
//非对称加密算法
public
static
final
String
KEY_ALGORITHM
=
“DH”
;
//本地密钥算法,即对称加密算法
public
static
final
String
SECRET_ALGORITHM
=
“AES”
;
private
static
final
int
KEY_SIZE
=512;
//公钥
private
static
final
String
PUBLIC_KEY
=
“DHPublicKey”
;
//私钥
private
static
final
String
PRIVATE_KEY
=
“DHPrivateKey”
;
//初始化甲方密钥
public
static
Map<String,Object> initKey()
throws
Exception{
//密钥对生成器
KeyPairGenerator keyPairGenerator=KeyPairGenerator.getInstance(
KEY_ALGORITHM
);
keyPairGenerator.initialize(
KEY_SIZE
);
KeyPair keyPair=keyPairGenerator.generateKeyPair();
//甲方公钥
DHPublicKey publicKey=(DHPublicKey)keyPair.getPublic();
//甲方私钥
DHPrivateKey privateKey=(DHPrivateKey)keyPair.getPrivate();
//将密钥对存储在Map中
Map<String,Object> keyMap=
new
HashMap<String,Object>(2);
keyMap.put(
PUBLIC_KEY
,publicKey);
keyMap.put(
PRIVATE_KEY
,privateKey);
return
keyMap;
}
//乙方密钥
public
static
Map<String,Object> initKey(
byte
[] key)
throws
Exception{
//解析甲方公钥,转换公钥规范
X509EncodedKeySpec x509KeySpec=
new
X509EncodedKeySpec(key);
KeyFactory keyFactory=KeyFactory. getInstance(
KEY_ALGORITHM
);
//产生甲方公钥
PublicKey publicKey=keyFactory.generatePublic(x509KeySpec);
//由甲方公钥构建乙方密钥对
DHParameterSpec dhParamSpec=((DHPublicKey)publicKey).getParams();
KeyPairGenerator keyPairGenerator=KeyPairGenerator.getInstance(keyFactory.getAlgorithm());
keyPairGenerator.initialize(dhParamSpec);
//产生密钥对
KeyPair keyPair=keyPairGenerator.generateKeyPair();
//乙方公钥
DHPublicKey publicKey1=(DHPublicKey)keyPair.getPublic();
//乙方私钥
DHPrivateKey privateKey1=(DHPrivateKey)keyPair.getPrivate();
//将密钥存储在Map中
Map<String,Object> keyMap=
new
HashMap<String,Object>(2);
keyMap.put(
PRIVATE_KEY
, privateKey1);
keyMap.put(
PUBLIC_KEY
, publicKey1);
return
keyMap;
}
//加密
public
static
byte
[] encrypt(
byte
[] data,
byte
[] key)
throws
Exception, GeneralSecurityException{
//生成本地密钥
SecretKey secretKey=
new
SecretKeySpec(key,
SECRET_ALGORITHM
);
//加密数据
Cipher cipher=Cipher. getInstance(secretKey.getAlgorithm());
cipher.init(Cipher.
ENCRYPT_MODE
, secretKey);
return
cipher.doFinal(data);
}
//解密
public
static
byte
[] decrypt(
byte
[] data,
byte
[] key)
throws
Exception, NoSuchPaddingException{
//生成本地密钥
SecretKey secretKey=
new
SecretKeySpec(key,
SECRET_ALGORITHM
);
Cipher cipher=Cipher. getInstance(secretKey.getAlgorithm());
cipher.init(Cipher.
DECRYPT_MODE
, secretKey);
return
cipher.doFinal(data);
}
//由公钥,私钥,构建本地密钥
public
static
byte
[] getSecretKey(
byte
[] publicKey,
byte
[] privateKey)
throws
Exception{
KeyFactory keyFactory=KeyFactory. getInstance(
KEY_ALGORITHM
);
//初始化公钥
X509EncodedKeySpec x509EncodedKeySpec=
new
X509EncodedKeySpec(publicKey);
//产生公钥
PublicKey pubKey=keyFactory.generatePublic(x509EncodedKeySpec);
//初始化私钥,密钥规范转换
PKCS8EncodedKeySpec pkcs8KeySpec=
new
PKCS8EncodedKeySpec(privateKey);
//产生私钥
PrivateKey priKey=keyFactory.generatePrivate(pkcs8KeySpec);
//实例化
KeyAgreement keyAgree=KeyAgreement.getInstance(keyFactory.getAlgorithm());
keyAgree.init(priKey);
keyAgree.doPhase(pubKey,
true
);
//生成本地密钥
SecretKey secretKey=keyAgree.generateSecret(
SECRET_ALGORITHM
);
return
secretKey.getEncoded();
}
//取得私钥
public
static
byte
[] getPrivateKey(Map<String,Object> keyMap){
Key key=(Key)keyMap.get(
PRIVATE_KEY
);
return
key.getEncoded();
}
//取得公钥
public
static
byte
[] getPublicKey(Map<String,Object> keyMap){
Key key=(Key)keyMap.get(
PUBLIC_KEY
);
return
key.getEncoded();
}
}
验证===
public
class
DHCoderTest {
//初始化密钥
public
final
void
initKey()
throws
Exception{
}
public
static
void
main(String[] args)
throws
Exception {
//
TODO
Auto-generated method stub
/*
* 需要关注,甲乙双方本地密钥是否相同;一方加密,另一方是否可以解密
*/
//甲方公钥
byte
[] publicKey1;
//甲方私钥
byte
[] privateKey1;
//甲方本地密钥
byte
[] key1;
//乙方公钥
byte
[] publicKey2;
//一方私钥
byte
[] privateKey2;
//一方本地密钥
byte
[] key2;
//生成甲方密钥对
Map<String,Object> keyMap1=DHCoder. initKey();
privateKey1=DHCoder. getPrivateKey(keyMap1);
publicKey1=DHCoder. getPublicKey(keyMap1);
System.
out
.println(
“甲方公钥:\n”
+Base64.encodeBase64String (publicKey1));
System.
out
.println(
“甲方私钥:\n”
+Base64.encodeBase64String (privateKey1));
//由甲方产生乙方密钥对
Map<String,Object> keyMap2=DHCoder. initKey(publicKey1);
publicKey2=DHCoder. getPublicKey(keyMap2);
privateKey2=DHCoder. getPrivateKey(keyMap2);
System.
out
.println(
“乙方公钥:\n”
+Base64.encodeBase64String (publicKey2));
System.
out
.println(
“乙方私钥:\n”
+Base64.encodeBase64String (privateKey2));
//甲方私钥,乙方公钥,构造本地密钥
key1=DHCoder. getSecretKey(publicKey2, privateKey1);
System.
out
.println(
“甲方本地密钥:\n”
+Base64.encodeBase64String (key1));
//乙方私钥,甲方公钥,构造本地密钥
key2=DHCoder. getSecretKey(publicKey1, privateKey2);
System.
out
.println(
“乙方本地密钥:\n”
+Base64.encodeBase64String (key2));
//校验
assertArrayEquals(key1,key2);
}
}
非对称加密--密钥交换算法DHCoder
原文作者:徐小鱼
原文地址: http://www.cnblogs.com/littlefishxu/p/3969185.html
本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
原文地址: http://www.cnblogs.com/littlefishxu/p/3969185.html
本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。