私钥加密-->公钥解密,反之亦然,但不安全。也可以当做数字签名。


public class RSACoder
{

        //非对称加密算法
        public static final String KEY_ALGORITHM= "RSA";
        //公钥
        private static final String PUBLIC_KEY= "RSAPublicKey";
        //私钥
        private static final String PRIVATE_KEY= "RSAPrivateKey";
        //密钥长度,默认1024位,512~65536位,必须是64的倍数
        private static final int KEY_SIZE=512;
       
        //私钥解密
        public static byte[]
decryptByPrivateKey(byte[] data, byte[]
key) throws KeyException, Exception{
               //取得私钥,
              PKCS8EncodedKeySpec pkcs8KeySpec= new PKCS8EncodedKeySpec(key);
              KeyFactory keyFactory=KeyFactory. getInstance(KEY_ALGORITHM);
              PrivateKey privateKey=keyFactory.generatePrivate(pkcs8KeySpec);
              
               //对数据解密
              Cipher cipher=Cipher. getInstance(keyFactory.getAlgorithm());
              cipher.init(Cipher. DECRYPT_MODE,
privateKey);
               return cipher.doFinal(data);
       }
       
        //公钥解密
        public static byte[]
decryptByPublicKey(byte[] data, byte[]
key) throws Exception{
              X509EncodedKeySpec x509= new X509EncodedKeySpec(key);
              KeyFactory keyFactory=KeyFactory. getInstance(KEY_ALGORITHM);
              
               //生成公钥
              PublicKey publicKey=keyFactory.generatePublic(x509);
               //对数据解密
              Cipher cipher=Cipher. getInstance(keyFactory.getAlgorithm());
              cipher.init(Cipher. DECRYPT_MODE,
publicKey);
               return cipher.doFinal(data);
       }
       
        //公钥加密
        public static byte[]
encrpytByPublicKey(byte[] data, byte[]
key) throws Exception{
               //取得公钥
              X509EncodedKeySpec x509= new X509EncodedKeySpec(key);
              KeyFactory keyFactory=KeyFactory. getInstance(KEY_ALGORITHM);
              PublicKey pubKey=keyFactory.generatePublic(x509);
               //对数据加密
              Cipher cipher=Cipher. getInstance(keyFactory.getAlgorithm());
              cipher.init(Cipher. ENCRYPT_MODE,
pubKey);
               return cipher.doFinal(data);
       }
       
        //私钥加密
        public static byte[]
encryptByPrivate(byte[] data, byte[]
key) throws Exception, GeneralSecurityException{
               //取得私钥
              PKCS8EncodedKeySpec pkcs8= new PKCS8EncodedKeySpec(key);
              KeyFactory keyFactory=KeyFactory. getInstance(KEY_ALGORITHM);
               //生成私钥
              PrivateKey privateKey=keyFactory.generatePrivate(pkcs8);
               //对数据加密
              Cipher cipher=Cipher. getInstance(keyFactory.getAlgorithm());
              cipher.init(Cipher. ENCRYPT_MODE,
privateKey);
               return cipher.doFinal(data);
       }
       
        //取得私钥
        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 static Map<String,Object>
initKey() throws Exception{
              KeyPairGenerator keyPairGen=KeyPairGenerator.getInstanceKEY_ALGORITHM);
              keyPairGen.initialize( KEY_SIZE);
              KeyPair keyPair=keyPairGen.generateKeyPair();
              
               //公钥
              RSAPublicKey publicKey=(RSAPublicKey)keyPair.getPublic();
               //私钥
              RSAPrivateKey privateKey=(RSAPrivateKey)keyPair.getPrivate();
              
               //封装密钥
              Map<String,Object> keyMap= new HashMap<String,Object>(2);
              keyMap.put( PRIVATE_KEY,
privateKey);
              keyMap.put( PUBLIC_KEY,
publicKey);
               return keyMap;
       }

}


public class RSATest
{

        private static byte[] privateKey;
        private static byte[] publicKey;
       
        public static void initKey() throws Exception{
               //初始化密钥
              Map<String,Object> keyMap=RSACoder. initKey();
               publicKey=RSACoder.getPublicKey(keyMap);
               privateKey=RSACoder.getPrivateKey(keyMap);
              
              System. out.println("公钥:\n" +Base64.encodeBase64String (publicKey ));
              System. out.println("私钥:\n" +Base64.encodeBase64String (privateKey ));
       }
        public static void main(String[]
args) throws GeneralSecurityException,
Exception {
               // TODO Auto-generated
method stub
               initKey();
              String inputStr1= "RSA加密算法" ;
               byte[]
data1=inputStr1.getBytes();
              System. out.println("原文:\n" +inputStr1);
              
               //私钥加密
               byte[]
encodeData1=RSACoder.encryptByPrivate(data1, privateKey);
              System. err.println("加密后:\n" +Base64.encodeBase64String (encodeData1));
              
               //公钥解密
               byte[]
decodeData1=RSACoder.decryptByPublicKey(encodeData1, publicKey);
              String outputStr1= new String(decodeData1);
              System. err.println("解密后:\n" +outputStr1);
              
               assertEquals(inputStr1,outputStr1);
              
       }

}

RSA典型非对称加密算法的更多相关文章

  1. JAVA实现RSA加密,非对称加密算法

    RSA.java package org.icesnow.jeasywx.util.security; import java.security.Key; import java.security.K ...

  2. 非对称加密算法-RSA算法

    一.概述 1.RSA是基于大数因子分解难题.目前各种主流计算机语言都支持RSA算法的实现 2.java6支持RSA算法 3.RSA算法可以用于数据加密和数字签名 4.RSA算法相对于DES/AES等对 ...

  3. SSH加密原理、RSA非对称加密算法学习与理解

    首先声明一下,这里所说的SSH,并不是Java传统的三大框架,而是一种建立在应用层和传输层基础上的安全外壳协议,熟悉Linux的朋友经常使 用到一 个SSH Secure Shell Cilent的工 ...

  4. 非对称加密算法-RSA

    注意:本节内容主要参考自<Java加密与解密的艺术(第2版)>第8章“高等加密算法--非对称加密算法” 12.1.RSA(最经典的非对称加密算法) 特点: 使用一套密钥即可完成加解密(与D ...

  5. 信息加密之非对称加密算法RSA

    前面为大家已经总结了,基于密钥交换的DH算法,现在就为大家再介绍一种基于因子分解的RSA算法,这种加密算法有两种实现形式:1.公钥加密,私钥解密:2.私钥加密,公钥解密.下面就为大家分析一下实现代码, ...

  6. 【Java】聊聊常用的非对称加密算法之一RSA的使用(Java)

    参考的优秀文章 Java加密技术(四)——非对称加密算法RSA RSA算法原理(一).RSA算法原理(二) RSA的公钥和私钥到底哪个才是用来加密和哪个用来解密? 简单的介绍 RSA是有名的非对称加密 ...

  7. 第十二章 非对称加密算法-RSA

    注意:本节内容主要参考自<Java加密与解密的艺术(第2版)>第8章“高等加密算法--非对称加密算法” 12.1.RSA(最经典的非对称加密算法) 特点: 使用一套密钥即可完成加解密(与D ...

  8. Java 加密解密 对称加密算法 非对称加密算法 MD5 BASE64 AES RSA

    版权声明:本文为博主原创文章,未经博主允许不得转载. [前言] 本文简单的介绍了加密技术相关概念,最后总结了java中现有的加密技术以及使用方法和例子 [最简单的加密] 1.简单的概念 明文:加密前的 ...

  9. openssl 非对称加密算法RSA命令详解

    1.非对称加密算法概述 非对称加密算法也称公开密钥算法,其解决了对称加密算法密钥分配的问题,非对称加密算法基本特点如下: 1.加密密钥和解密密钥不同 2.密钥对中的一个密钥可以公开 3.根据公开密钥很 ...

随机推荐

  1. Windows10电脑优化和使用

    本文将结合自身经验和短视频软件中的优化技巧,推荐一些Win10系统的优化和使用小技巧. 电脑优化 新电脑调出我的电脑等桌面图标: 右键桌面,选择个性化,左侧选择主题,在相关的设置中找到桌面图标设置,将 ...

  2. woj1002-Genesis woj1003-birthofnoah woj1004-noah's ark

    title: woj1002-Genesis date: 2020-03-05 categories: acm tags: [acm,woj] 输入输出考虑一下.easy #include <i ...

  3. Spring(一)概述

    Spring 的前世今生 相信经历过不使用框架开发 Web 项目的 70 后.80 后都会有如此感触,如今的程序员开发项目太轻松 了,基本只需要关心业务如何实现,通用技术问题只需要集成框架便可.早在 ...

  4. js Nullish Coalescing Operator

    js Nullish Coalescing Operator 空值合并 const n = null ?? 'default string'; console.log(n); // "def ...

  5. YouTube 视频下载工具

    YouTube 视频下载工具 我不生产视频,只是优秀视频的搬运工! YouTube-dl https://github.com/search?q=youtube-dl https://github.c ...

  6. Android Jetpack All In One

    Android Jetpack All In One 在2018年,我们推出了Android Jetpack作为一组库,以帮助开发人员遵循最佳实践,减少样板代码以及编写可在Android版本和设备之间 ...

  7. React + GraphQL 2020 速成课程

    React + GraphQL 2020 速成课程 technologies React (to build our user interface) GraphQL (to get and chang ...

  8. how to using Linux pipe command output another command's help content to a file

    how to using Linux pipe command output another command's help content to a file Linux tee > >& ...

  9. CSS3 & Flex Layout All In One

    CSS3 & Flex Layout All In One demos https://www.cnblogs.com/xgqfrms/p/10769302.html .flex-contai ...

  10. jsbridge 原理 & 通信原理

    jsbridge 原理 & 通信原理 Hybrid 方案是基于 WebView 的,JavaScript 执行在 WebView 的 Webkit 引擎中; 因此,Hybrid 方案中 JSB ...