java实现rsa非对称性密码加密与解密

上传人:第*** 文档编号:34046766 上传时间:2018-02-20 格式:DOCX 页数:6 大小:567.89KB
返回 下载 相关 举报
java实现rsa非对称性密码加密与解密_第1页
第1页 / 共6页
java实现rsa非对称性密码加密与解密_第2页
第2页 / 共6页
java实现rsa非对称性密码加密与解密_第3页
第3页 / 共6页
java实现rsa非对称性密码加密与解密_第4页
第4页 / 共6页
java实现rsa非对称性密码加密与解密_第5页
第5页 / 共6页
点击查看更多>>
资源描述

《java实现rsa非对称性密码加密与解密》由会员分享,可在线阅读,更多相关《java实现rsa非对称性密码加密与解密(6页珍藏版)》请在金锄头文库上搜索。

1、创建 RSAKeyMap.java 类package test_time;import java.security.Key;import java.security.KeyPair;import java.security.KeyPairGenerator;import java.security.interfaces.RSAPrivateKey;import java.security.interfaces.RSAPublicKey;import java.util.HashMap;import java.util.Map; import sun.misc.BASE64Decoder; im

2、port sun.misc.BASE64Encoder;SuppressWarnings(unused)public class RSAKeyMap public static final String KEY_ALGORITHM = RSA;public static final String SIGNATURE_ALGORITHM = MD5withRSA;private static final String PUBLIC_KEY = RSAPublicKey;private static final String PRIVATE_KEY = RSAPrivateKey;public s

3、tatic String getPublicKey(Map keyMap) throws Exception Key key = (Key) keyMap.get(PUBLIC_KEY); byte publicKey = key.getEncoded(); return encryptBASE64(key.getEncoded();public static String getPrivateKey(Map keyMap) throws Exception Key key = (Key) keyMap.get(PRIVATE_KEY); byte privateKey =key.getEnc

4、oded(); return encryptBASE64(key.getEncoded(); public static byte decryptBASE64(String key) throws Exception return (new BASE64Decoder().decodeBuffer(key); public static String encryptBASE64(byte key) throws Exception return (new BASE64Encoder().encodeBuffer(key); public static Map initKey() throws

5、Exception KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(KEY_ALGORITHM);keyPairGen.initialize(1024);KeyPair keyPair = keyPairGen.generateKeyPair();RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();Map keyMap = new

6、HashMap(2);keyMap.put(PUBLIC_KEY, publicKey);keyMap.put(PRIVATE_KEY, privateKey);return keyMap;/*public static void main(String args) Map keyMap;try keyMap = initKey();String publicKey = getPublicKey(keyMap);System.out.println(publicKey);String privateKey = getPrivateKey(keyMap);System.out.println(p

7、rivateKey); catch (Exception e) e.printStackTrace();*/测试截图将 main 方法注释掉创建 RSA.java 类package test_time;import java.io.UnsupportedEncodingException;import java.security.InvalidKeyException;import java.security.KeyFactory;import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;i

8、mport java.security.PrivateKey;import java.security.PublicKey;import java.security.SecureRandom;import java.security.spec.PKCS8EncodedKeySpec;import java.security.spec.X509EncodedKeySpec;import java.util.Map;import javax.crypto.BadPaddingException;import javax.crypto.Cipher;import javax.crypto.Illeg

9、alBlockSizeException;import javax.crypto.KeyGenerator;import javax.crypto.NoSuchPaddingException;import javax.crypto.SecretKey;import com.sun.mail.util.BASE64DecoderStream;import com.sun.mail.util.BASE64EncoderStream;import sun.misc.BASE64Decoder;public class RSA /* 传入名文和公钥钥对数据进行 RSA 解密* 返回值:String*

10、 param src* param pubkey* return*/public static String rsaEncoding(String src,PublicKey pubkey)try Cipher cip = Cipher.getInstance(RSA);cip.init(cip.ENCRYPT_MODE, pubkey);byte by = cip.doFinal(src.getBytes();return new String(BASE64EncoderStream.encode(by); catch (NoSuchAlgorithmException e) throw n

11、ew RuntimeException(e); catch (NoSuchPaddingException e) throw new RuntimeException(e); catch (InvalidKeyException e) throw new RuntimeException(e); catch (IllegalBlockSizeException e) throw new RuntimeException(e); catch (BadPaddingException e) throw new RuntimeException(e);/* 传入 RSA 密文和私钥对数据进行解密 *

12、 返回值:String* param sec* param privkey* return*/public static String rsaDeEncoding(String sec,PrivateKey privkey)try Cipher cip = Cipher.getInstance(RSA);cip.init(cip.DECRYPT_MODE, privkey);byte by = BASE64DecoderStream.decode(sec.getBytes();return new String(cip.doFinal(by); catch (NoSuchAlgorithmEx

13、ception e) throw new RuntimeException(e); catch (NoSuchPaddingException e) throw new RuntimeException(e); catch (InvalidKeyException e) throw new RuntimeException(e); catch (IllegalBlockSizeException e) throw new RuntimeException(e); catch (BadPaddingException e) throw new RuntimeException(e);/* 将 S

14、tring 类型公钥转换成 PublicKey 类型* param key* return* throws Exception*/public static PublicKey getPublicKey(String key) throws Exception byte keyBytes;keyBytes = (new BASE64Decoder().decodeBuffer(key);X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);KeyFactory keyFactory = KeyFactory.getInsta

15、nce(RSA);PublicKey publicKey = keyFactory.generatePublic(keySpec);return publicKey;/* 将 String 类型私钥转换成 PrivateKey 类型* param key* return* throws Exception */public static PrivateKey getPrivateKey(String key) throws Exception byte keyBytes;keyBytes = (new BASE64Decoder().decodeBuffer(key);PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);KeyFactory keyFactory = KeyFactory.getInstance(RSA);PrivateKey privateKey = keyFactory.generatePrivate(keySpec);return privateKey;需要在 lib 包里面导入 包mail.jar测试:public static void main(String args) RS

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > 解决方案

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号