📄 elgamal.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.io.*;
import java.math.*;
public class ElGamal extends JInternalFrame implements ActionListener
{
// Parameters Begin
private JTextField bTextField;
private JTextField cTextField;
private JTextField pTextField;
private JTextField rTextField;
// Parameters End
private JButton encryptButton, decryptButton, loadButton, saveButton;
private JTextArea inputTextArea, outputTextArea;
private JPanel parameterPanel, textPanel, inputPanel, outputPanel, topInputPanel, topOutputPanel;
private Container mainContainer;
private final int width=400, height=600;
private boolean casechange=false, clean=false;
public ElGamal()
{
super("ElGamal Cipher", true, true, true, true);
mainContainer = getContentPane();
mainContainer.setLayout(new BorderLayout(0,0));
// Parameter Panel Begin
bTextField = new JTextField(5);
cTextField = new JTextField(5);
pTextField = new JTextField(5);
rTextField = new JTextField(5);
parameterPanel = new JPanel();
parameterPanel.add(new JLabel("b="));
parameterPanel.add(bTextField);
parameterPanel.add(new JLabel("c="));
parameterPanel.add(cTextField);
parameterPanel.add(new JLabel("p="));
parameterPanel.add(pTextField);
parameterPanel.add(new JLabel("r="));
parameterPanel.add(rTextField);
// Parameter Panel End / Input Panel Begin
loadButton = new JButton("Load");
loadButton.addActionListener(this);
topInputPanel = new JPanel();
topInputPanel.add(new JLabel("Enter text or "));
topInputPanel.add(loadButton);
inputTextArea = new JTextArea();
inputTextArea.setAutoscrolls(true);
inputTextArea.setLineWrap(true);
inputTextArea.setWrapStyleWord(true);
inputPanel = new JPanel();
inputPanel.setLayout(new BorderLayout(0,0));
inputPanel.add(topInputPanel,BorderLayout.NORTH);
inputPanel.add(new JScrollPane(inputTextArea, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER),BorderLayout.CENTER);
// Input Panel End / Output Panel Begin
encryptButton = new JButton("Encrypt");
encryptButton.addActionListener(this);
decryptButton = new JButton("Decrypt");
decryptButton.addActionListener(this);
saveButton = new JButton("Save");
saveButton.addActionListener(this);
topOutputPanel = new JPanel();
topOutputPanel.add(encryptButton);
topOutputPanel.add(new JLabel(" or "));
topOutputPanel.add(decryptButton);
topOutputPanel.add(new JLabel(" or "));
topOutputPanel.add(saveButton);
outputTextArea = new JTextArea();
outputTextArea.setAutoscrolls(true);
outputTextArea.setLineWrap(true);
outputTextArea.setWrapStyleWord(true);
outputTextArea.setEditable(false);
outputPanel = new JPanel();
outputPanel.setLayout(new BorderLayout(0,0));
outputPanel.add(topOutputPanel,BorderLayout.NORTH);
outputPanel.add(new JScrollPane(outputTextArea, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER),BorderLayout.CENTER);
// Output Panel End
textPanel = new JPanel();
textPanel.setLayout(new GridLayout(2,1));
textPanel.add(inputPanel);
textPanel.add(outputPanel);
mainContainer.add(parameterPanel, BorderLayout.NORTH);
mainContainer.add(textPanel, BorderLayout.CENTER);
setSize(width,height);
setLocation(450,0);
show();
}
public String cleanString(String toClean)
{
String returnString = new String();
for(int i=0;i<toClean.length();i++)
{
if (Character.isLetter(toClean.charAt(i)) == true)
returnString = returnString + toClean.charAt(i);
}
return returnString;
}
public void actionPerformed(ActionEvent e)
{
outputTextArea.setText("");
if (e.getSource()==encryptButton)
{
String toEncrypt = new String(inputTextArea.getText());
BigInteger B = new BigInteger(bTextField.getText());
BigInteger C = new BigInteger(cTextField.getText());
BigInteger P = new BigInteger(pTextField.getText());
BigInteger R = new BigInteger(rTextField.getText());
BigInteger Q = new BigInteger("0");
BigInteger Y = new BigInteger("0");
Q = P.subtract(BigInteger.ONE);
System.out.println(B.modPow(Q,P).intValue());
if (B.modPow(Q,P).intValue()!=1)
outputTextArea.setText("B is not a near primitive root of P");
else
{
if(toEncrypt.length()%2!=0);
toEncrypt = toEncrypt + "0";
char charArray[] = toEncrypt.toCharArray();
String s = new String();
for(int i=0;i<toEncrypt.length()-1;i+=2)
{
BigInteger X = new BigInteger(""+(((int)charArray[i]-48)*10+(int)charArray[i+1]-48));
Y = X.multiply(C.pow(R.intValue()));
Y = Y.mod(P);
s = s + Y.toString() + ", ";
}
char charAr[] = s.toCharArray();
charAr[s.length()-1] = ' ';
charAr[s.length()-2] = ' ';
outputTextArea.setText(new String(charAr));
}
}
else if (e.getSource()==decryptButton)
{
String toDecrypt = new String(inputTextArea.getText());
BigInteger B = new BigInteger(bTextField.getText());
BigInteger C = new BigInteger(cTextField.getText());
BigInteger P = new BigInteger(pTextField.getText());
BigInteger R = new BigInteger(rTextField.getText());
BigInteger Q = new BigInteger("0");
BigInteger Y = new BigInteger("0");
String s = new String();
char charArray[] = toDecrypt.toCharArray();
StringTokenizer st = new StringTokenizer(inputTextArea.getText(), "();., ");
while (st.hasMoreTokens())
{
BigInteger V = new BigInteger(st.nextToken());
Q= C.modPow(R,P);
Q= Q.modInverse(P);
Q = V.multiply(Q).mod(P);
if (Q.toString().length()==1)
s = s + "0" + Q.toString();
else
s = s + Q.toString();
}
outputTextArea.setText(s);
}
else if (e.getSource()==loadButton)
{
JFileChooser fileChooser = new JFileChooser();
JOptionPane optionPane;
BufferedReader file;
int dialogvalue;
String temp;
try
{
dialogvalue = fileChooser.showOpenDialog(mainContainer);
repaint();
if (dialogvalue == fileChooser.CANCEL_OPTION || dialogvalue == fileChooser.ERROR_OPTION)
{
optionPane = new JOptionPane();
optionPane.showMessageDialog(mainContainer, "File not loaded.");
}
else
{
file = new BufferedReader(new FileReader(fileChooser.getSelectedFile()));
inputTextArea.setText("");
while ((temp = file.readLine()) != null)
inputTextArea.setText(inputTextArea.getText()+temp+"\n");
file.close();
}
}
catch (Exception ex)
{
optionPane = new JOptionPane();
optionPane.showMessageDialog(mainContainer, "Error!");
};
}
else if (e.getSource() == saveButton)
{
JFileChooser fileChooser = new JFileChooser();
JOptionPane optionPane;
BufferedWriter file;
int dialogvalue;
try
{
dialogvalue = fileChooser.showOpenDialog(mainContainer);
repaint();
if (dialogvalue == fileChooser.CANCEL_OPTION || dialogvalue == fileChooser.ERROR_OPTION)
{
optionPane = new JOptionPane();
optionPane.showMessageDialog(mainContainer, "File not loaded.");
}
else
{
file = new BufferedWriter(new FileWriter(fileChooser.getSelectedFile()));
file.write(outputTextArea.getText());
file.close();
}
}
catch (Exception ex)
{
optionPane = new JOptionPane();
optionPane.showMessageDialog(mainContainer, "Error!");
};
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -