RSA是目前使用最多的公钥加密算法,可以抵抗大多数已知的密码攻击,被ISO推荐为公钥数据加密标准。
在RSA算法中,每个通信主体有两个密钥,一个公钥(Public Key )用于加密数据。 用于解密数据的私钥。
让我们看看如何在Java中使用密钥生成器生成密钥,并从中分析PublickKey和PrivateKey。
如图所示,制作key map寒暑initKey
公共静态映射init key () throws Exception {
keypairgeneratorkeypairgen=key pair generator.getinstance (key _ algorithm;
keypairgen.initialize(1024;
key pair key pair=key pair gen.generate key pair (;
RSA public key public key=(RSA public key ) keyPair.getPublic;
RSA private key private key=(RSA private key ) keyPair.getPrivate;
mapkeymap=newHashmap(2;
kymap.put(public_key,publicKey );
kymap.put(private_key,privateKey );
返回密钥映射;
}
返回相应的公钥私钥:
publicstaticstringgetpublickey (map keymap ) throws Exception {
keykey=(key ) keymap.get ) public_key;
byte [ ]公共key=key.get encoded (;
returnencryptbase 64 (key.get encoded );
}
publicstaticstringgetprivatekey (map keymap ) throws Exception {
keykey=(key ) keymap.get ) private_key;
byte [ ] private key=key.get encoded (;
returnencryptbase 64 (key.get encoded );
}
public static byte [ ] decrypt base64 (string key ) throws Exception {
return(newbase64decoder () ).decodebuffer ) ) key;
}
publicstaticstringencryptbase 64 (byte [ ] key ) throws Exception {
return(newbase64encoder () ).encodebuffer ) )密钥;
}
完整代码:
import java.security.Key;
import java.security.KeyPair;
import Java.security.key pair generator;
import Java.security.interfaces.RSA private key;
import Java.security.interfaces.RSA public key;
import java.util.HashMap;
import java.util.Map;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
@suppresswarnings('unused”) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) )。) ) )
公共类资源测试{
publicstaticfinalstringkey _ algorithm=' RSA ';
publicstaticfinalstringsignature _ algorithm=' m D5 with RSA ';
privatestaticfinalstringpublic _ key=' RSA public key ';
privatestaticfinalstringprivate _ key=' RSA private key ';
publicstaticstringgetpublickey (map keymap ) throws Exception {
keykey=(key ) keymap.get ) public_key;
byte [ ]公共key=key.get encoded (;
returnencryptbase 64 (key.get encoded );
}
publicstaticstringgetprivatekey (map keymap ) throws Exception {
keykey=(key ) keymap.get ) private_key;
byte [ ] private key=key.get encoded (;
returnencryptbase 64 (key.get encoded );
}
public static byte [ ] decrypt base64 (string key ) throws Exception {
return(newbase64decoder () ).decodebuffer ) ) key;
}
publicstaticstringencryptbase 64 (byte [ ] key ) throws Exception {
return(newbase64encoder () ).encodebuffer ) )密钥;
}
publicstaticmapinitkey { } throws exception {
keypairgeneratorkeypairgen=key pair generator.getinstance (key _ algorithm;
keypairgen.initialize(1024;
key pair key pair=key pair gen.generate key pair (;
RSA public key public key=(RSA public key ) keyPair.getPublic;
RSA private key private key=(RSA private key ) keyPair.getPrivate;
mapkeymap=newHashmap(2;
kymap.put(public_key,publicKey );
kymap.put(private_key,privateKey );
返回密钥映射;
}
publicstaticvoidmain (字符串[ ] args ) {
映射密钥;
try {
keyMap=initKey (;
string public key=get public key (keymap );
system.out.println(publickey );
string private key=get private key (keymap;
system.out.println(privatekey;
}catch(exceptione ) {
e .打印堆栈跟踪(;
}
}
}
执行结果如下。