求一个用java编写的可逆的加密算法程序,自己写的小程序也行 。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 =https://www.04ip.com/post/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 = 0;i hexStr.length()/2; i++) {
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 = 0; ibuf.length; i++) {
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 java反向加密代码我";
String ss =mySecurity.getInstance().encrypt(str) ;
System.out.println(ss);
System.out.println(mySecurity.getInstance().decrypt(ss));
}
}
java 反向加密:输入任意长度的密码,输出反向的密码 怎么做public static String fanxiang(String str) {
StringBuilder sb = new StringBuilder();
for (int i = str.length() - 1; i = 0; i--) {
sb.append(str.substring(i, i + 1));
}
return sb.toString();
}
java加密解密代码package com.cube.limail.util;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;/**
* 加密解密类
*/
publicclassEryptogram
{
privatestaticStringAlgorithm ="DES";
private String key="CB7A92E3D3491964";
//定义 加密算法,可用 DES,DESede,Blowfish
staticbooleandebug= false ;
/**
* 构造子注解.
*/
publicEryptogram ()
{
} /**
* 生成密钥
* @return byte[] 返回生成的密钥
* @throws exception 扔出异常.
*/
publicstaticbyte [] getSecretKey () throwsException
{
KeyGeneratorkeygen= KeyGenerator.getInstance (Algorithm );
SecretKeydeskey= keygen.generateKey ();
System.out.println ("生成密钥:"+bytesToHexString (deskey.getEncoded ()));
if(debug ) System.out.println ("生成密钥:"+bytesToHexString (deskey.getEncoded ()));
returndeskey.getEncoded ();
} /**
* 将指定的数据根据提供的密钥进行加密
* @param input 需要加密的数据
* @param key 密钥
* @return byte[] 加密后的数据
* @throws Exception
*/
publicstaticbyte [] encryptData (byte [] input ,byte [] key ) throwsException
{
SecretKeydeskey= newjavax.crypto.spec.SecretKeySpec (key ,Algorithm );
推荐阅读
- 街机游戏警察格斗,街机警察射击游戏
- 泰斯玛系统与erp的区别,泰玛斯旗舰店
- u盘怎么发送qq群文件,怎么将u盘里的文件发给好友
- 怎么移除pdd小程序,怎么移除小程序拼多多
- go语言能跨平台吗 go语言咋样
- sqlserver2005简版,sql server200
- flutter切换效果,flutter 页面跳转动画
- 即时游戏名字男生网名英文,游戏英文名字男生简单气质
- c语言n阶乘函数 c语言阶乘函数怎么表示