Java中使用SSHA对数据进行加密的示例
2009-05-31 17:25
555 查看
SshaEncryptTool.java
SshaEncrypt.java
ssha.bat
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
相关文章推荐
- Android使用KeyStore对数据进行加密的示例代码
- Java中使用AES对数据进行加密
- java使用异或对图片数据进行加密
- JAVA中使用JSON进行数据传递示例
- 使用异或对图片数据进行加密.--Java基础017
- java中的使用RSA算法进行公钥加密私钥解密
- 使用md5对数据进行加密
- 使用iServer JAVA API访问iServer数据服务进行数据操作示例
- PHP 使用 mcrypt 扩展中的 mcrypt_encrypt() 和 mcrypt_decrypt() 对数据进行加密和解密
- Java:如何使用Cipher类对字符串进行加密解密
- 前端 使用 crypto-js 对数据进行对称加密
- java中使用MD5加密算法进行加密
- JS使用插件cryptojs进行加密解密数据实例
- 使用java对文件或文件夹进行压缩和加密
- iOS开发中使用AES算法对数据进行加密
- Struts2数据验证与使用Java代码进行数据验证
- JAVA中使用JSON进行数据传递
- java中使用MD5进行加密
- 使用 Feed4JUnit 进行数据与代码分离的 Java 单元测试
- php使用curl存取cookie进行登录抓取数据示例