java小程序,怎么把界面和算法结合起来

java小程序,怎么把界面和算法结合起来,第1张

建议在JFrame中增加一个Button,

然后在Button上增拍歼此加一个事件监听函数

Button b= new Button(String  button名)

b.addActionListener( new ActionListener() {

@Override

  袭迅  public void actionPerformed(ActionEvent e) {

        String str =  field1.getText()

   改誉     field2.setText(str.length())

    }

})

public class mySecurity {

private static KeyGenerator keygen

private static SecretKey secretKey

private static Cipher cipher

private static mySecurity security = null

private mySecurity(){

}

public static mySecurity getInstance() throws Exception{

if(security == null){

security = new mySecurity()

keygen = KeyGenerator.getInstance("AES")

secretKey = keygen.generateKey()

cipher =Cipher.getInstance("AES")

}

return security

}

//加密

public String encrypt(String str) throws Exception{

cipher.init(Cipher.ENCRYPT_MODE,secretKey)

byte [] src = str.getBytes() byte [] enc = cipher.doFinal(src)

return parseByte2HexStr(enc) }

//解密

public String decrypt(String str) throws Exception{

cipher.init(Cipher.DECRYPT_MODE,secretKey)

byte[] enc = parseHexStr2Byte(str) byte [] dec = cipher.doFinal(enc)

return new String(dec) }

/**将16进制转换为二进制

* @param hexStr

* @return

*/

public static byte[] parseHexStr2Byte(String hexStr) {

if (hexStr.length() <1)

return null

byte[] result = new byte[hexStr.length()/2]

for (int i = 0i<hexStr.length()/2i++) {

int high = Integer.parseInt(hexStr.substring(i*2, i*2+1), 16)

int low = Integer.parseInt(hexStr.substring(i*2+1, i*2+2), 16)

result[i] = (byte) (high * 16 + low)

}

return result

}

/**将二进制转换成16进制

* @param buf

* @return

*/

public static String parseByte2HexStr(byte buf[]) {

StringBuffer sb = new StringBuffer()

for (int i = 0i <buf.lengthi++) {

String hex = Integer.toHexString(buf[i] &0xFF)

if (hex.length() == 1) {

hex = '0' + hex

}

sb.append(hex.toUpperCase())

}

return sb.toString()

}

public static void main(String[] args) throws Exception{

String str = "abc haha 我"

String ss = mySecurity.getInstance().encrypt(str)

System.out.println(ss)

System.out.println(mySecurity.getInstance().decrypt(ss))

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12407131.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-25
下一篇 2023-05-25

发表评论

登录后才能评论

评论列表(0条)

保存