算法学习
程序员文章站
2022-07-14 21:06:47
...
//可传入key值的加密算法
public String encrypt(String src, String key)
throws Exception
{
if (null == key || null == src)
{
return null;
}
byte[] raw = key.getBytes("ASCII");
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] encrypted = cipher.doFinal(src.getBytes());
return TypeConvertUtil.byteTS(encrypted).toLowerCase();
}
//对应解密算法
public String decrypt(String src, String key)
throws Exception
{
try
{
if (null == key || null == src)
{
return null;
}
byte[] raw = key.getBytes("ASCII");
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
byte[] encrypted1 = TypeConvertUtil.sTByte(sSrc);
try
{
byte[] original = cipher.doFinal(encrypted1);
String originalString = new String(original);
return originalString;
}
catch (Exception e)
{
return null;
}
}
catch (Exception ex)
{
throw ex;
}
}
public String encrypt(String src, String key)
throws Exception
{
if (null == key || null == src)
{
return null;
}
byte[] raw = key.getBytes("ASCII");
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] encrypted = cipher.doFinal(src.getBytes());
return TypeConvertUtil.byteTS(encrypted).toLowerCase();
}
//对应解密算法
public String decrypt(String src, String key)
throws Exception
{
try
{
if (null == key || null == src)
{
return null;
}
byte[] raw = key.getBytes("ASCII");
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
byte[] encrypted1 = TypeConvertUtil.sTByte(sSrc);
try
{
byte[] original = cipher.doFinal(encrypted1);
String originalString = new String(original);
return originalString;
}
catch (Exception e)
{
return null;
}
}
catch (Exception ex)
{
throw ex;
}
}
上一篇: java虚拟机深入理解
下一篇: jsp编程规范