Java中使用SSHA对数据进行加密的示例

SshaEncryptTool.java

import java.awt.FlowLayout; import java.awt.datatransfer.Clipboard; import java.awt.datatransfer.StringSelection; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.JPopupMenu; import javax.swing.JTextField; import javax.swing.WindowConstants; public class SshaEncryptTool extends JFrame { private static final long serialVersionUID = 8249254735235333019L; public static void main(String[] args) { new SshaEncryptTool().setVisible(true); } public SshaEncryptTool() { initComponents(); } private void initComponents() { passwordLabel = new JLabel("パスワード:"); encryptButton = new JButton(); srcTextField = new JTextField(16); targetTextField = new JTextField(34); targetTextField.setEditable(false); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); setLocation(200, 400); setTitle("パスワード暗号化ツール"); encryptButton.setText("暗号化"); encryptButton.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent event) { encryptButtonActionPerformed(event); } }); targetTextField.addMouseListener(new MouseAdapter(){ public void mousePressed(MouseEvent e) { if (e.getButton() == MouseEvent.BUTTON3) { getPopup().show(e.getComponent(), e.getX(), e.getY()); } } }); FlowLayout layout = new FlowLayout(); getContentPane().setLayout(layout); getContentPane().add(passwordLabel); getContentPane().add(srcTextField); getContentPane().add(encryptButton); getContentPane().add(targetTextField); pack(); } private JPopupMenu getPopup() { JPopupMenu popup = new JPopupMenu("Popup"); JMenuItem item = new JMenuItem("コピー"); item.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Clipboard clipboard = getToolkit().getSystemClipboard(); String text = targetTextField.getText(); StringSelection contents = new StringSelection(text); clipboard.setContents(contents, null); targetTextField.requestFocus(); targetTextField.selectAll(); } }); popup.add(item); return popup; } private void encryptButtonActionPerformed(ActionEvent event) { String password = srcTextField.getText(); if(password == null || password.trim().equals("")) { JOptionPane.showMessageDialog(this, "パスワードを入力してください!"); srcTextField.requestFocus(); targetTextField.setText(""); } else { SshaEncrypt ssha = SshaEncrypt.getInstance(); String ssha_password = ssha.createDigest(password); targetTextField.setText(ssha_password); } } private JLabel passwordLabel; private JButton encryptButton; private JTextField srcTextField; private JTextField targetTextField; }

SshaEncrypt.java

import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import sun.misc.BASE64Encoder; public class SshaEncrypt { private static String SecretKey = "test_ssha_key_word"; private static BASE64Encoder enc = new BASE64Encoder(); private static SshaEncrypt inst = new SshaEncrypt("SHA-1"); private MessageDigest sha = null; public static SshaEncrypt getInstance(){ return inst; } public SshaEncrypt(String alg) { try { sha = MessageDigest.getInstance(alg); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } } public String createDigest(String entity) { return createDigest(SecretKey.getBytes(),entity); } public String createDigest(String salt, String entity) { return createDigest(salt.getBytes(),entity); } public String createDigest(byte[] salt, String entity) { sha.reset(); sha.update(entity.getBytes()); sha.update(salt); byte[] pwhash = sha.digest(); return new String(enc.encode(concatenate(pwhash, salt))); } private static byte[] concatenate(byte[] l, byte[] r) { byte[] b = new byte[l.length + r.length]; System.arraycopy(l, 0, b, 0, l.length); System.arraycopy(r, 0, b, l.length, r.length); return b; } }

ssha.bat

@echo off javac SshaEncryptTool.java java SshaEncryptTool pause 

你可能感兴趣的:(Java相关)