首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >为什么Java RSA解密不起作用?

为什么Java RSA解密不起作用?
EN

Stack Overflow用户
提问于 2018-07-22 23:39:24
回答 1查看 675关注 0票数 0

因此,这是我在http://www.loyalty.org/~schoen/rsa/的一项任务的一部分(尽管我已经不得不提交未解决的任务),这是基于这个挑战的。

总之,质询提供了包含密码和相关公钥文件列表。利用RSA公钥经常重复素数的事实,我们希望从100个私钥中找到尽可能多的私钥,并解密相关的消息。虽然我认为形成私钥很简单(并且相信我这样做是正确的),但我得到了错误"javax.crypto.BadPaddingException: Message is than modulus“。谁能告诉我我哪里做错了?我包含了我的代码以及两个bin和cipher文件作为示例。

我的代码:

代码语言:javascript
复制
package decryptrsa;

import com.sun.org.apache.xml.internal.security.utils.Base64;
import java.io.File;

import java.util.Scanner;
import java.security.spec.X509EncodedKeySpec;
import java.security.spec.RSAPrivateKeySpec;
import java.security.KeyFactory;
import java.security.interfaces.*;
import java.math.BigInteger;
import java.util.ArrayList;
import javax.crypto.Cipher;
import java.io.FileInputStream;
import java.io.FileOutputStream;


/**
 *
 * @author qscot
 */
public class DecryptRSA {

    static ArrayList<BigInteger> publicKeys = new ArrayList<BigInteger>();

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws Exception {
        // TODO code application logic here
        ReadPublicKeys();
        crackPrivateKeys();

    }

    static void ReadPublicKeys() throws Exception {
        String publicKey;

        for (int i = 1; i <= 100; i++) {
            publicKey = "";
            Scanner scanner = new Scanner(new File("C:\\Users\\qscot\\Downloads\\challenge\\" + i + ".pem"));
            scanner.nextLine();
            String lineString = scanner.nextLine();
            do {
                publicKey += lineString;
                lineString = scanner.nextLine();
            } while (lineString.contains("END PUBLIC KEY") == false);
            scanner.close();

            byte[] decoded = Base64.decode(publicKey.getBytes());
            X509EncodedKeySpec spec = new X509EncodedKeySpec(decoded);
            KeyFactory kf = KeyFactory.getInstance("RSA");

            RSAPublicKey pk = (RSAPublicKey) kf.generatePublic(spec);
            BigInteger modulus = pk.getModulus();

            publicKeys.add(modulus);
        }

    }

    static void crackPrivateKeys() throws Exception {

        BigInteger gcd;
        BigInteger q1;
        BigInteger q2;
        for (int i = 0; i < 100; i++) {
            for (int z = 0; z < 100; z++) {
                if (i != z) {
                    gcd = (gcd(publicKeys.get(i), publicKeys.get(z)));

                    if (!gcd.equals(BigInteger.ONE)) {
                        q1 = publicKeys.get(i).divide(gcd);
                        q2 = publicKeys.get(z).divide(gcd);
                        RSAPrivateKey key1 = getKey(gcd, q1);
                        RSAPrivateKey key2 = getKey(gcd, q2);


                        writeDecryptedFile(i, key1);
                        writeDecryptedFile(z, key2);

                    }
                }
            }
        }

    }

    static void writeDecryptedFile(int fileNo, RSAPrivateKey privKey) throws Exception {
        Cipher cipher = Cipher.getInstance("RSA/ECB/NoPadding");

        File file = new File("C:\\Users\\qscot\\Downloads\\challenge\\" + fileNo + ".bin");

        FileInputStream fis = new FileInputStream(file);
        byte[] fbytes = new byte[(int) file.length()];
        fis.read(fbytes);
        fis.close();
        cipher.init(Cipher.DECRYPT_MODE, privKey);
        byte[] oBytes = cipher.doFinal(fbytes);
        FileOutputStream fos = new FileOutputStream("C:\\Users\\qscot\\Downloads\\challenge\\" + fileNo + "-OUTPUT.txt");
        fos.write(oBytes);
        fos.flush();
        fos.close();

    }

    static BigInteger gcd(BigInteger num1, BigInteger num2) {

        while (!num1.equals(BigInteger.ONE) && !num1.equals(BigInteger.ZERO) && !num2.equals(BigInteger.ONE) && !num2.equals(BigInteger.ZERO)) {
            if (num1.compareTo(num2) == 1) {
                num1 = num1.mod(num2);

            } else {
                num2 = num2.mod(num1);

            }
        }

        if (num1.equals(BigInteger.ONE) || num2.equals(BigInteger.ONE)) {
            return BigInteger.valueOf(1);

        } else {
            if (num1.equals(BigInteger.ZERO)) {
                return num2;
            } else {
                return num1;
            }

        }
    }

    static RSAPrivateKey getKey(BigInteger p, BigInteger q) throws Exception {
        KeyFactory kf = KeyFactory.getInstance("RSA");
        BigInteger t, d, e;
        e = BigInteger.valueOf(65537);
        t = (p.subtract(BigInteger.ONE)).multiply((q.subtract(BigInteger.ONE)));
        d = e.modInverse(t);
        RSAPrivateKeySpec keyspec = new RSAPrivateKeySpec(d, e);
        return (RSAPrivateKey) kf.generatePrivate(keyspec);

    }

}

6.pem文件:

代码语言:javascript
复制
-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDn5BqEUDrlhvwXQ68MqZ001B74
fGD1w2Le++wILzEX7Ba8LeJVeuwpOxxdxDQ7280yc0SKPiChWpb3bE1/G/hV5e++
95qfhbe+SP7MRL39TxEotADaqyHY6SfloDk5A9NiIzgebWmtFriamBfhrxzx8G3K
6NWAAjDAIMx+xjLn6QIDAQAB
-----END PUBLIC KEY-----

6.bin文件:

代码语言:javascript
复制
xƒâTD§¼çÄ   ؈ßPã…Ôä3x4b2Ð#•—æ¨
U«õ`Êzÿúw"Ü°™è0ÄÕ~³•—˜§FºqŠ„hÏŒÞõ&د³Ô<*pàbGÃGìMÿö¶3Ùù­¸²Z•a¯®éDNïæÝjn¢¯tå!WÐ

6-OUTPUT.txt:

8.pem文件:

代码语言:javascript
复制
-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC1fGe+mMJYZ+BDm76Ag25bXcBB
pABkGZpnQpnSTocEuCQyp5/lNNVFdF0QliNRULnxoK+pD8VEBqxR+zkYsYf9iGzU
PzOELWvbFgIJdUPixlaD3/1Y6+eSDffCOsCoJ1A/8DELMbjQdbFoxfqj9AVRU3cd
R0AauL4O9hPz0N9OVQIDAQAB
-----END PUBLIC KEY-----

8.bin文件:

代码语言:javascript
复制
¤”»BÃ."
îÂT.<(bø×]¥”£Ó¯!›==±Ñ·;ª%7¿ðU@xÀÉ5ç£
‡*h\w¸¸@¦aܳj Ù~t´õêæSü®Î ŒQU¼L-â-äK}\-žù‹ý«>DÕ£ñ”Õe6Œ"
G®lI
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-23 01:51:40

RSAPrivateKeySpec keyspec = new RSAPrivateKeySpec(d, e);包含一个错误。RSAPrivateKeySpec的参数应该依次是模数和解密指数。根据您提供的链接中的文档,使用的填充是PKCS1PADDING。因此,您应该使用Cipher.getInstance("RSA/ECB/PKCS1PADDING");获取密码实例

在您的writeDecryptedFile方法中也有一个off-by-1错误。这些文件名为1.bin ... 100.bin,但是您的键的索引范围是从0到99。因此,您需要访问(fileNo + 1) + ".bin"而不是fileNo + ".bin"

没有理由假设加密指数始终为65537,因为此信息在RSAPublicKey中可用。此外,请不要使用您正在使用的Base64编解码器,而应使用来自java.util.Base64的编解码器。

您从加密*.bin文件中读取字节的方法是不可靠的,因为它假定读取操作始终返回请求的字节数。它可能会在当前的实现中以这种方式工作,但javadoc并不能保证这一点。您应该研究用于读取小文件的非常简单的Files.readAllBytes()方法和related methods。它们极大地简化了您的代码。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51466674

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档