轉載保留原文地址:hao54216的博客
前言:
常常聽小夥伴們提起MD5:
瞭解SHA:
public class SHA1Util {
private static final boolean hexcase = false;
private static final String b64pad = "=";
private static final int chrsz = 8;
// 得到字符串SHA-1值的方法
public static String hex_sha1(String s) {
s = (s == null) ? "" : s;
return binb2hex(core_sha1(str2binb(s), s.length() * chrsz));
}
public static String b64_hmac_sha1(String key, String data) {
return binb2b64(core_hmac_sha1(key, data));
}
public static String b64_sha1(String s) {
s = (s == null) ? "" : s;
return binb2b64(core_sha1(str2binb(s), s.length() * chrsz));
}
private static String binb2b64(int[] binarray) {
String tab = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/";
String str = "";
binarray = strechbinarray(binarray, binarray.length * 4);
for (int i = 0; i < binarray.length * 4; i += 3) {
int triplet = (((binarray[i >> 2] >> 8 * (3 - i % 4)) & 0xff) << 16)
| (((binarray[i + 1 >> 2] >> 8 * (3 - (i + 1) % 4)) & 0xff) << 8)
| ((binarray[i + 2 >> 2] >> 8 * (3 - (i + 2) % 4)) & 0xff);
for (int j = 0; j < 4; j++) {
if (i * 8 + j * 6 > binarray.length * 32) {
str += b64pad;
} else {
str += tab.charAt((triplet >> 6 * (3 - j)) & 0x3f);
}
}
}
return cleanb64str(str);
}
private static String binb2hex(int[] binarray) {
String hex_tab = hexcase ? "0123456789abcdef" : "0123456789abcdef";
String str = "";
for (int i = 0; i < binarray.length * 4; i++) {
char a = (char) hex_tab.charAt((binarray[i >> 2] >> ((3 - i % 4) * 8 + 4)) & 0xf);
char b = (char) hex_tab.charAt((binarray[i >> 2] >> ((3 - i % 4) * 8)) & 0xf);
str += (new Character(a).toString() + new Character(b).toString());
}
return str;
}
private static String binb2str(int[] bin) {
String str = "";
int mask = (1 << chrsz) - 1;
for (int i = 0; i < bin.length * 32; i += chrsz) {
str += (char) ((bin[i >> 5] >>> (24 - i % 32)) & mask);
}
return str;
}
private static int bit_rol(int num, int cnt) {
return (num << cnt) | (num >>> (32 - cnt));
}
private static String cleanb64str(String str) {
str = (str == null) ? "" : str;
int len = str.length();
if (len <= 1) {
return str;
}
char trailchar = str.charAt(len - 1);
String trailstr = "";
for (int i = len - 1; i >= 0 && str.charAt(i) == trailchar; i--) {
trailstr += str.charAt(i);
}
return str.substring(0, str.indexOf(trailstr));
}
private static int[] complete216(int[] oldbin) {
if (oldbin.length >= 16) {
return oldbin;
}
int[] newbin = new int[16 - oldbin.length];
for (int i = 0; i < newbin.length; newbin[i] = 0, i++)
;
return concat(oldbin, newbin);
}
private static int[] concat(int[] oldbin, int[] newbin) {
int[] retval = new int[oldbin.length + newbin.length];
for (int i = 0; i < (oldbin.length + newbin.length); i++) {
if (i < oldbin.length) {
retval[i] = oldbin[i];
} else {
retval[i] = newbin[i - oldbin.length];
}
}
return retval;
}
private static int[] core_hmac_sha1(String key, String data) {
key = (key == null) ? "" : key;
data = (data == null) ? "" : data;
int[] bkey = complete216(str2binb(key));
if (bkey.length > 16) {
bkey = core_sha1(bkey, key.length() * chrsz);
}
int[] ipad = new int[16];
int[] opad = new int[16];
for (int i = 0; i < 16; ipad[i] = 0, opad[i] = 0, i++)
;
for (int i = 0; i < 16; i++) {
ipad[i] = bkey[i] ^ 0x36363636;
opad[i] = bkey[i] ^ 0x5c5c5c5c;
}
int[] hash = core_sha1(concat(ipad, str2binb(data)), 512 + data.length() * chrsz);
return core_sha1(concat(opad, hash), 512 + 160);
}
private static int[] core_sha1(int[] x, int len) {
int size = (len >> 5);
x = strechbinarray(x, size);
x[len >> 5] |= 0x80 << (24 - len % 32);
size = ((len + 64 >> 9) << 4) + 15;
x = strechbinarray(x, size);
x[((len + 64 >> 9) << 4) + 15] = len;
int[] w = new int[80];
int a = 1732584193;
int b = -271733879;
int c = -1732584194;
int d = 271733878;
int e = -1009589776;
for (int i = 0; i < x.length; i += 16) {
int olda = a;
int oldb = b;
int oldc = c;
int oldd = d;
int olde = e;
for (int j = 0; j < 80; j++) {
if (j < 16) {
w[j] = x[i + j];
} else {
w[j] = rol(w[j - 3] ^ w[j - 8] ^ w[j - 14] ^ w[j - 16], 1);
}
int t = safe_add(safe_add(rol(a, 5), sha1_ft(j, b, c, d)), safe_add(safe_add(e, w[j]), sha1_kt(j)));
e = d;
d = c;
c = rol(b, 30);
b = a;
a = t;
}
a = safe_add(a, olda);
b = safe_add(b, oldb);
c = safe_add(c, oldc);
d = safe_add(d, oldd);
e = safe_add(e, olde);
}
int[] retval = new int[5];
retval[0] = a;
retval[1] = b;
retval[2] = c;
retval[3] = d;
retval[4] = e;
return retval;
}
private static void dotest() {
String key = "key";
String data = "data";
System.out.println("hex_sha1(" + data + ")=" + hex_sha1(data));
System.out.println("b64_sha1(" + data + ")=" + b64_sha1(data));
System.out.println("str_sha1(" + data + ")=" + str_sha1(data));
System.out.println("hex_hmac_sha1(" + key + "," + data + ")=" + hex_hmac_sha1(key, data));
System.out.println("b64_hmac_sha1(" + key + "," + data + ")=" + b64_hmac_sha1(key, data));
System.out.println("str_hmac_sha1(" + key + "," + data + ")=" + str_hmac_sha1(key, data));
}
public static String hex_hmac_sha1(String key, String data) {
return binb2hex(core_hmac_sha1(key, data));
}
private static int rol(int num, int cnt) {
return (num << cnt) | (num >>> (32 - cnt));
}
private static int safe_add(int x, int y) {
int lsw = (int) (x & 0xffff) + (int) (y & 0xffff);
int msw = (x >> 16) + (y >> 16) + (lsw >> 16);
return (msw << 16) | (lsw & 0xffff);
}
private static int sha1_ft(int t, int b, int c, int d) {
if (t < 20)
return (b & c) | ((~b) & d);
if (t < 40)
return b ^ c ^ d;
if (t < 60)
return (b & c) | (b & d) | (c & d);
return b ^ c ^ d;
}
private static int sha1_kt(int t) {
return (t < 20) ? 1518500249 : (t < 40) ? 1859775393 : (t < 60) ? -1894007588 : -899497514;
}
private static boolean sha1_vm_test() {
return hexcase ? hex_sha1("abc").equals("a9993e364706816aba3e25717850c26c9cd0d89d") : hex_sha1("abc").equals(
"a9993e364706816aba3e25717850c26c9cd0d89d");
}
public static String str_hmac_sha1(String key, String data) {
return binb2str(core_hmac_sha1(key, data));
}
public static String str_sha1(String s) {
s = (s == null) ? "" : s;
return binb2str(core_sha1(str2binb(s), s.length() * chrsz));
}
private static int[] str2binb(String str) {
str = (str == null) ? "" : str;
int[] tmp = new int[str.length() * chrsz];
int mask = (1 << chrsz) - 1;
for (int i = 0; i < str.length() * chrsz; i += chrsz) {
tmp[i >> 5] |= ((int) (str.charAt(i / chrsz)) & mask) << (24 - i % 32);
}
int len = 0;
for (int i = 0; i < tmp.length && tmp[i] != 0; i++, len++)
;
int[] bin = new int[len];
for (int i = 0; i < len; i++) {
bin[i] = tmp[i];
}
return bin;
}
private static int[] strechbinarray(int[] oldbin, int size) {
int currlen = oldbin.length;
if (currlen >= size + 1) {
return oldbin;
}
int[] newbin = new int[size + 1];
for (int i = 0; i < size; newbin[i] = 0, i++)
;
for (int i = 0; i < currlen; i++) {
newbin[i] = oldbin[i];
}
return newbin;
}
public static void main(String args[]) {
System.out.println("admin的SHA1的值爲:" + hex_sha1("admin") + ",length=" + hex_sha1("admin").length());
}
}
瞭解AES:
瞭解DES:
瞭解RSA:
瞭解BASE64:
<span style="font-weight: normal;">//模板類模板類寫好了再按思路寫個實現就可以了
publicinterfaceBase64{
/**
*根據傳進來的字符的字節碼,查詢base64碼錶的索引,並返回所查到的索引
*
*@paramb一個編碼後的字節碼
*@return返回base64碼錶的索引
*/
publicabstractbytebaseIndex(byteb);
/**
*解碼的方法
*傳進來的是編碼後的base64字符的字節碼
*解析時是4個一組進行解析
*@paramb編碼後的字符的字節碼數組
*@return返回原來的字符串
*/
publicabstractStringbackEncode(byte[]b);
/**
*解碼
*將4個字節碼中的第1個的後6位(00xxxxxx)和第2個
*字節的前4位的後2位(00yy0000)
*還原爲原來的字節碼(xxxxxxyy)
*
*@paramfirst4個字節碼中的第1個
*@paramsecond4個字節碼中的第2個
*@return原來的字符的字節碼
*/publicabstractbytebackFirst(bytefirst,bytesecond);
/**
*解碼
*將4個字節碼中的第2個的後4位(0000xxxx)和第3個
*字節的前6位的後4位(00yyyy00)
*還原爲原來的字節碼(xxxxyyyy)
*@paramsecond4個字節碼中的第2個
*@paramthird4個字節碼中的第3個
*@return原來的字符的字節碼
*/
publicabstractbytebackSecond(bytesecond,bytethird);
/**
*解碼
*將4個字節碼中的第3個的後2位(000000xx)和第4個
*字節的後6位(00yyyyyy)
*還原爲原來的字節碼(xxyyyyyy)
*@paramthird傳進來的第3個字符
*@paramfourth傳進來的第4個字符
*@return原來的字符的字節碼
*/publicabstractbytebackThird(bytethird,bytefourth);
/**
*解碼
*將編碼後的字符串數組的最後2個字節碼還原爲原來的字節碼
*假如數組末尾剩下2個字節:
*將倒數第2個字節的前後6位(00xxxxxx)
*和倒數第一個字節的後2位(000000yy)
*還原爲原來的編碼(xxxxxxyy)
*假如數組末尾剩下3個字節:
*將倒數第2個字節的前後4位(0000xxxx)
*和倒數第一個字節的後4位(0000yyyy)
*還原爲原來的編碼(xxxxyyyy)
*@paramlast_b倒數第2個字節
*@paramnext_b倒數第1個字節
*@parammove_l倒數第2個字節移動位數的參數
*@parammove_b倒數第1個字節移動位數的參數
*@return原來的字符的字節碼
*/
publicbytebackLastOne(bytelast_b,bytenext_b,intmove_l,intmove_b);
/**
*編碼
*將傳進來的字符編碼爲base64,返回一個base64的字符串
*編碼時3個字節一組進行編碼,傳進來的是要進行編碼的字符串數組
*@paramb要進行編碼的字符串數組
*@return編碼後的字符串
*/
publicabstractStringencode(byte[]b);
/**
*假如字符長度%3!=0,使用此方法編碼末尾字符
*假如b=xxxxyyyy
*假如末尾字節個數等於1:
*將這個字節的前6位作爲一個字節(00xxxxyy)
*將這個字節的後6位作爲一個字節(00xxyyyy)
*假如末尾字節個數等於2:
*將這個字節的後6位作爲一個字節(00xxyyyy)
*@paramb末尾的字符的字節碼
*@parammove末尾的字符的字節碼要移動的位數的參數
*@return編碼後的字節碼
*/
publicabstractbytelastOneByte(byteb,intmove);
/**
*編碼
*假如b=xxxxyyyy
*將第1個字節的前6位編碼爲base64
*將3個字節中的第1個子節碼轉爲(00xxxxyy)
*@paramb3個字節中的第1個字節
*@return編碼後的字節碼
*/
publicabstractbytefirstByte(byteb);
/**
*編碼
*假如last_b=xxxxyyyynext_b=kkkkffff
*將3個字節中的第1個字節的最後2位(000000yy)
*和第2個字節的前4位(kkkk0000)編碼爲(00yykkkk)
*
*@paramlast_b3個字節中的第1個字節
*@paramnext_b3個字節中的第2個字節
*@return編碼後的字節碼
*/
publicabstractbytesecondByte(bytelast_b,bytenext_b);
/**
*編碼
*假如last_b=xxxxyyyynext_b=kkkkffff
*將3個字節中的第2個字節的最後4位(0000yyyy)
*和第4個字節的前2位(kk000000)編碼爲(00yyyykk)
*
*
*@paramlast_b3個字節中的第2個字節
*@paramnext_b3個字節中的第3個字節
*@return編碼後的字節碼
*/
publicabstractbytethirdByte(bytelast_b,bytenext_b);
/**
*編碼
*假如b=xxxxyyyy
*將3個字節中的第3個字節的最後6位(00xxyyyy)
*轉碼爲(00xxyyyy)
*@paramb3個字節中的第3個字節
*@return編碼後的字節碼
*/
publicabstractbytefourthByte(byteb);
}</span>
======================以下是實現類代碼=====================華麗的分界線======<span style="font-weight: normal;">importjava.util.Enumeration;
importjava.util.Vector;
publicclassMyBase64EncoderimplementsBase64{
/**
*base64碼錶
*/
privatestaticfinalbytebase[]={0x41,0x42,0x43,0x44,0x45,0x46,
0x47,0x48,0x49,0x4a,0x4b,0x4c,0x4d,0x4e,0x4f,0x50,0x51,
0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5a,0x61,0x62,
0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6a,0x6b,0x6c,0x6d,
0x6e,0x6f,0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x78,
0x79,0x7a,0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,
0x39,0x2b,0x2f};</span>
<span style="font-weight: normal;">@Override
publicbytebaseIndex(byteb){
for(inti=0;i<base.length;i++){
if(base[i]==b){
return(byte)i;
}
}
return-1;
}</span>
<span style="font-weight: normal;">@Override
publicStringbackEncode(byte[]b){
StringBuffersb=newStringBuffer();
Vector<Byte>list=newVector<Byte>();
intreal_len=b.length;
intlen=real_len-2;
intmore_len=len&3;
intuse_len=len-more_len;</span>
<span style="font-weight: normal;">for(inti=0;i<use_len;i+=4){
list.add(backFirst(baseIndex(b[i]),baseIndex(b[i+1])));
list.add(backSecond(baseIndex(b[i+1]),baseIndex(b[i+2])));
list.add(backThird(baseIndex(b[i+2]),baseIndex(b[i+3])));
}
Enumeratione=list.elements();
bytebytes[]=newbyte[list.size()];
intk=-1;
while(e.hasMoreElements()){
bytes[++k]=(Byte)e.nextElement();
}
sb.append(newString(bytes));</span>
<span style="font-weight: normal;">if(more_len==2){
byteb_1[]=newbyte[1];
b_1[0]=backLastOne(baseIndex(b[len-2]),baseIndex(b[len-1]),2,6);
sb.append(newString(b_1));
}
if(more_len==3){
byteb_2[]=newbyte[2];
b_2[0]=backFirst(baseIndex(b[len-3]),baseIndex(b[len-2]));
b_2[1]=backLastOne(baseIndex(b[len-2]),baseIndex(b[len-1]),4,4);
sb.append(newString(b_2));
}
returnsb.toString();
}
@Override
publicbytelastOneByte(byteb,intmove){
intr_b=b&0xff;
r_b=r_b<<move;
r_b=r_b>>>2;
return(byte)(r_b&0x3f);
}
@Override
publicbytebackLastOne(bytelast_b,bytenext_b,intmove_l,intmove_b){
intr_l=last_b&0xff;
intr_n=next_b&0xff;
r_l=r_l<<move_l;
r_n=r_n<<move_b;
r_n=r_n>>>move_b;
return(byte)((r_l|r_n)&0xff);
}
@Override
publicbytebackFirst(bytefirst,bytesecond){
intr_f=first&0xff;
intr_s=second&0xff;
r_f=r_f<<2;
r_s=r_s>>>4;
return(byte)((r_f|r_s)&0xff);
}</span>
<span style="font-weight: normal;">@Override
publicbytebackSecond(bytesecond,bytethird){
intr_s=second&0xff;
intr_t=third&0xff;
r_s=r_s<<4;
r_t=r_t>>>2;
return(byte)((r_s|r_t)&0xff);
}</span>
<span style="font-weight: normal;">@Override
publicbytebackThird(bytethird,bytefourth){
intr_t=third&0xff;
intr_f=fourth&0xff;
r_t=r_t<<6;
return(byte)((r_t|r_f)&0xff);
}</span>
<span style="font-weight: normal;">@Override
publicStringencode(byte[]b){
StringBuffersb=newStringBuffer();
intlen=b.length;
intmore_len=len%3;
intuse_len=len-more_len;
byte[]bytes=newbyte[4];
for(inti=0;i<use_len;i+=3){
bytes[0]=base[firstByte(b[i])];
bytes[1]=base[secondByte(b[i],b[i+1])];
bytes[2]=base[thirdByte(b[i+1],b[i+2])];
bytes[3]=base[fourthByte(b[i+2])];
sb.append(newString(bytes));
}
if(more_len==1){
byteb_2[]=newbyte[2];
b_2[0]=base[firstByte(b[len-1])];
b_2[1]=base[lastOneByte(b[len-1],6)];
sb.append(newString(b_2));
returnsb.append("==").toString();
}elseif(more_len==2){
byteb_3[]=newbyte[3];
b_3[0]=base[firstByte(b[len-2])];
b_3[1]=base[secondByte(b[len-2],b[len-1])];
b_3[2]=base[lastOneByte(b[len-1],4)];
sb.append(newString(b_3));
returnsb.append("=").toString();
}
returnsb.toString();
}</span>
<span style="font-weight: normal;">@Override
publicbytefirstByte(byteb){
//00000000000000000000000001010011
//01010011
intr_f=b&0xff;
r_f=r_f>>>2;
return(byte)(r_f&0x3f);
}</span>
<span style="font-weight: normal;">@Override
publicbytesecondByte(bytelast_b,bytenext_b){
intr_l=last_b&0xff;
intr_n=next_b&0xff;
r_l=r_l<<6;
r_l=r_l>>>2;
r_n=r_n>>>4;
return(byte)((r_l|r_n)&0x3f);
}</span>
<span style="font-weight: normal;">@Override
publicbytethirdByte(bytelast_b,bytenext_b){
intr_l=last_b&0xff;
intr_n=next_b&0xff;
r_l=r_l<<4;
r_l=r_l>>>2;
r_n=r_n>>>6;
return(byte)((r_l|r_n)&0x3f);
}</span>
<span style="font-weight: normal;">@Override
publicbytefourthByte(byteb){
intr_b=b&0xff;
r_b=r_b<<2;
r_b=r_b>>>2;
return(byte)(r_b&0x3f);
}
}</span>
更多語言實現-----點擊打開鏈接Android加密策略---結束語
3DES(Triple DES):是基於DES,對一塊數據用三個不同的密鑰進行三次加密,強度更高;
RC2和 RC4:用變長密鑰對大量數據進行加密,比 DES 快;
IDEA(International Data Encryption Algorithm)國際數據加密算法:使用 128 位密鑰提供非常強的安全性;
最後感謝您的閱讀~