简体   繁体   English

如何在RSA中加密?

[英]How to encrypt in RSA?

I want to write my own RSA encrypter without libaries! 我要编写没有库的RSA加密器!

Code: 码:

import java.util.Random;

public class Main {

public static void main(String[] args) {
    System.out.println(createPrime());
}

private static byte encrypt(byte message) {
    double p = createPrime();
    double q = createPrime();
    double e = 2 ^ new Random().nextInt(100) % (p * q);
    byte ciphered = Math.pow(message, e);
    return ciphered;
}

private static double createPrime() {
    double testPow;
    do {
    int test = new Random().nextInt(20);
    int power = new Random().nextInt(20);
    test += 1;
    power +=1;
    testPow = Math.pow(test, power);
    System.out.println("Double Math.pow: " + testPow);
    } while (!testPrime(testPow));
    return testPow;
}

private static Boolean testPrime(Double test) {
    int factor = 2;
    int lastFactor = 1;
    while (test > 1) {
        if (test % factor == 0) {
            lastFactor = factor;
            test /= factor;
            while (test % factor == 0) {
                test /= factor;
            }
        }
        factor++;
    }
    Boolean isPrime = false;
    if (test == lastFactor) {
        isPrime = true;
    }
    return isPrime;
 }
}

This is what I have for encrypting. 这就是我要加密的内容。 I don't know what I should do to correct this, but I have pretty much done this by hand before trying this. 我不知道该怎么做才能更正此问题,但是在尝试之前,我已经做了很多手工工作。

So I know that the equation to encrypt is c = m^e (mod N) and decryption m = c^d (mod N) 所以我知道要加密的方程是c = m ^ e(mod N)和解密m = c ^ d(mod N)

where p, q are prime numbers - m is message - c is ciphered text - e is totient of N - N is p times q - totient of N is (p-1)(q-1) 其中p,q是质数-m是消息-c是密文-e是N的上位字母-N是p的乘以q-N的上位字母是(p-1)(q-1)

Any help is appreciated 任何帮助表示赞赏

The first thing to do is to have a look at the class java.math.BigInteger. 首先要做的是查看类java.math.BigInteger。 This class will help you a lot to implement "School-book" RSA. 该课程将为您实施“学校手册” RSA带来很大帮助。

You didn't ask a real question, but I see a couple of problems anyways 您没有问一个真正的问题,但是我仍然看到几个问题


double e = 2 ^ new Random().nextInt(100) % (p * q);

I don't know what this is supposed to do, but it's wrong. 我不知道该怎么办,但这是错误的。 Did you mean Math.Pow() rather than ^ ? 您是说Math.Pow()而不是^吗? In any case, usually you just use some small constant with very few set bits for e to make encryption fast. 在任何情况下,通常都只使用一些很小的常数(带有很少的设置位)来使e更快地进行加密。 e=3 or e=65 would work fine. e=3e=65可以正常工作。

You don't seem to be calculating the private key ( d ), or even storing the public key ( e , p*q ) at all. 您似乎根本没有计算私钥( d ),甚至根本没有存储公钥( ep*q )。

When you start using large numbers, int and double (??) will not be able to hold them. 当您开始使用大数时, intdouble (??)将无法保留它们。 Use BigInteger instead. 改用BigInteger


do {
    testPow = Math.pow(test, power);
} while (!testPrime(testPow));

If power > 1 , testPow will never be prime... 如果power > 1 ,则testPow将永远不是素数...


I have not looked at testPrime() , but you should be able to write some quick unit tests to convince yourself whether or not it probably works. 我没有看过testPrime() ,但是您应该能够编写一些快速的单元测试来说服自己是否可行。

Java has built-in encryption algorithms under the java.security package. Java在java.security包下具有内置的加密算法。 Check this . 检查一下 So no need for external libraries. 因此,无需外部库。 I see no production need to implement it yourself. 我认为没有生产需要自己实施。 Only if it is homework (which you didn't tag) 仅当是家庭作业(您未标记)时

I'd suggest reading/copying an existing implementation for reference, such as BouncyCastle: http://www.docjar.com/html/api/org/bouncycastle/crypto/engines/RSACoreEngine.java.html 我建议阅读/复制现有的实现以供参考,例如BouncyCastle: http : //www.docjar.com/html/api/org/bouncycastle/crypto/engines/RSACoreEngine.java.html

By the way, if you want this to be at all secure, you should be using java.security.SecureRandom, not java.util.Random 顺便说一句,如果您希望此操作完全安全,则应该使用java.security.SecureRandom,而不是java.util.Random

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

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