简体   繁体   English

Java中的RSA解密,未使用RSA库

[英]RSA decryption in Java, RSA libraries not used

I'm writing a program to encrypt and decrypt text using RSA. 我正在编写一个使用RSA加密和解密文本的程序。 The values of P, Q, and E are provided by the user and checked to see if they are prime. P,Q和E的值由用户提供,并检查它们是否为质数。 The program finds N and D. The encryption and decryption are malfunctioning. 程序找到N和D。加密和解密出现故障。 I've looked at a few of the questions on here and most of them use libraries which for this assignment I'm not meant to use. 我看过这里的一些问题,其中大多数使用的库不是我打算使用的库。 Code snippet of encryption: 加密代码段:

JMenuItem mntmEncrypt = new JMenuItem("Encrypt");
mntmEncrypt.addActionListener(new ActionListener() {
    @SuppressWarnings("null")
    public void actionPerformed(ActionEvent ev) {

        textArea_1.setText("");
        String blah = textArea.getText();
        int something;

        for(int i=0; i<blah.length();i++){
            something =(int)(blah.charAt(i));

            enc = BigInteger.valueOf((long) (Math.pow(something, e)%n));

            textArea_1.append(blah.valueOf(enc) + " ");
        }

And malfunctioning decryption: 和解密故障:

JMenuItem mntmDecrypt = new JMenuItem("Decrypt");
mntmDecrypt.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent em) {
        textArea_1.setText("");
        String blah2 = textArea.getText();
        String[] somethingElse = blah2.split(" ");
        char character;
        for(int i=0;i<somethingElse.length;i++){
            int cipher = Integer.parseInt(somethingElse[i]);                    

            dec = BigInteger.valueOf((long)Math.pow(cipher, d)%n);

            System.out.println("Cipher: "+cipher); //print check

            System.out.println("d: "+d); //print check

            System.out.println("n: "+n); //print check

            System.out.println("dec: "+dec); //print check

            BigInteger x = BigInteger.valueOf(dec);
            int x2 = x.intValue();
            char c = (char) x2;

            System.out.println(cipher + " " + dec);
            String textBack = new String(dec.toByteArray());

            textArea_1.append(String.valueOf(dec));
        }
    }
});

I checked the value of dec using this calculator and it is completely wrong but I can't see why. 我使用此计算器检查了dec的值,这是完全错误的,但我不知道为什么。 Any help would be appreciated. 任何帮助,将不胜感激。 I CANNOT make anything BigInteger except enc and dec. 除了enc和dec外,我什么也做不了BigInteger。

The answer is simple: You are using Math.pow 答案很简单:您正在使用Math.pow

Math.pow works on double values - hence it uses floating point numbers. Math.pow使用double精度值-因此它使用浮点数。 Floating point numbers are never exact, therefore your result is already screwed up at that point. 浮点数从来都不是精确的,因此您的结果在那一点上已经搞砸了。

If you really want to calculate RSA by hand you can use BigInteger.modPow(..) . 如果您真的想手工计算RSA,则可以使用BigInteger.modPow(..)

However your project is not a learning project I strongly recommend to use the Java Cryptography Extension. 但是,您的项目不是学习项目,我强烈建议您使用Java密码学扩展。 Therefore use the BigInteger values and create a RSAPrivateKeySpec/RSAPublicKeySpec and use them with Cipher class. 因此,请使用BigInteger值并创建RSAPrivateKeySpec / RSAPublicKeySpec并将其与Cipher类一起使用。

See for example this question: Generating RSA keys for given modulus and exponent 例如,请参见以下问题: 为给定的模数和指数生成RSA密钥

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM