2023年java 加密解密实用

格式:DOC 上传日期:2023-04-29 14:20:22
2023年java 加密解密实用
时间:2023-04-29 14:20:22     小编:zdfb

在日常学习、工作或生活中,大家总少不了接触作文或者范文吧,通过文章可以把我们那些零零散散的思想,聚集在一块。写范文的时候需要注意什么呢?有哪些格式需要注意呢?下面是小编帮大家整理的优质范文,仅供参考,大家一起来看看吧。

java 加密解密篇一

java很多时候要对秘要进行持久化加密,此时的加密采用md5。采用对称加密的时候就采用des方法了,那么java证书的加密与解密代码是什么呢?下面跟yjbys小编一起来学习一下吧!

加密:t(password)

解密:t(password)

[java]

package ;

import ption;

import ;

import erator;

import key;

import 64;

public class cryptutils {

private static string algorithm = "des";

private static byte[] default_key=new byte[] {-53, 122, -42, -88, -110, -123, -60, -74};

private static string value_encoding="utf-8";

/**

* 生成密钥

*

* @return byte[] 返回生成的密钥

* @throws exception

* 扔出异常.

*/

public static byte[] getsecretkey() throws exception {

keygenerator keygen = tance(algorithm);

secretkey deskey = tekey();

// if (debug ) n ("生成密钥:"+byte2hex (oded

// ()));

return oded();

}

/**

* 将指定的数据根据提供的密钥进行加密

*

* @param input

* 需要加密的数据

* @param key

* 密钥

* @return byte[] 加密后的数据

* @throws exception

*/

public static byte[] encryptdata(byte[] input, byte[] key) throws exception {

secretkey deskey = new keyspec(key, algorithm);

// if (debug )

// {

// n ("加密前的二进串:"+byte2hex (input ));

// n ("加密前的字符串:"+new string (input ));

//

// }

cipher c1 = tance(algorithm);

(t_mode, deskey);

byte[] cipherbyte = l(input);

// if (debug ) n ("加密后的二进串:"+byte2hex (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 keyspec(key, algorithm);

// if (debug ) n ("解密前的信息:"+byte2hex (input ));

cipher c1 = tance(algorithm);

(t_mode, deskey);

byte[] clearbyte = l(input);

// if (debug )

// {

// n ("解密后的二进串:"+byte2hex (clearbyte ));

// n ("解密后的字符串:"+(new string (clearbyte )));

//

// }

return clearbyte;

}

public static byte[] decryptdata(byte[] input) throws exception {

return decryptdata(input, default_key);

}

/**

* 字节码转换成16进制字符串

*

* @param byte[] b 输入要转换的字节码

* @return string 返回转换后的16进制字符串

*/

public static string byte2hex(byte[] bytes) {

stringbuilder hs = new stringbuilder();

for(byte b : bytes)

(("%1$02x", b));

return ng();

}

public static byte[] hex2byte(string content) {

int l=()>>1;

byte[] result=new byte[l];

for(int i=0;i

int j=i<<1;

string s=ing(j, j+2);

result[i]=f(s, 16).bytevalue();

}

return result;

}

/**

* 将字节数组转换为base64编码字符串

* @param buffer

* @return

*/

public static string bytestobase64(byte[] buffer) {

【本文地址:http://www.xuefen.com.cn/zuowen/2754998.html】

全文阅读已结束,如果需要下载本文请点击

下载此文档