破解 RSA 的有效方法?
我正在尝试找到一种更有效的方法来破解 RSA 密码。 下面的非常非优化的小程序使用公共指数和模数破解了简单的 RSA 密码。请记住,这是暴力攻击,因此破解时间取决于您的机器。
编辑:我正在寻找基于代码的解决方案,而不是寻找有关如何解决问题的理论。
import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;
import java.awt.event.*;
import java.math.BigInteger;
public class RSACracker extends JApplet implements ActionListener {
private static final long serialVersionUID = 1 L;
String[] fieldNames = {
"Modulus(n):",
"Public exponent(e):",
"Cipher(c):",
"Message(m):"
};
JComponent[] jca = new JComponent[fieldNames.length];
int[][] fs = {
{
16,
1
},
{
16,
1
},
{
16,
1
},
{
16,
10
}
};
Container cp;
@Override
public void init() {
cp = getContentPane();
cp.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.WEST;
gbc.insets = new Insets(5, 10, 5, 5);
for (int i = 0; i < fieldNames.length; ++i) {
gbc.gridwidth = GridBagConstraints.RELATIVE;
cp.add(new JLabel(fieldNames[i]), gbc);
gbc.gridwidth = GridBagConstraints.REMAINDER;
if (fs[i][1] == 1) {
JTextField tf = new JTextField(fs[i][0]);
jca[i] = tf;
cp.add(tf, gbc);
} else {
JTextArea ta = new JTextArea(fs[i][1], fs[i][0]);
ta.setLineWrap(true);
ta.setWrapStyleWord(true);
JScrollPane jsp = new JScrollPane(ta,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
jca[i] = ta;
cp.add(jsp, gbc);
}
}
JButton b = new JButton("Calculate");
b.addActionListener(this);
gbc.anchor = GridBagConstraints.EAST;
cp.add(b, gbc);
cp.setSize(400, 400);
//My Test: p=51407, q=63667, message=123456
((JTextComponent) jca[0]).setText("1760806643"); //n - modulus
((JTextComponent) jca[1]).setText("65537"); //e - public exponent
((JTextComponent) jca[2]).setText("818474911"); //c - cipher
//Wiki Test: p = 61 and q = 53, message = 65
// ((JTextComponent) jca[0]).setText("3233"); //n - modulus
// ((JTextComponent) jca[1]).setText("17"); //e - public exponent
// ((JTextComponent) jca[2]).setText("2790"); //c - cipher
}
@Override
public void start() {
setSize(450, 450);
repaint();
}
public void actionPerformed(ActionEvent evt) {
BigInteger n = new BigInteger(((JTextComponent) jca[0]).getText());
BigInteger e = new BigInteger(((JTextComponent) jca[1]).getText());
BigInteger c = new BigInteger(((JTextComponent) jca[2]).getText());
BigInteger p = BigInteger.ONE;
BigInteger q = BigInteger.ZERO;
while (true) {
p = p.add(BigInteger.ONE);
if (n.mod(p).compareTo(BigInteger.ZERO) == 0) {
q = n.divide(p);
if (p.multiply(q).compareTo(n) == 0)
break;
}
}
BigInteger totient = p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE));
// System.out.println("p="+p+"\n"+"q="+q+"\n"+"totient="+totient);
BigInteger d = e.modInverse(totient);
BigInteger m = c.modPow(d, n);
((JTextComponent) jca[fieldNames.length - 1]).setText(m.toString());
}
}
I'm trying to find a more efficient way how to break the RSA cipher.
Bellow very non-optimal applet break simple RSA cipher using public exponent and modulus. Remember that it is brute-force attack, so breaking time depend on your machine.
EDIT: I'm looking for a code-based solution, not for theory about how to solve a problem.
import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;
import java.awt.event.*;
import java.math.BigInteger;
public class RSACracker extends JApplet implements ActionListener {
private static final long serialVersionUID = 1 L;
String[] fieldNames = {
"Modulus(n):",
"Public exponent(e):",
"Cipher(c):",
"Message(m):"
};
JComponent[] jca = new JComponent[fieldNames.length];
int[][] fs = {
{
16,
1
},
{
16,
1
},
{
16,
1
},
{
16,
10
}
};
Container cp;
@Override
public void init() {
cp = getContentPane();
cp.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.WEST;
gbc.insets = new Insets(5, 10, 5, 5);
for (int i = 0; i < fieldNames.length; ++i) {
gbc.gridwidth = GridBagConstraints.RELATIVE;
cp.add(new JLabel(fieldNames[i]), gbc);
gbc.gridwidth = GridBagConstraints.REMAINDER;
if (fs[i][1] == 1) {
JTextField tf = new JTextField(fs[i][0]);
jca[i] = tf;
cp.add(tf, gbc);
} else {
JTextArea ta = new JTextArea(fs[i][1], fs[i][0]);
ta.setLineWrap(true);
ta.setWrapStyleWord(true);
JScrollPane jsp = new JScrollPane(ta,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
jca[i] = ta;
cp.add(jsp, gbc);
}
}
JButton b = new JButton("Calculate");
b.addActionListener(this);
gbc.anchor = GridBagConstraints.EAST;
cp.add(b, gbc);
cp.setSize(400, 400);
//My Test: p=51407, q=63667, message=123456
((JTextComponent) jca[0]).setText("1760806643"); //n - modulus
((JTextComponent) jca[1]).setText("65537"); //e - public exponent
((JTextComponent) jca[2]).setText("818474911"); //c - cipher
//Wiki Test: p = 61 and q = 53, message = 65
// ((JTextComponent) jca[0]).setText("3233"); //n - modulus
// ((JTextComponent) jca[1]).setText("17"); //e - public exponent
// ((JTextComponent) jca[2]).setText("2790"); //c - cipher
}
@Override
public void start() {
setSize(450, 450);
repaint();
}
public void actionPerformed(ActionEvent evt) {
BigInteger n = new BigInteger(((JTextComponent) jca[0]).getText());
BigInteger e = new BigInteger(((JTextComponent) jca[1]).getText());
BigInteger c = new BigInteger(((JTextComponent) jca[2]).getText());
BigInteger p = BigInteger.ONE;
BigInteger q = BigInteger.ZERO;
while (true) {
p = p.add(BigInteger.ONE);
if (n.mod(p).compareTo(BigInteger.ZERO) == 0) {
q = n.divide(p);
if (p.multiply(q).compareTo(n) == 0)
break;
}
}
BigInteger totient = p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE));
// System.out.println("p="+p+"\n"+"q="+q+"\n"+"totient="+totient);
BigInteger d = e.modInverse(totient);
BigInteger m = c.modPow(d, n);
((JTextComponent) jca[fieldNames.length - 1]).setText(m.toString());
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论