java ssha_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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值