常用的加密算法---数字摘要
數(shù)字摘要:
數(shù)字摘要也稱(chēng)為消息摘要,它是一個(gè)唯一對(duì)應(yīng)一個(gè)消息或文本的固定長(zhǎng)度的值,它是一個(gè)單向
Hash函數(shù)對(duì)消息進(jìn)行計(jì)算產(chǎn)生的。
摘要生成的過(guò)程:待摘要串----->?Hash函數(shù)----->?摘要
消息摘要的特點(diǎn):
1.無(wú)論輸入的消息多長(zhǎng),計(jì)算出來(lái)的消息摘要的長(zhǎng)度是固定的。例如:MD5 的為128個(gè)比特位,SHA-1的為
160個(gè)比特位;
2.一般只要輸入的消息不同,對(duì)其產(chǎn)生的摘要消息也是不相同的,相同的輸入必然會(huì)產(chǎn)生相同的摘要消息;
3.由于消息摘要并不包含原文的完整信息,因此只能進(jìn)行正向的消息摘要,而無(wú)法從摘要中恢復(fù)出原來(lái)的消息
甚至根本不可能找到任何與原信息相關(guān)的消息。
1.MD5:
基于java的MD5算法的使用:
/**
?*?實(shí)現(xiàn)MD5的加密
?*?
?*?@param?con
?* ? 需要加密的字符串
?*?@return
?*?@throws?Exception
?*/
?private?static?byte[]?testMD5(String?con)?throws?Exception?{
? ? ?MessageDigest?md5?=?MessageDigest.getInstance("MD5");
? ? ?byte[]?bytes?=?md5.digest(con.getBytes("utf-8"));
? ? ?return?bytes;
?}
2.SHA(摘要的信息長(zhǎng)度160位 ?最安全的散列算法之一):
?/**
? *SHA?散列安全算法
? *?
? *?@param?con?
? *?????????????待加密的字符串
? *?@return
? *?@throws?Exception
? */
? private?static?byte[]?tstSHA1(String?con)?throws?Exception?{
? ? ?MessageDigest?sha?=?MessageDigest.getInstance("SHA-1");
? ? ?byte[]?bytes?=?sha.digest(con.getBytes("utf-8"));
? ? ?return?bytes;
?}
3.十六進(jìn)制編碼:
?/**
? *?16進(jìn)制加密
? *?
? *?@param?bytes
? *?@return
? */
?private?static?String?bytes2hex(byte[]?bytes)?{
?? StringBuilder?hex?=?new?StringBuilder();
? ?for?(int?i?=?0;?i?<?bytes.length;?i++)?{
? ? ? byte?b?=?bytes[i];
? ? ? boolean?negtive?=?false;
? ? ? if?(b?<?0)?{
? ? ? ? negtive?=?true;
? ? ? }
? ? ? int?inte?=?Math.abs(b);
? ? ? if?(negtive)
? ? ? inte?=?inte?|?0x80;
? ? ? String?temp?=?Integer.toHexString(inte?&?0xFF);
? ? ? if?(temp.length()?==?1)?{
? ? ? ? hex.append("0");
? ? ? }
? ? ? hex.append(temp.toLowerCase());
? ? ? }
? ? ? return?hex.toString();
? ?}
? /**
? ?*?16進(jìn)制解密
? ?*?
? ?*?@param?hex
? ?*?@return
? ?*/
?private?static?byte[]?hex2bytes(String?hex)?{
? ?byte[]?bytes?=?new?byte[hex.length()?/?2];
? ?for?(int?i?=?0;?i?<?hex.length();?i?=?i?+?2)?{
? ? ? String?subStr?=?hex.substring(i,?i?+?2);
? ? ? boolean?negative?=?false;
? ? ? int?inte?=?Integer.parseInt(subStr,?16);
? ? ? if?(inte?>?127)?{
? ? ? ? negative?=?true;
? ? ? }
? ? ? if?(inte?==?128)?{
? ? ? ? inte?=?-128;
? ? ? }?else?if?(negative)?{
? ? ? ? inte?=?0?-?(inte?&?0x7F);
? ? ? }
? ? ? byte?b?=?(byte)?inte;
? ? ? bytes[i?/?2]?=?b;
? ? ? }
? ? ? return?bytes;
? }
4.Base64編碼:
?/**
? *?base64?編碼
? *?@param?base64
? *?@return
? *?@throws?IOException
? */
?private?static?byte[]?base642byte(String?base64)?throws?IOException?{
? ? BASE64Decoder?bs?=?new?BASE64Decoder();
? ? return?bs.decodeBuffer(base64);?
? }
?/**
? *?base64?解碼
? *?@param?bytes
? *?@return
? */
?private?static?String?byte2base64(byte[]?bytes)?{
? ? BASE64Encoder?bse?=?new?BASE64Encoder();
? ? return?bse.encode(bytes);
?}
??隨著數(shù)據(jù)化時(shí)代的到來(lái),信息的安全性越來(lái)越成為我們關(guān)注的問(wèn)題,本期博客寫(xiě)了一些基礎(chǔ)的加密算法,希望對(duì)大家有用。其他的加密算法我也會(huì)在后期的書(shū)寫(xiě)中逐漸補(bǔ)上。
轉(zhuǎn)載于:https://blog.51cto.com/wang963825/1734883
總結(jié)
以上是生活随笔為你收集整理的常用的加密算法---数字摘要的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 跳槽9招让你“空降”任何企业都能成功
- 下一篇: CountDownLatch应用实战