HIT·信息安全数学基础·实验二

实验背景

RSA 算法是一种公开密钥算法,由 Ron Rivest、Adi Shamir 和 Leonard Adleman
于 1977 年共同提出。该算法旨在解决利用公开信道传输、分发私钥的难题,还
可以用于保护数据信息的完整性,能够抵抗目前已知的绝大多数密码攻击,已被
国际标准化组织推荐为公钥数据加密标准。
本次实验要求同学们利用 Java 或者其它编程语言对经典的 RSA 算法和
RSA-OAEP 算法进行实践。
本次实验的目的包括:

  1. 理解和掌握 RSA 算法和 RSA-OAEP 算法的原理和流程:通过实验,学习
    RSA 算法和 RSA-OAEP 算法的基本原理和流程,了解公钥加密、私钥解密的过
    程,并理解其安全性和适用性。
  2. 了解加密算法的性能和效率:实验中可以对比 RSA 算法和 RSA-OAEP 算
    法在不同输入大小和密钥长度下的加密和解密性能,对比它们的效率和时间复杂
    度,评估其在实际应用中的可行性。
  3. 实践加密和解密过程:通过实验,实际进行 RSA 算法和 RSA-OAEP 算法
    的加密和解密操作,加深对于加密和解密过程的理解,并掌握编写相关代码的技
    巧和经验。
  4. 评估算法的安全性:通过实验,了解 RSA 算法和 RSA-OAEP 算法的安全
    性,对比它们的安全性级别,例如密钥长度、抵御常见攻击方式等,评估其在实
    际应用中的安全性。
  5. 探索实际应用场景:通过实验,尝试将 RSA 算法和 RSA-OAEP 算法应用
    于实际场景,例如加密通信、数字签名等,评估它们的适用性和效果,并思考如
    何结合其他技术改进和扩展算法的应用。

实验内容

RSA算法

  1. 实现密钥生成
  2. 实现加密
  3. 实现解密

OAEP算法

  1. 填充算法
  2. 加密算法
  3. 解密算法

注意事项

本次实验的算法是要求用到实验一种写到的快速幂、欧几里得、扩展的欧几里得算法的,因此需要考虑自己实验所写的代码的接口

实验代码

RSA

package myRSA;

import java.math.BigInteger;
import java.util.Random;
import java.util.Scanner;
import myFast.FastModularExpo;
public class RSA {

    // 生成大素数
    public static BigInteger getPrime(int bitCount) {
        return BigInteger.probablePrime(bitCount, new Random());//随机生成一个大素数,bitCount对位数进行定义
    }

    // 生成公私钥
    public static BigInteger[] generateKey(int bits) {
        BigInteger p = getPrime(bits / 2);
        BigInteger q = getPrime(bits / 2);
        BigInteger n = p.multiply(q);
        BigInteger phi = p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE));

        BigInteger e = BigInteger.valueOf(65537); // 公钥值取65537
        while (phi.gcd(e).intValue() > 1) {
            e = e.add(new BigInteger("2"));
        }

        BigInteger d = e.modInverse(phi);

        return new BigInteger[]{e, d, n};
    }

    // 加密
    public static BigInteger encrypt(BigInteger plaintext, BigInteger e, BigInteger n) {
        return FastModularExpo.fastModPow(plaintext, e, n);
    }

    // 解密
    public static BigInteger decrypt(BigInteger ciphertext, BigInteger d, BigInteger n) {
        return FastModularExpo.fastModPow(ciphertext, d, n);
    }

    // 主函数
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("请输入安全参数 bits: ");
        int bits = scanner.nextInt();

        System.out.print("请输入要加密的明文: ");
        BigInteger plaintext = scanner.nextBigInteger();

        BigInteger[] keys = generateKey(bits);
        BigInteger e = keys[0];
        BigInteger d = keys[1];
        BigInteger n = keys[2];

        System.out.println("公钥 (e, n): (" + e + ", " + n + ")");
        System.out.println("私钥 (d, n): (" + d + ", " + n + ")");

        BigInteger ciphertext = encrypt(plaintext, e, n);
        System.out.println("加密后的密文: " + ciphertext);

        BigInteger decryptedText = decrypt(ciphertext, d, n);
        System.out.println("解密后的明文: " + decryptedText);

        scanner.close();
    }
}

RSA-OAEP

import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
import myRSA.RSA;//自己写的RSA算法

public class RSA_OAEP {

    // 生成指定长度的大素数
    public BigInteger getPrime(int bitCount) {
        return BigInteger.probablePrime(bitCount, new Random());//同样的,生成一个大素数
    }

    // 生成加密解密所需要的公、私密钥对
    public BigInteger[] generateKey(int bits) {
       return RSA.generateKey(bits);//调用刚才写的RSA函数的生成密钥方法
    }

    // 生成掩码
    public byte[] MGF(byte[] X, int maskLen) throws NoSuchAlgorithmException {//错误的时候要抛出异常才行
        //X是seed,masklen是掩码长度
        MessageDigest md = MessageDigest.getInstance("SHA-256");//用SHA-256算法进行生成一个消息摘要
        byte[] T = new byte[maskLen];//存储最终的掩码
        int counter = 0;
        byte[] C = new byte[4];
        int hLen = md.getDigestLength();//获取digest的长度

        while (counter < (maskLen / hLen)) {
            ByteBuffer.wrap(C).putInt(counter);//将计数器的值转换为字节然后存储在C中
            md.update(X);//更新消息摘要,加入输入种子,然后加入C
            md.update(C);
            System.arraycopy(md.digest(), 0, T, counter * hLen, hLen);//把消息摘要复制到T
            counter++;
        }

        if (maskLen % hLen != 0) {//处理最后部分
            ByteBuffer.wrap(C).putInt(counter);
            md.update(X);
            md.update(C);
            System.arraycopy(md.digest(), 0, T, counter * hLen, maskLen % hLen);
        }

        return T;
    }

    // 消息填充算法
    public byte[] encode(byte[] plainOAEP/*原始参数*/, String L, int k/*RSA秘钥长度*/) throws NoSuchAlgorithmException {
        MessageDigest md = MessageDigest.getInstance("SHA-256");//初始化消息摘要
        byte[] lHash = md.digest(L.getBytes());//计算md的hash值
        int hLen = lHash.length;
        int mLen = plainOAEP.length;

        if (k < (mLen + 2 * hLen + 2)) {
            throw new IllegalArgumentException("Encoding error, message too long.");
        }

        byte[] PS = new byte[k - mLen - 2 * hLen - 2];
        byte[] DB = ByteBuffer.allocate(k - hLen - 1)
                .put(lHash)
                .put(PS)
                .put((byte) 0x01)
                .put(plainOAEP)
                .array();
        //创建数据块 `DB`,包含 `lHash`、`PS`、一个字节的 `0x01` 和原始消息。
        byte[] seed = new byte[hLen];//创建种子
        new SecureRandom().nextBytes(seed);//随机填充种子
        byte[] dbMask = MGF(seed, k - hLen - 1);//用MGF生成数据块掩码
        byte[] maskedDB = new byte[dbMask.length];

        for (int i = 0; i < dbMask.length; i++) {
            maskedDB[i] = (byte) (DB[i] ^ dbMask[i]);//异或操作将dbMask应用于DB
        }
        //掩码种子
        byte[] seedMask = MGF(maskedDB, hLen);
        byte[] maskedSeed = new byte[seed.length];

        for (int i = 0; i < seed.length; i++) {
            maskedSeed[i] = (byte) (seed[i] ^ seedMask[i]);
        }

        return ByteBuffer.allocate(1 + maskedSeed.length + maskedDB.length)
                .put((byte) 0x00)
                .put(maskedSeed)
                .put(maskedDB)
                .array();
    }

    // OAEP 加密
//    public BigInteger encryptOAEP(byte[] plainOAEP, String L, int k, BigInteger e, BigInteger n) throws NoSuchAlgorithmException {
//        byte[] EM = encode(plainOAEP, L, k);
//        BigInteger m = new BigInteger(1, EM); // 确保 BigInteger 是正数
//        BigInteger c = m.modPow(e, n);
//
//        // 将 BigInteger 转换为字节数组,并确保长度与 k 相匹配
//        byte[] cipherBytes = c.toByteArray();
//        if (cipherBytes.length < k) {
//            byte[] temp = new byte[k];
//            System.arraycopy(cipherBytes, 0, temp, k - cipherBytes.length, cipherBytes.length);
//            cipherBytes = temp;
//        }
//
//        return new BigInteger(cipherBytes);
//    }
    public BigInteger encryptOAEP(byte[] plainOAEP, String L, int k, BigInteger e, BigInteger n) throws NoSuchAlgorithmException {
        byte[] EM = encode(plainOAEP, L, k);
        BigInteger m = new BigInteger(1, EM); // 确保 BigInteger 是正数

        // 使用自己写的 RSA 加密方法
        BigInteger c = RSA.encrypt(m, e, n);

        // 将 BigInteger 转换为字节数组,并确保长度与 k 相匹配
        byte[] cipherBytes = c.toByteArray();
        if (cipherBytes.length < k) {
            byte[] temp = new byte[k];
            System.arraycopy(cipherBytes, 0, temp, k - cipherBytes.length, cipherBytes.length);
            cipherBytes = temp;
        }

        return new BigInteger(cipherBytes);
    }

    // 解码(去掉填充)
    public byte[] decode(byte[] EM/*加密后的消息*/, String L, int k/*秘钥的长度*/) throws NoSuchAlgorithmException {
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        byte[] lHash = md.digest(L.getBytes());
        int hLen = lHash.length;

        if (EM.length != k || k < 2 * hLen + 2) {
            throw new IllegalArgumentException("Decoding error, incorrect message length.");
        }

        byte Y = EM[0];//提取第一个字节0x00
        byte[] maskedSeed = Arrays.copyOfRange(EM, 1, hLen + 1);//根据算法 提取掩码后的种子
        byte[] maskedDB = Arrays.copyOfRange(EM, hLen + 1, EM.length);//提取掩码后的数据块

        byte[] seedMask = MGF(maskedDB, hLen);//利用MGF和maskedDB生成种子掩码
        byte[] seed = new byte[hLen];
        for (int i = 0; i < hLen; i++) {//还原db
            seed[i] = (byte) (maskedSeed[i] ^ seedMask[i]);
        }

        byte[] dbMask = MGF(seed, k - hLen - 1);
        byte[] DB = new byte[dbMask.length];
        for (int i = 0; i < dbMask.length; i++) {
            DB[i] = (byte) (maskedDB[i] ^ dbMask[i]);
        }
        //vertify the hash
        if (!Arrays.equals(Arrays.copyOfRange(DB, 0, hLen), lHash)) {
            throw new IllegalArgumentException("Decoding error, lHash mismatch.");
        }

        int index = hLen;
        while (index < DB.length && DB[index] == 0) {
            index++;
        }
        //找到DB第一个非零字节后的部分,就是原始消息
        if (index == DB.length || DB[index] != 1) {
            throw new IllegalArgumentException("Decoding error, incorrect padding.");
        }

        return Arrays.copyOfRange(DB, index + 1, DB.length);
    }

    //解密
    public byte[] decrypt(BigInteger cipherOAEP, String L, int k, BigInteger d, BigInteger n) throws NoSuchAlgorithmException {
        BigInteger c = cipherOAEP;
//        BigInteger m = c.modPow(d, n);
        BigInteger m = RSA.decrypt(c,d,n);
        byte[] decryptedBytes = m.toByteArray();
        if (decryptedBytes.length < k) {
            byte[] temp = new byte[k];
            System.arraycopy(decryptedBytes, 0, temp, k - decryptedBytes.length, decryptedBytes.length);
            decryptedBytes = temp;
        }

        return decode(decryptedBytes, L, k);
    }

    public static void main(String[] args) throws NoSuchAlgorithmException {
        RSA_OAEP rsa = new RSA_OAEP();
        Scanner scanner = new Scanner(System.in);

        // 输入比特数
        System.out.print("Enter the bit count for RSA key generation: ");
        int bitCount = scanner.nextInt();

        // 生成公钥和私钥
        BigInteger[] keys = rsa.generateKey(bitCount);
        BigInteger e = keys[0]; // 公钥 e
        BigInteger d = keys[1]; // 私钥 d
        BigInteger n = keys[2]; // 公钥和私钥共用 n

        System.out.print("Enter the message to encrypt: ");
        scanner.nextLine();
        String message = scanner.nextLine();

        // 加密
        byte[] encrypted = rsa.encryptOAEP(message.getBytes(), "", n.bitLength() / 8, e, n).toByteArray();
        System.out.println("Encrypted message: " + Arrays.toString(encrypted));

        // 解密
        byte[] decrypted = rsa.decrypt(new BigInteger(encrypted), "", n.bitLength() / 8, d, n);
        System.out.println("Decrypted message: " + new String(decrypted));
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值