关于java中rsa的问题【实例下载】本文介绍RSA2加密与解密,RSA2是RSA的加强版本,在密钥长度上采用2048, RSA2比RSA更安全,更可靠, 本人的另一篇文章RSA已经发表 , 有想了解的可以点开下面的RSA文章
java RSA 加解密 import java security Key;
import java security KeyFactory;
import java security KeyPair;
import java security KeyPairGenerator;
import java security PrivateKey;
import java security PublicKey;
import java security interfaces RSAPrivateKey;
import java security interfaces RSAPublicKey;
import java security spec PKCS EncodedKeySpec;
import java security spec X EncodedKeySpec;
import javax crypto Cipher;
import sun misc BASE Decoder;
import sun misc BASE Encoder;
public class RSACoder {
/**
* 得到公钥
* @param key 密钥字符串(经过base 编码)
* @throws Exception
*/
public static PublicKey getPublicKey(String key) throws Exception {
byte[] keyBytes;
keyBytes = (new BASE Decoder()) decodeBuffer(key);
X EncodedKeySpec keySpec = new X EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory getInstance( RSA );
PublicKey publicKey = keyFactory generatePublic(keySpec);
return publicKey;
}
/**
* 得到私钥
* @param key 密钥字符串(经过base 编码)
* @throws Exception
*/
public static PrivateKey getPrivateKey(String key) throws Exception {
byte[] keyBytes;
keyBytes = (new BASE Decoder()) decodeBuffer(key);
PKCS EncodedKeySpec keySpec = new PKCS EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory getInstance( RSA );
PrivateKey privateKey = keyFactory generatePrivate(keySpec);
return privateKey;
}
/**
* 得到密钥字符串(经过base 编码)
* @return
*/
public static String getKeyString(Key key) throws Exception {
byte[] keyBytes = key getEncoded();
String s = (new BASE Encoder()) encode(keyBytes);
return s;
}
public static void main(String[] args) throws Exception {
KeyPairGenerator keyPairGen = KeyPairGenerator getInstance( RSA );
//密钥位数
keyPairGen initialize( );
//密钥对
KeyPair keyPair = keyPairGen generateKeyPair();
// 公钥
PublicKey publicKey = (RSAPublicKey) keyPair getPublic();
// 私钥
PrivateKey privateKey = (RSAPrivateKey) keyPair getPrivate();
String publicKeyString = getKeyString(publicKey);
System out println( public:\n + publicKeyString);
String privateKeyString = getKeyString(privateKey);
System out println( private:\n + privateKeyString);
//加解密类
Cipher cipher = Cipher getInstance( RSA );//Cipher getInstance( RSA/ECB/PKCS Padding );
//明文
byte[] plainText = 我们都很好!邮件 getBytes();
//加密
cipher init(Cipher ENCRYPT_MODE publicKey);
byte[] enBytes = cipher doFinal(plainText);
//通过密钥字符串得到密钥
publicKey = getPublicKey(publicKeyString);
privateKey = getPrivateKey(privateKeyString);
//解密
cipher init(Cipher DECRYPT_MODE privateKey);
byte[]deBytes = cipher doFinal(enBytes);
publicKeyString = getKeyString(publicKey);
System out println( public:\n +publicKeyString);
privateKeyString = getKeyString(privateKey);
System out println( private:\n + privateKeyString);
String s = new String(deBytes);
System out println(s);
}
lishixinzhi/Article/program/Java/hx/201311/25516
RSA PKCS#1在java中怎么实现?楼主看看下面的代码是不是你所需要的,这是我原来用的时候收集的
import javax.crypto.Cipher;
import java.security.*;
import java.security.spec.RSAPublicKeySpec;
推荐阅读
- ERP应收系统怎么结账,erp应收系统怎么结账操作
- 新媒体如何布局,2020年新媒体怎么做
- 做电商如何申请公司,申请电商需要什么条件
- 手机虚拟机改imei,手机虚拟机改屏幕比例
- c语言求整数长度的函数 c语言中怎么求整数的位数
- c语言如何设计成程序,c语言程序设计怎么写程序
- pg数据库重命名表名,pgsql重命名表名
- flutter页面怎么切换,flutter布局技巧
- vb.net线程界面 vb 线程