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