java通過AES生成公鑰加密數(shù)據(jù)ECC加密公鑰
成功

本文通過java語言實現(xiàn)ECC+AES混合加密。
ECC加密算法具有密鑰分配與管理簡單,安全強度高等優(yōu)點,AES的加密算法具有速度快,強度高,便于實現(xiàn)等優(yōu)點。Ecc橢圓曲線算法對AES公鑰進(jìn)行加密管理,AES主要為我們數(shù)據(jù)進(jìn)行加密。
通過集成AES加密算法和ECC加密算法的優(yōu)點,實現(xiàn)了加密速度快和安全方便管理密鑰的優(yōu)點,有效地解決了密碼體制中速度和安全性不能兼顧的問題。JDK中自帶了橢圓曲線的簽名,但是沒有實現(xiàn)橢圓曲線的加密解密。通過引入bouncycastle庫實現(xiàn)實現(xiàn)橢圓曲線的加密解密。
- 去JDK的下載頁面,下載
www.oracle.com/technetwork…這個東西。這個是為了解除默認(rèn)JDK中的加密強度的限制。不使用這個可能會報錯。
下載下來以后,需要將local_policy.jar 和 US_export_policy.jar替換掉D:\Program Files\Java\jdk1.8.0_91\jre\lib\security下面的相同的兩個jar包。
基于AES的加密算法具有速度快,強度高,便于實現(xiàn)等優(yōu)點
ECC加密算法具有密鑰分配與管理簡單,安全強度高等優(yōu)點
采用AES加密算法加密大數(shù)據(jù)塊, 而用ECC加密算法管理AES密鑰
通過集成AES加密算法和ECC加密算法的優(yōu)點,實現(xiàn)了加密速度快和安全方便管理密鑰的優(yōu)點,有效地解決了密碼體制中速度和安全性不能兼顧的問題.
AES加密標(biāo)準(zhǔn)又稱為高級加密標(biāo)準(zhǔn)Rijndael加密法,是美國國家標(biāo)準(zhǔn)技術(shù)研究所NIST旨在取代DES的21世紀(jì)的加密標(biāo)準(zhǔn)。AES的基本要求是,采用對稱分組密碼體制,密鑰長度可以為128、192或256位,分組長度128位,算法應(yīng)易在各種硬件和軟件上實現(xiàn)。
AES屬于對稱加密算法;加密、解密使用相同的密鑰,AES加解密過程如下圖所示:

通過AES獲取公鑰和私鑰
package utils;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.io.IOException;
public class AESUtil {
//生成AES秘鑰,然后Base64編碼
public static String genKeyAES() throws Exception{
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
keyGen.init(128);
SecretKey key = keyGen.generateKey();
String base64Str = byte2Base64(key.getEncoded());
return base64Str;
}
//將Base64編碼后的AES秘鑰轉(zhuǎn)換成SecretKey對象
public static SecretKey loadKeyAES(String base64Key) throws Exception{
byte[] bytes = base642Byte(base64Key);
SecretKeySpec key = new SecretKeySpec(bytes, "AES");
return key;
}
//加密
public static byte[] encryptAES(byte[] source, SecretKey key) throws Exception{
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, key);
return cipher.doFinal(source);
}
//解密
public static byte[] decryptAES(byte[] source, SecretKey key) throws Exception{
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, key);
return cipher.doFinal(source);
}
//字節(jié)數(shù)組轉(zhuǎn)Base64編碼
public static String byte2Base64(byte[] bytes){
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(bytes);
}
//Base64編碼轉(zhuǎn)字節(jié)數(shù)組
public static byte[] base642Byte(String base64Key) throws IOException {
BASE64Decoder decoder = new BASE64Decoder();
return decoder.decodeBuffer(base64Key);
}
}
進(jìn)行ECC加密
package utils;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.Security;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import javax.crypto.Cipher;
import org.bouncycastle.jce.interfaces.ECPrivateKey;
import org.bouncycastle.jce.interfaces.ECPublicKey;
public class ECCUtil {
static {
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
}
//生成秘鑰對
public static KeyPair getKeyPair() throws Exception {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("EC", "BC");
keyPairGenerator.initialize(256, new SecureRandom());
KeyPair keyPair = keyPairGenerator.generateKeyPair();
return keyPair;
}
//獲取公鑰(Base64編碼)
public static String getPublicKey(KeyPair keyPair){
ECPublicKey publicKey = (ECPublicKey) keyPair.getPublic();
byte[] bytes = publicKey.getEncoded();
return AESUtil.byte2Base64(bytes);
}
//獲取私鑰(Base64編碼)
public static String getPrivateKey(KeyPair keyPair){
ECPrivateKey privateKey = (ECPrivateKey) keyPair.getPrivate();
byte[] bytes = privateKey.getEncoded();
return AESUtil.byte2Base64(bytes);
}
//將Base64編碼后的公鑰轉(zhuǎn)換成PublicKey對象
public static ECPublicKey string2PublicKey(String pubStr) throws Exception{
byte[] keyBytes = AESUtil.base642Byte(pubStr);
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("EC", "BC");
ECPublicKey publicKey = (ECPublicKey) keyFactory.generatePublic(keySpec);
return publicKey;
}
//將Base64編碼后的私鑰轉(zhuǎn)換成PrivateKey對象
public static ECPrivateKey string2PrivateKey(String priStr) throws Exception{
byte[] keyBytes = AESUtil.base642Byte(priStr);
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("EC", "BC");
ECPrivateKey privateKey = (ECPrivateKey) keyFactory.generatePrivate(keySpec);
return privateKey;
}
//公鑰加密
public static byte[] publicEncrypt(byte[] content, PublicKey publicKey) throws Exception{
Cipher cipher = Cipher.getInstance("ECIES", "BC");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] bytes = cipher.doFinal(content);
return bytes;
}
//私鑰解密
public static byte[] privateDecrypt(byte[] content, PrivateKey privateKey) throws Exception{
Cipher cipher = Cipher.getInstance("ECIES", "BC");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] bytes = cipher.doFinal(content);
return bytes;
}
public static void main(String[] args) throws Exception {
KeyPair keyPair = ECCUtil.getKeyPair();
String publicKeyStr = ECCUtil.getPublicKey(keyPair);
String privateKeyStr = ECCUtil.getPrivateKey(keyPair);
System.out.println("ECC公鑰Base64編碼:" + publicKeyStr);
System.out.println("ECC私鑰Base64編碼:" + privateKeyStr);
ECPublicKey publicKey = string2PublicKey(publicKeyStr);
ECPrivateKey privateKey = string2PrivateKey(privateKeyStr);
byte[] publicEncrypt = publicEncrypt("hello world".getBytes(), publicKey);
byte[] privateDecrypt = privateDecrypt(publicEncrypt, privateKey);
System.out.println(new String(privateDecrypt));
}
}以上就是java通過AES生成公鑰加密數(shù)據(jù)ECC加密公鑰的詳細(xì)內(nèi)容,更多關(guān)于java AES生成公鑰加密數(shù)據(jù)ecc的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Java網(wǎng)絡(luò)編程之TCP程序設(shè)計
這篇文章主要為大家詳細(xì)介紹了Java網(wǎng)絡(luò)編程之TCP程序設(shè)計,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-08-08

