?? myframe.java
字號:
signet = java.security.Signature.getInstance("DSA");
signet.initSign(rk);// 初始一個Signature對象,
signet.update(message.getBytes());
signed=signet.sign();
//System.out.println(signed);//
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("."));
chooser.setDialogTitle("save signature");
int i = chooser.showSaveDialog(this);
if (i == JFileChooser.APPROVE_OPTION) {
ObjectOutputStream oos;
try {
oos = new ObjectOutputStream(
new FileOutputStream(chooser.getSelectedFile()));
oos.writeObject(signed);
oos.flush();
oos.close();
} catch (FileNotFoundException e) {
JOptionPane.showMessageDialog(this, e.getMessage());
} catch (IOException e) {
JOptionPane.showMessageDialog(this, e.getMessage());
}
}
} catch (NoSuchAlgorithmException e) {
JOptionPane.showMessageDialog(this, e.getMessage());
} catch (InvalidKeyException e) {
JOptionPane.showMessageDialog(this, e.getMessage());
} catch (SignatureException e) {
JOptionPane.showMessageDialog(this, e.getMessage());
}
}
private void getRK() {
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("."));
chooser.setDialogTitle("get private key");
int i = chooser.showOpenDialog(this);
if (i == JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
try {
ObjectInputStream ois=new ObjectInputStream(new FileInputStream(file));
rk=(PrivateKey)ois.readObject();
ois.close();
} catch (FileNotFoundException e) {
JOptionPane.showMessageDialog(this, e.getMessage());
} catch (IOException e) {
JOptionPane.showMessageDialog(this, e.getMessage());
} catch (ClassNotFoundException e) {
JOptionPane.showMessageDialog(this, e.getMessage());
}
}
}
private void getPK() {
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("."));
chooser.setDialogTitle("get public key");
int i = chooser.showOpenDialog(this);
if (i == JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
try {
ObjectInputStream ois=new ObjectInputStream(new FileInputStream(file));
pk=(PublicKey)ois.readObject();
ois.close();
} catch (FileNotFoundException e) {
JOptionPane.showMessageDialog(this, e.getMessage());
} catch (IOException e) {
JOptionPane.showMessageDialog(this, e.getMessage());
} catch (ClassNotFoundException e) {
JOptionPane.showMessageDialog(this, e.getMessage());
}
}
}
private void getKPair(String alg) {
try {
KeyPairGenerator kg = KeyPairGenerator.getInstance(alg);
System.out.println("Generating key pair ...");
kg.initialize(512);
KeyPair keyPair = kg.genKeyPair();// 生成密鑰組
pk = keyPair.getPublic();
rk = keyPair.getPrivate();
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("."));
chooser.setDialogTitle("save public key");
int i = chooser.showSaveDialog(this);
if (i == JFileChooser.APPROVE_OPTION) {
ObjectOutputStream oos = new ObjectOutputStream(
new FileOutputStream(chooser.getSelectedFile()));
oos.writeObject(pk);
oos.flush();
oos.close();
}
chooser.setDialogTitle("save private key");
i = chooser.showSaveDialog(this);
if (i == JFileChooser.APPROVE_OPTION) {
ObjectOutputStream oos = new ObjectOutputStream(
new FileOutputStream(chooser.getSelectedFile()));
oos.writeObject(rk);
oos.close();
}
} catch (Exception e) {
JOptionPane.showMessageDialog(this, e.getMessage());
}
}
private String openFile(String label) {
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("."));
chooser.setDialogTitle(label);
int i = chooser.showOpenDialog(this);
StringBuffer s = new StringBuffer();
StringBuffer s1 = new StringBuffer();
if (i == JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
try {
FileReader reader = new FileReader(file);
Scanner scan = new Scanner(reader);
while (scan.hasNextLine()) {
String str = scan.nextLine();
s.append(str);
s1.append(str + "\n");
}
scan.close();
} catch (FileNotFoundException e) {
JOptionPane.showMessageDialog(this, e.getMessage());
return "";
}
}
messageShow = s1.toString();
return s.toString();
}
}
class MyDESCipher extends JPanel implements ActionListener {
JTextArea myInfoArea = new JTextArea();
JButton getDESKey = new JButton("GetDESK");// 生成密鑰并保存到文件
JButton openMessage = new JButton("OpenMessage");// 導入消息或已加密的信息
JButton openKey = new JButton("OpenK");// 導入密鑰
JButton encrypt = new JButton("Encrypt");// 對消息進行加密
JButton decrypt = new JButton("Decrypt");// 對消息進行解密
SecretKey deskey =null;
String message=null;
String messageShow=null;
public MyDESCipher() {
this.setLayout(new BorderLayout());
this.add(new JScrollPane(myInfoArea), BorderLayout.CENTER);
JPanel buttons = new JPanel(new FlowLayout());
buttons.add(getDESKey);
buttons.add(openMessage);
buttons.add(openKey);
buttons.add(encrypt);
buttons.add(decrypt);
getDESKey.addActionListener(this);
openMessage.addActionListener(this);
openKey.addActionListener(this);
encrypt.addActionListener(this);
decrypt.addActionListener(this);
this.add(buttons, BorderLayout.SOUTH);
}
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (command.equals("GetDESK")) {
getDESK();
} else if (command.equals("OpenMessage")) {
message=openFile("open message");
myInfoArea.append(messageShow);
} else if (command.equals("OpenK")) {
openKey();
} else if (command.equals("Encrypt")) {
encrypt();
}
else if (command.equals("Decrypt")) {
decrypt();
}
}
private void decrypt() {
byte[] cipherByte=null;
byte[] clearByte=null;
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("."));
chooser.setDialogTitle("get cipher");
int i = chooser.showOpenDialog(this);
if (i == JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
try {
ObjectInputStream ois=new ObjectInputStream(new FileInputStream(file));
cipherByte=(byte[])ois.readObject();
ois.close();
} catch (FileNotFoundException e) {
JOptionPane.showMessageDialog(this, e.getMessage());
} catch (IOException e) {
JOptionPane.showMessageDialog(this, e.getMessage());
} catch (ClassNotFoundException e) {
JOptionPane.showMessageDialog(this, e.getMessage());
}
}
Cipher c1;
try {
c1 = Cipher.getInstance("DES");
c1.init(Cipher.DECRYPT_MODE,deskey);
clearByte=c1.doFinal(cipherByte);
} catch (NoSuchAlgorithmException e) {
JOptionPane.showMessageDialog(this, e.getMessage());
} catch (NoSuchPaddingException e) {
JOptionPane.showMessageDialog(this, e.getMessage());
} catch (InvalidKeyException e) {
JOptionPane.showMessageDialog(this, e.getMessage());
} catch (IllegalBlockSizeException e) {
JOptionPane.showMessageDialog(this, e.getMessage());
} catch (BadPaddingException e) {
JOptionPane.showMessageDialog(this, e.getMessage());
}
try {
myInfoArea.append(new String(clearByte, "UTF8"));
} catch (UnsupportedEncodingException e) {
JOptionPane.showMessageDialog(this, e.getMessage());
}
}
private void encrypt() {
Cipher c1;
try {
c1 = Cipher.getInstance("DES");
c1.init(Cipher.ENCRYPT_MODE,deskey);
byte[] cipherByte=c1.doFinal(message.getBytes("UTF8"));
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("."));
chooser.setDialogTitle("save cipher");
int i = chooser.showSaveDialog(this);
if (i == JFileChooser.APPROVE_OPTION) {
ObjectOutputStream oos;
try {
oos = new ObjectOutputStream(
new FileOutputStream(chooser.getSelectedFile()));
oos.writeObject(cipherByte);
oos.flush();
oos.close();
} catch (FileNotFoundException e) {
JOptionPane.showMessageDialog(this, e.getMessage());
} catch (IOException e) {
JOptionPane.showMessageDialog(this, e.getMessage());
}
}
} catch (NoSuchAlgorithmException e) {
JOptionPane.showMessageDialog(this, e.getMessage());
} catch (NoSuchPaddingException e) {
JOptionPane.showMessageDialog(this, e.getMessage());
} catch (InvalidKeyException e) {
JOptionPane.showMessageDialog(this, e.getMessage());
} catch (IllegalBlockSizeException e) {
JOptionPane.showMessageDialog(this, e.getMessage());
} catch (BadPaddingException e) {
JOptionPane.showMessageDialog(this, e.getMessage());
} catch (UnsupportedEncodingException e) {
JOptionPane.showMessageDialog(this, e.getMessage());
}
}
private void openKey() {
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("."));
chooser.setDialogTitle("open des key");
int i = chooser.showOpenDialog(this);
if (i == JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
try {
ObjectInputStream ois=new ObjectInputStream(new FileInputStream(file));
deskey=(SecretKey)ois.readObject();
ois.close();
} catch (FileNotFoundException e) {
JOptionPane.showMessageDialog(this, e.getMessage());
} catch (IOException e) {
JOptionPane.showMessageDialog(this, e.getMessage());
} catch (ClassNotFoundException e) {
JOptionPane.showMessageDialog(this, e.getMessage());
}
}
}
private String openFile(String label) {
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("."));
chooser.setDialogTitle(label);
int i = chooser.showOpenDialog(this);
StringBuffer s = new StringBuffer();
StringBuffer s1 = new StringBuffer();
if (i == JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
try {
FileReader reader = new FileReader(file);
Scanner scan = new Scanner(reader);
while (scan.hasNextLine()) {
String str = scan.nextLine();
s.append(str);
s1.append(str + "\n");
}
scan.close();
} catch (FileNotFoundException e) {
JOptionPane.showMessageDialog(this, e.getMessage());
return "";
}
}
messageShow = s1.toString();
return s.toString();
}
private void getDESK() {
KeyGenerator keygen;
try {
keygen = KeyGenerator.getInstance("DES");
deskey = keygen.generateKey();
JFileChooser chooser = new JFileChooser();
chooser.setDialogTitle("save des key");
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -