Java实现可逆加密解密
public class CryptoUtil {
private static final String DEFAULT_SECRET_KEY1 = "?:P)(OL><KI*&UJMNHY^%TGBVFR$#EDCXSW@!QAZ"; private static final String DEFAULT_SECRET_KEY2 = "1qaz2wsx3edc4rfv5tgb6yhn7ujm8ik,9ol.0p;/";
private static final String DEFAULT_SECRET_KEY3 = "!QAZ@WSX#EDC$RFV%TGB^YHN&UJM*IK<(OL>)P:?"; private static final String DEFAULT_SECRET_KEY4 = "1qaz@WSX3edc$RFV5tgb^YHN7ujm*IK<9ol.)P:?";
private static final String DEFAULT_SECRET_KEY5 = "!QAZ2wsx#EDC4rfv%TGB6yhn&UJM8ik,(OL>0p;/";
private static final String DEFAULT_SECRET_KEY6 = "1qaz2wsx3edc4rfv5tgb^YHN&UJM*IK<(OL>)P:?";
private static final String DEFAULT_SECRET_KEY = DEFAULT_SECRET_KEY1;
private static final String SALT = "20121204";
private static final String DES = "DES";
private static final Base32 base32 = new Base32();
private static Key DEFAULT_KEY = null;
static {
DEFAULT_KEY = obtainKey(DEFAULT_SECRET_KEY);
}
/**
* 获得key
**/
private static Key obtainKey(String key) {
if (key == null) {
return DEFAULT_KEY;
}
KeyGenerator generator = null;
try {
generator = Instance(DES);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
SecureRandom random = null;
try {
random = Instance("SHA1PRNG");
random.Bytes());
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
generator.init(random);
Key key1 = ateKey();
generator = null;
return key1;
}
/**
* 加密<br>
* String明⽂输⼊,String密⽂输出
*/
private static String encode(String str) {
return encode64(null, str);
}
/**
* 加密<br>
* String明⽂输⼊,String密⽂输出
*/
private static String encode64(String key, String str) {
deBase64URLSafeString(obtainEncode(key, Bytes()));
deBase64URLSafeString(obtainEncode(key, Bytes()));
}
/**
* 加密<br>
* String明⽂输⼊,String密⽂输出
*/
private static String encode32(String key, String str) {
deAsString(obtainEncode(key, Bytes())).replaceAll("=", ""); }
/**
* 加密<br>
* String明⽂输⼊,String密⽂输出
*/
private static String encode16(String key, String str) {
deHexString(obtainEncode(key, Bytes()));
}
/**
* 解密<br>
* 以String密⽂输⼊,String明⽂输出
*/
private static String decode(String str) {
return decode64(null, str);
}
/
**
* 解密<br>
* 以String密⽂输⼊,String明⽂输出
*/
private static String decode64(String key, String str) {
return new String(obtainDecode(key, Base64.decodeBase64(str)));
}
/**
* 解密<br>
* 以String密⽂输⼊,String明⽂输出
*/
private static String decode32(String key, String str) {
return new String(obtainDecode(key, base32.decode(str)));
}
/**
* 解密<br>
* 以String密⽂输⼊,String明⽂输出
*/
private static String decode16(String key, String str) {
try {
return new String(obtainDecode(key, Hex.CharArray())));
} catch (DecoderException e) {
e.printStackTrace();
}
return null;
}
/**
* 加密<br>
* 以byte[]明⽂输⼊,byte[]密⽂输出
*/
private static byte[] obtainEncode(String key, byte[] str) {
byte[] byteFina = null;
Cipher cipher;
try {
Key key1 = obtainKey(key);
cipher = Instance(DES);
cipher = Instance(DES);
cipher.init(Cipher.ENCRYPT_MODE, key1);
byteFina = cipher.doFinal(str);
} catch (Exception e) {
e.printStackTrace();
} finally {
cipher = null;
}
return byteFina;
}
/**
* 解密<br>
* 以byte[]密⽂输⼊,以byte[]明⽂输出
*/
private static byte[] obtainDecode(String key, byte[] str) {
Cipher cipher;
byte[] byteFina = null;
try {
Key key1 = obtainKey(key);
cipher = Instance(DES);
cipher.init(Cipher.DECRYPT_MODE, key1);
byteFina = cipher.doFinal(str);
} catch (Exception e) {
e.printStackTrace();java加密方式有哪些
} finally {
cipher = null;
}
return byteFina;
}
public static String encodeSrc(String src) {
String m = encode64(DEFAULT_SECRET_KEY2 + SALT, src); String saltm = SALT + ";" + m;
String result = encode32(DEFAULT_SECRET_KEY3, saltm); return result;
}
public static String decodeTarget(String target) {
String n = decode32(DEFAULT_SECRET_KEY3, target);
String key = n.split(";")[0];
String m = n.split(";")[1];
String result = decode64(DEFAULT_SECRET_KEY2 + key, m); return result;
}
public static void main(String[] args) {
String x = "123456";
String y = encodeSrc(x);
System.out.println(y);
System.out.println(decodeTarget(y));
y = encode64(SALT, x);
System.out.println(y);
System.out.println(decode64(SALT, y));
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论