RSA因子分解算法
唯一广泛接受的实现类型
数字加密&数字签名
公钥加密 私钥解密
私钥加密 公钥解密
工作实现以及填充模式
文章插图
加解密流程
文章插图
package asymmetrickeyencryption;import org.apache.commons.codec.binary.Base64;import javax.crypto.Cipher;import java.security.*;import java.security.interfaces.RSAPrivateKey;import java.security.interfaces.RSAPublicKey;import java.security.spec.PKCS8EncodedKeySpec;import java.security.spec.X509EncodedKeySpec;/*** 大数因子分解 64的整数倍*/public class RSA {private static final String BASE_STRING = "security DHTest";public static void main(String[] args) throws Exception {jdkRSA();}public static void jdkRSA() throws Exception {//1.初始化秘钥KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");keyPairGenerator.initialize(512);KeyPair keyPair = keyPairGenerator.generateKeyPair();RSAPublicKey rsaPublicKey = (RSAPublicKey) keyPair.getPublic();RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate();System.out.println("RSAPublicKey:" + Base64.encodeBase64String(rsaPublicKey.getEncoded()));System.out.println("RSAPrivateKey:" + Base64.encodeBase64String(rsaPrivateKey.getEncoded()));//2.私钥加密PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(rsaPrivateKey.getEncoded());KeyFactory keyFactory = KeyFactory.getInstance("RSA");PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);Cipher cipher = Cipher.getInstance("RSA");cipher.init(Cipher.ENCRYPT_MODE, privateKey);byte[] result = cipher.doFinal(BASE_STRING.getBytes());System.out.println("JDK RSA 私钥加密:" + Base64.encodeBase64String(result));//公钥解密X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(rsaPublicKey.getEncoded());KeyFactory keyFactory1 = KeyFactory.getInstance("RSA");PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);Cipher cipher1 = Cipher.getInstance("RSA");cipher1.init(Cipher.DECRYPT_MODE, publicKey);result = cipher1.doFinal(result);System.out.println("JDK RSA 公钥解密:" + new String(result));//3 公钥加密x509EncodedKeySpec = new X509EncodedKeySpec(rsaPublicKey.getEncoded());keyFactory = KeyFactory.getInstance("RSA");publicKey = keyFactory.generatePublic(x509EncodedKeySpec);cipher = Cipher.getInstance("RSA");cipher.init(Cipher.ENCRYPT_MODE, publicKey);result = cipher.doFinal(BASE_STRING.getBytes());System.out.println("JDK RSA 公钥加密:" + Base64.encodeBase64String(result));//私钥解密pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(rsaPrivateKey.getEncoded());keyFactory = KeyFactory.getInstance("RSA");privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);cipher = Cipher.getInstance("RSA");cipher.init(Cipher.DECRYPT_MODE, privateKey);result = cipher.doFinal(result);System.out.println("JDK RSA 私钥解密:" + new String(result));}}
离散对数加密算法
工作实现以及填充模式
文章插图
加解密流程
文章插图
秘钥的构建
public static void BCElGamal() throws Exception {//添加bc加密工具Security.addProvider(new BouncyCastleProvider());//生成秘钥AlgorithmParameterGenerator algorithmParameterGenerator = AlgorithmParameterGenerator.getInstance("ElGamal");algorithmParameterGenerator.init(256);AlgorithmParameters algorithmParameters = algorithmParameterGenerator.generateParameters();DHParameterSpec dhParameterSpec = algorithmParameters.getParameterSpec(DHParameterSpec.class);KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("ElGamal");keyPairGenerator.initialize(dhParameterSpec,new SecureRandom());KeyPair keyPair = keyPairGenerator.generateKeyPair();PublicKey publicKey = keyPair.getPublic();PrivateKey privateKey = keyPair.getPrivate();System.out.println("ElGamal 公钥:" + Base64.encodeBase64String(publicKey.getEncoded()));System.out.println("ElGamal 私钥:" + Base64.encodeBase64String(privateKey.getEncoded()));}
- java学习笔记2--面向对象编程
- java中静态块的作用,调用顺序和使用方法
- 一 微信公众平台开发JAVA验证来自微信服务器的消息
- 4 零基础学java---方法method
- 海贼王在中国哪年上映
- 1、改变参考文献在正文和末尾中的颜色
- 快速调整毕业论文格式:Word中给表格设置不同磅值的内外框
- 受较强冷空气影响中东部地区将迎降温降雨 广东广西局地有暴雨到大暴雨
- Redis 中的 流水线与事务 流水线
- 基础概念:书中的域、final关键字