RSA如何加密?

发布于 2024-10-19 06:44:45 字数 1411 浏览 6 评论 0原文

我想编写自己的 RSA 加密器,无需库!

代码:

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;
 }
}

这是我用来加密的。我不知道应该做什么来纠正这个问题,但在尝试这个之前我几乎已经手动完成了此操作。

所以我知道加密的方程式是 c = m^e (mod N) 和解密 m = c^d (mod N)

其中 p、q 是质数 - m 是消息 - c 是密文 - e 是 N 的总值 - N 是 p 乘以 q - N 的总值是 (p-1)(q- 1)

任何帮助表示赞赏

I want to write my own RSA encrypter without libaries!

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)

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)

Any help is appreciated

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

本王不退位尔等都是臣 2024-10-26 06:44:45

首先要做的是查看 java.math.BigInteger 类。本课程将对您实现“教科书”RSA 有很大帮助。

The first thing to do is to have a look at the class java.math.BigInteger. This class will help you a lot to implement "School-book" RSA.

通知家属抬走 2024-10-26 06:44:45

你没有问一个真正的问题,但我看到了一些问题,无论如何


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

我不知道这应该做什么,但这是错误的。您指的是 Math.Pow() 而不是 ^ 吗?无论如何,通常您只需使用一些带有很少设置位的小常量来使 e 加密速度更快。 e=3e=65 可以正常工作。

您似乎没有计算私钥(d),甚至没有将公钥(ep*q)存储在全部。

当您开始使用大数字时,intdouble (??) 将无法容纳它们。使用 BigInteger 相反。


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

如果功率> 1,testPow永远不会是prime...


我没有看过testPrime(),但是你应该能够编写一些快速的单元测试来说服自己它是否可能有效。

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 ^? In any case, usually you just use some small constant with very few set bits for e to make encryption fast. e=3 or e=65 would work fine.

You don't seem to be calculating the private key (d), or even storing the public key (e, p*q) at all.

When you start using large numbers, int and double (??) will not be able to hold them. Use BigInteger instead.


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

If power > 1, testPow will never be prime...


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.

手长情犹 2024-10-26 06:44:45

我建议阅读/复制现有的实现以供参考,例如 BouncyCastle: http://www.docjar.com/html/api/org/bouncycastle/crypto/engines/RSACoreEngine.java.html

顺便说一句,如果你希望它完全安全,您应该使用 java.security.SecureRandom,而不是 java.util.Random

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

By the way, if you want this to be at all secure, you should be using java.security.SecureRandom, not java.util.Random

无言温柔 2024-10-26 06:44:45

Java 在 java.security 包下内置了加密算法。 检查一下。所以不需要外部库。我认为生产不需要自己实现它。仅当它是家庭作业时(您没有标记)

Java has built-in encryption algorithms under the java.security package. 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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文