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


byte[] cipherByte = c doFinal(input)
// if (debug ) System out println ( 加密后的二进串 +byte hex (cipherByte ))
return cipherByte;
}
public static byte[] encryptData(byte[] input) throws Exception {
return encryptData(input DEFAULT_KEY)
}
/**
* 将给定的已加密的数据通过指定的密钥进行解密
*
* @param input
*待解密的数据
* @param key
*密钥
* @return byte[] 解密后的数据
* @throws Exception
*/
public static byte[] decryptData(byte[] input byte[] key) throws Exception {
SecretKey deskey = new javax crypto spec SecretKeySpec(key Algorithm)
// if (debug ) System out println ( 解密前的信息 +byte hex (input ))
Cipher c = Cipher getInstance(Algorithm)
c init(Cipher DECRYPT_MODE deskey)
byte[] clearByte = c doFinal(input)
// if (debug )
// {
// System out println ( 解密后的二进串 +byte hex (clearByte ))
// System out println ( 解密后的字符串 +(new String (clearByte )))
//
// }
return clearByte;
}
public static byte[] decryptData(byte[] input) throws Exception {
return decryptData(input DEFAULT_KEY)
}
/**
* 字节码转换成 进制字符串
*
* @param byte[] b 输入要转换的字节码
* @return String 返回转换后的 进制字符串
*/
public static String byte hex(byte[] bytes) {
StringBuilder hs = new StringBuilder()
for(byte b : bytes)
hs append(String format( % $ X b))
return hs toString()
}
public static byte[] hex byte(String content) {
int l=content length()》 ;
byte[] result=new byte[l];
for(int i= ;il;i++) {
int j=i《 ;
String s=content substring(j j+ )
result[i]=Integer valueOf(s ) byteValue()
}
return result;
}
/**
* 将字节数组转换为base 编码字符串
* @param buffer
* @return
*/
public static String bytesToBase (byte[] buffer) {
//BASE Encoder en=new BASE Encoder()
return Base encode(buffer)
//return encoder encode(buffer)
}
/**
* 将base 编码的字符串解码为字节数组
* @param value
* @return
* @throws IOException
*/
public static byte[] base ToBytes(String value) throws IOException {
//return Base decodeToByteArray(value)
//System out println(decoder decodeBuffer(value))
//return decoder decodeBuffer(value)
return Base decode(value)
}
/**
* 加密给定的字符串
* @param value
* @return 加密后的base 字符串
*/
public static String encryptString(String value) {
return encryptString(value DEFAULT_KEY)
}
/**
* 根据给定的密钥加密字符串
* @param value 待加密的字符串
* @param key 以BASE 形式存在的密钥
* @return 加密后的base 字符串
* @throws IOException
*/
public static String encryptString(String value String key) throws IOException {
return encryptString(value base ToBytes(key))
}
/**
* 根据给定的密钥加密字符串
* @param value 待加密的字符串
* @param key 字节数组形式的密钥
* @return 加密后的base 字符串
*/
public static String encryptString(String value byte[] key) {
try {
byte[] data=https://www.04ip.com/post/value getBytes(VALUE_ENCODING)
data=https://www.04ip.com/post/CryptUtils encryptData(data key)
return bytesToBase (data)

推荐阅读