如果我们捕获公共指数和模数(即使用中间人攻击),许多人会问如何破解RSA密码。 Bellow I我提出了非压缩密码的强力解决方案。可能有人会分享其他解决方案吗?
import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;
import java.awt.event.*;
import java.math.BigInteger;
/**
* Applet break simple RSA cipher using public exponent and modulus.
* Remember that it is brute-force attack, so breaking time depend on your machine.
*
* @author Tomasz Kanik [yetiicom(at)wp(dot)eu]
* @see {@link http://en.wikipedia.org/wiki/RSA} for details about RSA algorithm
*/
public class RSACracker extends JApplet implements ActionListener {
private static final long serialVersionUID = 1L;
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());
}
}