java明文解密源代码 java 加解密开源工具类( 六 )


try {
byte[] data=https://www.04ip.com/post/base ToBytes(value)
data=https://www.04ip.com/post/CryptUtils decryptData(data key)
return new String(data VALUE_ENCODING)
}catch(Exception e) {
e printStackTrace()
return null;
}
}
}
CryptHelper java
[java]
package gdie lab crypt;
import javax crypto Cipher;
import javax crypto SecretKey;
import javax crypto SecretKeyFactory;
import javax crypto spec DESKeySpec;
import javax crypto spec IvParameterSpec;
import springframework util DigestUtils;
public class CryptHelper{
private static String CRYPT_KEY = zhongqian ;
//加密
private static Cipher ecip;
//解密
private static Cipher dcip;
static {
try {
String KEY = DigestUtils md DigestAsHex(CRYPT_KEY getBytes()) toUpperCase()
KEY = KEY substring( )
byte[] bytes = KEY getBytes()
DESKeySpec ks = new DESKeySpec(bytes)
SecretKeyFactory skf = SecretKeyFactory getInstance( DES )
SecretKey sk = skf generateSecret(ks)
IvParameterSpec iv = new IvParameterSpec(bytes)
ecip = Cipher getInstance( DES/CBC/PKCS Padding )
ecip init(Cipher ENCRYPT_MODE sk iv )
dcip = Cipher getInstance( DES/CBC/PKCS Padding )
dcip init(Cipher DECRYPT_MODE sk iv )
}catch(Exception ex) {
ex printStackTrace()
}
}
public static String encrypt(String content) throws Exception {
byte[] bytes = ecip doFinal(content getBytes( ascii ))
return CryptUtils byte hex(bytes)
}
public static String decrypt(String content) throws Exception {
byte[] bytes= CryptUtils hex byte(content)
bytes = dcip doFinal(bytes)
return new String(bytes ascii )
}
//test
public static void main(String[] args) throws Exception {
String password = gly ;
String en = encrypt(password)
System out println(en)
System out println(decrypt(en))
}
}
package gdie lab crypt;
import javax crypto Cipher;
import javax crypto SecretKey;
import javax crypto SecretKeyFactory;
import javax crypto spec DESKeySpec;
import javax crypto spec IvParameterSpec;
import springframework util DigestUtils;
public class CryptHelper{
private static String CRYPT_KEY = zhongqian ;
//加密
private static Cipher ecip;
//解密
private static Cipher dcip;
static {
try {
String KEY = DigestUtils md DigestAsHex(CRYPT_KEY getBytes()) toUpperCase()
KEY = KEY substring( )
byte[] bytes = KEY getBytes()
DESKeySpec ks = new DESKeySpec(bytes)
SecretKeyFactory skf = SecretKeyFactory getInstance( DES )
SecretKey sk = skf generateSecret(ks)
IvParameterSpec iv = new IvParameterSpec(bytes)
ecip = Cipher getInstance( DES/CBC/PKCS Padding )
ecip init(Cipher ENCRYPT_MODE sk iv )
dcip = Cipher getInstance( DES/CBC/PKCS Padding )
dcip init(Cipher DECRYPT_MODE sk iv )
}catch(Exception ex) {
ex printStackTrace()
}
}
public static String encrypt(String content) throws Exception {
byte[] bytes = ecip doFinal(content getBytes( ascii ))
return CryptUtils byte hex(bytes)
}
public static String decrypt(String content) throws Exception {
byte[] bytes= CryptUtils hex byte(content)
bytes = dcip doFinal(bytes)
return new String(bytes ascii )
}
//test
public static void main(String[] args) throws Exception {
String password = gly ;
String en = encrypt(password)

推荐阅读