java反向加密代码 java加密算法代码

求一个用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 );

推荐阅读