安卓开发常用工具类Word文档下载推荐.docx

上传人:b****3 文档编号:15721767 上传时间:2022-11-15 格式:DOCX 页数:175 大小:84.98KB
下载 相关 举报
安卓开发常用工具类Word文档下载推荐.docx_第1页
第1页 / 共175页
安卓开发常用工具类Word文档下载推荐.docx_第2页
第2页 / 共175页
安卓开发常用工具类Word文档下载推荐.docx_第3页
第3页 / 共175页
安卓开发常用工具类Word文档下载推荐.docx_第4页
第4页 / 共175页
安卓开发常用工具类Word文档下载推荐.docx_第5页
第5页 / 共175页
点击查看更多>>
下载资源
资源描述

安卓开发常用工具类Word文档下载推荐.docx

《安卓开发常用工具类Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《安卓开发常用工具类Word文档下载推荐.docx(175页珍藏版)》请在冰豆网上搜索。

安卓开发常用工具类Word文档下载推荐.docx

StringSHAEncode(Stringmessage){

StringresultString=null;

try{

MessageDigestmd=MessageDigest.getInstance("

SHA"

);

md.update(message.getBytes());

resultString=bytes2HexString(md.digest());

}catch(Exceptione){

e.printStackTrace();

}

returnresultString;

}

//将Bytes数据转换成16进制字符串格式

将Bytes数据转换成16进制字符串格式

Stringbytes2HexString(byte[]bts){

Stringdes="

"

;

Stringtmp=null;

for(inti=0;

i<

bts.length;

i++){

tmp=(Integer.toHexString(bts[i]&

0xFF));

if(tmp.length()==1){

des+="

0"

}

des+=tmp;

returndes;

}

AES对称加密解密类

publicclassAes{

///**算法/模式/填充**/

privatestaticfinalStringCipherMode="

AES/ECB/PKCS5Padding"

///**创建密钥**/

privatestaticSecretKeySpeccreateKey(Stringpassword){

byte[]data=null;

if(password==null){

password="

StringBuffersb=newStringBuffer(32);

sb.append(password);

while(sb.length()<

32){

sb.append("

if(sb.length()>

sb.setLength(32);

data=sb.toString().getBytes("

UTF-8"

}catch(UnsupportedEncodingExceptione){

returnnewSecretKeySpec(data,"

AES"

加密字节数据

byte[]encrypt(byte[]content,Stringpassword)

publicstaticbyte[]encrypt(byte[]content,Stringpassword){

SecretKeySpeckey=createKey(password);

System.out.println(key);

Ciphercipher=Cipher.getInstance(CipherMode);

cipher.init(Cipher.ENCRYPT_MODE,key);

byte[]result=cipher.doFinal(content);

returnresult;

returnnull;

加密(结果为16进制字符串)

Stringencrypt(Stringcontent,Stringpassword)

///**加密(结果为16进制字符串)**/

publicstaticStringencrypt(Stringcontent,Stringpassword){

data=content.getBytes("

data=encrypt(data,password);

Stringresult=byte2hex(data);

returnresult;

解密字节数组

byte[]decrypt(byte[]content,Stringpassword)

///**解密字节数组**/

publicstaticbyte[]decrypt(byte[]content,Stringpassword){

cipher.init(Cipher.DECRYPT_MODE,key);

解密16进制的字符串为字符串

Stringdecrypt(Stringcontent,Stringpassword)

///**解密16进制的字符串为字符串**/

publicstaticStringdecrypt(Stringcontent,Stringpassword){

data=hex2byte(content);

data=decrypt(data,password);

if(data==null)

returnnull;

Stringresult=null;

result=newString(data,"

字节数组转成16进制字符串

Stringbyte2hex(byte[]b)

///**字节数组转成16进制字符串**/

publicstaticStringbyte2hex(byte[]b){//一个字节的数,

StringBuffersb=newStringBuffer(b.length*2);

Stringtmp="

for(intn=0;

n<

b.length;

n++){

//整数转成十六进制表示

tmp=(java.lang.Integer.toHexString(b[n]&

0XFF));

sb.append("

sb.append(tmp);

returnsb.toString().toUpperCase();

//转成大写

///**将hex字符串转换成字节数组**/

privatestaticbyte[]hex2byte(StringinputString){

if(inputString==null||inputString.length()<

2){

returnnewbyte[0];

inputString=inputString.toLowerCase();

intl=inputString.length()/2;

byte[]result=newbyte[l];

l;

++i){

Stringtmp=inputString.substring(2*i,2*i+2);

result[i]=(byte)(Integer.parseInt(tmp,16)&

0xFF);

Des加密

packagecom.itheima.androidutils.utils.encrypt.des;

importjava.io.IOException;

importjava.security.SecureRandom;

importjavax.crypto.SecretKey;

importjavax.crypto.SecretKeyFactory;

importjavax.crypto.spec.DESKeySpec;

importDecoder.BASE64Decoder;

importDecoder.BASE64Encoder;

publicclassDes{

privatefinalstaticStringDES="

DES"

publicstaticvoidmain(String[]args)throwsException{

Stringdata="

强大的黑马51期"

Stringkey="

wang!

@#$%"

System.err.println(encrypt(data,key));

//System.err.println(decrypt(encrypt(data,key),key));

根据键值进行加密Stringencrypt(Stringdata,Stringkey)

/**

*Description根据键

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 农林牧渔 > 林学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1