教学服务系统

 找回密码
 立即注册
搜索
查看: 664|回复: 3

信息计算2019级1班刘阔

[复制链接]

10

主题

22

帖子

96

积分

注册会员

Rank: 2

积分
96
发表于 2022-6-3 17:28:02 | 显示全部楼层 |阅读模式
RSA大数算法实现
RSA的定理模型:
设p,q是两个素数,n=p*q;
假设整数e满足1<e<φ(n),(e, φ(n))=1;
1、
那么就存在整数s、d,e*d+s* φ(n)=1;
2、
假设存在一个整数a,(a,n)=1;那么a和q,p也应该互素,即(a,p)=1;(a,q)=1;
所以a^φ(p)=1 mod p;
等式两边同时做φ(q)次幂运算,得到(a^φ(p))^φ(q)=1^φ(q) mod p,
整理得a^(φ(p)*φ(q))=1 mod p;
φ(n)=φ(p)*φ(q),所以a^φ(n)=1 mod p;
等式两边同时做s次幂运算,得到a^(s*φ(n))=(1^s) mod
p=1 mod p;
左右在个乘以a,得到a^(1+(s*φ(n)))=a mod p;
所以:
a^(e*d)=a mod p;①同理,q,p在本质上是相同的,所以
a^(e*d)=a mod q;②
a^(e*d)=a mod (p*q),因为n=p*q;所以a^(e*d)=a mod n;

回复

使用道具 举报

10

主题

22

帖子

96

积分

注册会员

Rank: 2

积分
96
 楼主| 发表于 2022-6-3 17:29:09 | 显示全部楼层
  1. import java.io.ByteArrayOutputStream;
  2. import java.security.Key;
  3. import java.security.KeyFactory;
  4. import java.security.KeyPair;
  5. import java.security.KeyPairGenerator;
  6. import java.security.PrivateKey;
  7. import java.security.PublicKey;
  8. import java.security.Signature;
  9. import java.security.interfaces.RSAPrivateKey;
  10. import java.security.interfaces.RSAPublicKey;
  11. import java.security.spec.PKCS8EncodedKeySpec;
  12. import java.security.spec.X509EncodedKeySpec;
  13. import java.util.*;
  14. import java.util.HashMap;
  15. import java.util.Map;
  16. import javax.crypto.Cipher;

  17. public class RSAUtils {
  18.     /**
  19.      * 加密算法RSA
  20.      */
  21.     public static final String KEY_ALGORITHM = "RSA";

  22.     /**
  23.      * 签名算法
  24.      */
  25.     public static final String SIGNATURE_ALGORITHM = "MD5withRSA";

  26.     /**
  27.      * 获取公钥的key
  28.      */
  29.     private static final String PUBLIC_KEY = "RSAPublicKey";

  30.     /**
  31.      * 获取私钥的key
  32.      */
  33.     private static final String PRIVATE_KEY = "RSAPrivateKey";

  34.     /**
  35.      * RSA最大加密明文大小
  36.      */
  37.     private static final int MAX_ENCRYPT_BLOCK = 117;

  38.     /**
  39.      * RSA最大解密密文大小
  40.      */
  41.     private static final int MAX_DECRYPT_BLOCK = 128;

  42.     /**
  43.      * <p>
  44.      * 生成密钥对(公钥和私钥)
  45.      * </p>
  46.      */
  47.     public static Map<String, Object> genKeyPair() throws Exception {
  48.         KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(KEY_ALGORITHM);
  49.         keyPairGen.initialize(1024);
  50.         KeyPair keyPair = keyPairGen.generateKeyPair();
  51.         RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
  52.         RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
  53.         Map<String, Object> keyMap = new HashMap<String, Object>(2);
  54.         keyMap.put(PUBLIC_KEY, publicKey);
  55.         keyMap.put(PRIVATE_KEY, privateKey);
  56.         return keyMap;
  57.     }

  58.     /**
  59.      * <p>
  60.      * 用私钥对信息生成数字签名
  61.      * </p>

  62.      */
  63.     public static String sign(byte[] data, String privateKey) throws Exception {
  64.         byte[] keyBytes = Base64.decode(privateKey);
  65.         PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
  66.         KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
  67.         PrivateKey privateK = keyFactory.generatePrivate(pkcs8KeySpec);
  68.         Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);
  69.         signature.initSign(privateK);
  70.         signature.update(data);
  71.         return Base64.encode(signature.sign());
  72.     }

  73.     /**
  74.      * <p>
  75.      * 校验数字签名
  76.      * </p>
  77.      */
  78.     public static boolean verify(byte[] data, String publicKey, String sign)
  79.             throws Exception {
  80.         byte[] keyBytes = Base64.decode(publicKey);
  81.         X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
  82.         KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
  83.         PublicKey publicK = keyFactory.generatePublic(keySpec);
  84.         Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);
  85.         signature.initVerify(publicK);
  86.         signature.update(data);
  87.         return signature.verify(Base64.decode(sign));
  88.     }

  89.     /**
  90.      * <P>
  91.      * 私钥解密
  92.      * </p>
  93.      */
  94.     public static byte[] decryptByPrivateKey(byte[] encryptedData, String privateKey)
  95.             throws Exception {
  96.         byte[] keyBytes = Base64.decode(privateKey);
  97.         PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
  98.         KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
  99.         Key privateK = keyFactory.generatePrivate(pkcs8KeySpec);
  100.         Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
  101.         cipher.init(Cipher.DECRYPT_MODE, privateK);
  102.         int inputLen = encryptedData.length;
  103.         ByteArrayOutputStream out = new ByteArrayOutputStream();
  104.         int offSet = 0;
  105.         byte[] cache;
  106.         int i = 0;
  107.         while (inputLen - offSet > 0) {
  108.             if (inputLen - offSet > MAX_DECRYPT_BLOCK) {
  109.                 cache = cipher.doFinal(encryptedData, offSet, MAX_DECRYPT_BLOCK);
  110.             } else {
  111.                 cache = cipher.doFinal(encryptedData, offSet, inputLen - offSet);
  112.             }
  113.             out.write(cache, 0, cache.length);
  114.             i++;
  115.             offSet = i * MAX_DECRYPT_BLOCK;
  116.         }
  117.         byte[] decryptedData = out.toByteArray();
  118.         out.close();
  119.         return decryptedData;
  120.     }

  121.     /**
  122.      * <p>
  123.      * 公钥解密
  124.      * </p>

  125.      */
  126.     public static byte[] decryptByPublicKey(byte[] encryptedData, String publicKey)
  127.             throws Exception {
  128.         byte[] keyBytes = Base64.decode(publicKey);
  129.         X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);
  130.         KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
  131.         Key publicK = keyFactory.generatePublic(x509KeySpec);
  132.         Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
  133.         cipher.init(Cipher.DECRYPT_MODE, publicK);
  134.         int inputLen = encryptedData.length;
  135.         ByteArrayOutputStream out = new ByteArrayOutputStream();
  136.         int offSet = 0;
  137.         byte[] cache;
  138.         int i = 0;

  139.         while (inputLen - offSet > 0) {
  140.             if (inputLen - offSet > MAX_DECRYPT_BLOCK) {
  141.                 cache = cipher.doFinal(encryptedData, offSet, MAX_DECRYPT_BLOCK);
  142.             } else {
  143.                 cache = cipher.doFinal(encryptedData, offSet, inputLen - offSet);
  144.             }
  145.             out.write(cache, 0, cache.length);
  146.             i++;
  147.             offSet = i * MAX_DECRYPT_BLOCK;
  148.         }
  149.         byte[] decryptedData = out.toByteArray();
  150.         out.close();
  151.         return decryptedData;
  152.     }

  153.     /**
  154.      * <p>
  155.      * 公钥加密
  156.      * </p>

  157.      */
  158.     public static byte[] encryptByPublicKey(byte[] data, String publicKey)
  159.             throws Exception {
  160.         byte[] keyBytes = Base64.decode(publicKey);
  161.         X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);
  162.         KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
  163.         Key publicK = keyFactory.generatePublic(x509KeySpec);

  164.         Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
  165.         cipher.init(Cipher.ENCRYPT_MODE, publicK);
  166.         int inputLen = data.length;
  167.         ByteArrayOutputStream out = new ByteArrayOutputStream();
  168.         int offSet = 0;
  169.         byte[] cache;
  170.         int i = 0;

  171.         while (inputLen - offSet > 0) {
  172.             if (inputLen - offSet > MAX_ENCRYPT_BLOCK) {
  173.                 cache = cipher.doFinal(data, offSet, MAX_ENCRYPT_BLOCK);
  174.             } else {
  175.                 cache = cipher.doFinal(data, offSet, inputLen - offSet);
  176.             }
  177.             out.write(cache, 0, cache.length);
  178.             i++;
  179.             offSet = i * MAX_ENCRYPT_BLOCK;
  180.         }
  181.         byte[] encryptedData = out.toByteArray();
  182.         out.close();
  183.         return encryptedData;
  184.     }

  185.     /**
  186.      * <p>
  187.      * 私钥加密
  188.      * </p>

  189.      */
  190.     public static byte[] encryptByPrivateKey(byte[] data, String privateKey)
  191.             throws Exception {
  192.         byte[] keyBytes = Base64.decode(privateKey);
  193.         PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
  194.         KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
  195.         Key privateK = keyFactory.generatePrivate(pkcs8KeySpec);
  196.         Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
  197.         cipher.init(Cipher.ENCRYPT_MODE, privateK);
  198.         int inputLen = data.length;
  199.         ByteArrayOutputStream out = new ByteArrayOutputStream();
  200.         int offSet = 0;
  201.         byte[] cache;
  202.         int i = 0;

  203.         while (inputLen - offSet > 0) {
  204.             if (inputLen - offSet > MAX_ENCRYPT_BLOCK) {
  205.                 cache = cipher.doFinal(data, offSet, MAX_ENCRYPT_BLOCK);
  206.             } else {
  207.                 cache = cipher.doFinal(data, offSet, inputLen - offSet);
  208.             }
  209.             out.write(cache, 0, cache.length);
  210.             i++;
  211.             offSet = i * MAX_ENCRYPT_BLOCK;
  212.         }
  213.         byte[] encryptedData = out.toByteArray();
  214.         out.close();
  215.         return encryptedData;
  216.     }

  217.     /**
  218.      * <p>
  219.      * 获取私钥
  220.      * </p>

  221.      */
  222.     public static String getPrivateKey(Map<String, Object> keyMap)
  223.             throws Exception {
  224.         Key key = (Key) keyMap.get(PRIVATE_KEY);
  225.         return Base64.encode(key.getEncoded());
  226.     }

  227.     /**
  228.      * <p>
  229.      * 获取公钥
  230.      * </p>
  231.      */
  232.     public static String getPublicKey(Map<String, Object> keyMap)
  233.             throws Exception {
  234.         Key key = (Key) keyMap.get(PUBLIC_KEY);
  235.         return Base64.encode(key.getEncoded());
  236.     }
  237. }
复制代码
回复

使用道具 举报

10

主题

22

帖子

96

积分

注册会员

Rank: 2

积分
96
 楼主| 发表于 2022-6-3 17:35:05 | 显示全部楼层
椭圆加密算法ECC算法原理:设私钥、公钥分别为d、Q,即Q = dG,其中G为基点,椭圆曲线上的已知G和dG,求d是非常困难的,也就是说已知公钥和基点,想要算出私钥是非常困难的。
公钥加密选择随机数r,将消息M生成密文C,该密文是一个点对,C = {rG, M+rQ},其中Q为公钥。
私钥解密:M + rQ - d(rG) = M + r(dG) - d(rG) = M,其中d、Q分别为私钥、公钥
回复

使用道具 举报

10

主题

22

帖子

96

积分

注册会员

Rank: 2

积分
96
 楼主| 发表于 2022-6-3 17:36:37 | 显示全部楼层

  1. import org.bouncycastle.jce.provider.BouncyCastleProvider;
  2. import javax.crypto.Cipher;
  3. import java.security.*;
  4. import java.security.interfaces.ECPrivateKey;
  5. import java.security.spec.PKCS8EncodedKeySpec;
  6. import java.security.spec.X509EncodedKeySpec;
  7. import java.util.Base64;

  8. public class ECC {
  9.         // 生成公钥和私钥
  10.         public static KeyPair initKey(int keySize, String KEY_ALGORITHM) throws Exception {
  11.                 KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(KEY_ALGORITHM);
  12.                 keyPairGen.initialize(keySize);
  13.                 KeyPair keyPair = keyPairGen.generateKeyPair();
  14.                 return keyPair;
  15.         }

  16.         // 公钥加密
  17.         public static String encryptByPublicKey(String data, String publicKey) throws Exception {
  18.                 byte[] keyBytes = Base64.getDecoder().decode(publicKey);
  19.                 Security.addProvider(new BouncyCastleProvider());
  20.                 X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);
  21.                 KeyFactory keyFactory = KeyFactory.getInstance("EC");
  22.                 Cipher cipher = Cipher.getInstance("ECIES", "BC");
  23.                 cipher.init(Cipher.ENCRYPT_MODE, keyFactory.generatePublic(x509KeySpec));
  24.                 return Base64.getEncoder().encodeToString(cipher.doFinal(data.getBytes()));

  25.         }

  26.         // 私钥解密
  27.         public static String decryptByPrivateKey(String encryptedData, String privateKey) throws Exception {
  28.                 byte[] keyBytes = Base64.getDecoder().decode(privateKey);
  29.                 PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
  30.                 KeyFactory keyFactory = KeyFactory.getInstance("EC");
  31.                 Security.addProvider(new BouncyCastleProvider());
  32.                 Cipher cipher = Cipher.getInstance("ECIES", "BC");
  33.                 cipher.init(Cipher.DECRYPT_MODE, keyFactory.generatePrivate(pkcs8KeySpec));
  34.                 return Base64.getEncoder().encodeToString(cipher.doFinal(Base64.getDecoder().decode(encryptedData)));
  35.         }

  36.         // 用私钥对信息生成数字签名
  37.         public static String sign(String content, String priKey, String signatureAl) throws Exception {
  38.                 PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(Base64.getDecoder().decode(priKey));
  39.                 KeyFactory keyFactory = KeyFactory.getInstance("EC");
  40.                 ECPrivateKey privateK = (ECPrivateKey) keyFactory.generatePrivate(pkcs8KeySpec);
  41.                 Signature sign = Signature.getInstance(signatureAl);
  42.                 sign.initSign(privateK);
  43.                 sign.update(Base64.getDecoder().decode(content));
  44.                 return Base64.getEncoder().encodeToString(sign.sign());
  45.         }

  46.         public static void main(String[] args) {
  47.                 try {
  48.                         // 初始化获取公钥和私钥
  49.                         KeyPair keypair = initKey(256, "EC");
  50.                         PublicKey publicKey = keypair.getPublic();
  51.                         PrivateKey privateKey = keypair.getPrivate();
  52.                         System.out.println("私钥:" + privateKey);
  53.                         System.out.println("公钥:" + publicKey);

  54.                         // 生成固定公钥私钥
  55.                         String publicKeyBase64 = "xinxijisuan";
  56.                         String privateKeyBase64 = "yuanminting";
  57.                         System.out.println("公钥:" + publicKeyBase64);
  58.                         System.out.println("私钥:" + privateKeyBase64);
  59.                         String c = "信息计算";
  60.                         System.out.println("加密之前:" + c);
  61.                         String content = encryptByPublicKey(c, publicKeyBase64);
  62.                         String contentDe = decryptByPrivateKey(content, privateKeyBase64);
  63.                         String d = new String(Base64.getDecoder().decode(contentDe));
  64.                         System.out.println("解密之后:" + d);
  65.                 } catch (Exception e) {
  66.                         e.printStackTrace();
  67.                 }
  68.         }
  69. }
复制代码


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

教学服务系统

GMT+8, 2025-4-30 11:57 , Processed in 0.018779 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表