RSA如何加密?
我想编写自己的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
首先要做的是查看 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.
你没有问一个真正的问题,但我看到了一些问题,无论如何
我不知道这应该做什么,但这是错误的。您指的是
Math.Pow()
而不是^
吗?无论如何,通常您只需使用一些带有很少设置位的小常量来使e
加密速度更快。e=3
或e=65
可以正常工作。您似乎没有计算私钥(
d
),甚至没有将公钥(e
,p*q
)存储在全部。当您开始使用大数字时,
int
和double
(??) 将无法容纳它们。使用BigInteger
相反。如果
功率> 1
,testPow永远不会是prime...我没有看过
testPrime()
,但是你应该能够编写一些快速的单元测试来说服自己它是否可能有效。You didn't ask a real question, but I see a couple of problems anyways
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 fore
to make encryption fast.e=3
ore=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
anddouble
(??) will not be able to hold them. UseBigInteger
instead.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.我建议阅读/复制现有的实现以供参考,例如 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
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)