文章目錄
常用的加密解密及編碼解碼演算法 http://iamcaihuafeng.blog.sohu.com/144299832.html今天從Google Reader上發現了一篇文章,關於URL編碼,寫得還算不錯,對自己也有一些啟示。順便把常用的加密及編碼演算法總結一下。
1.常用的加密及解密演算法
常規加密md5($pass)
32位小寫:d41d8cd98f00b204e9800998ecf8427e
32位大寫:D41D8CD98F00B204E9800998ECF8427E
16位小寫:8f00b204e9800998
16位大寫:8F00B204E9800998
二次加密md5(md5($pass))
32位小寫:74be16979710d4c4e7c6647856088456
32位大寫:80404D0C6D24E87F650FF7D1985CD762
16位小寫:16ae9f6a80547273
16位大寫:89E12A128CEBC695
md5($pass.$salt) md5($salt.$pass) md5(md5($pass).$salt)
md5(md5($salt).$pass) md5($salt.$pass.$salt) md5($salt.md5($pass))
md5(md5($salt).md5($pass)) md5(md5($pass).md5($salt))
2.md5
Message Digest Algorithm MD5(中文名為訊息摘要演算法第五版)為電腦安全領域廣泛使用的一種散列函數,用以提供訊息的完整性保護。該演算法的檔案號為RFC 1321(R.Rivest,MIT Laboratory for Computer Science and RSA Data Security Inc. April 1992)
在90年代初由MIT Laboratory for Computer Science和RSA Data Security In
c,的Ronald L. Rivest開發出來,經MD2、MD3和MD4發展而來。它的作用是讓大容量資訊在用數位簽章軟體簽署私人密鑰前被"壓縮"成一種保密的格式(就是把一個任意長度的位元組串變換成一定長的大整數)。不管是MD2、MD4還是MD5,它們都需要獲得一個隨機長度的資訊併產生一個128位的資訊摘要。雖然這些演算法的結構或多或少有些相似,但MD2的設計與MD4和MD5完全不同,那是因為MD2是為8位機器做設計最佳化的,而MD4和MD5卻是面向32位的電腦。這三個演算法的描述和c語言原始碼在Internet RFC
1321中有詳細的描述,這是一份最權威的文檔,由Ronald L. Rivest在1992年8月向IETF提交。Via
(PHP 3, PHP 4, PHP 5)
md5 -- Calculate the md5 hash of a string
Description
string md5 ( string str [, bool raw_output] )
Calculates the MD5 hash of str using the RSA Data Security, Inc. MD5 Message-Digest Algorithm, and returns that hash. The hash is a 32-character hexadecimal number. If the optional raw_output is set to TRUE, then the md5 digest is instead returned in raw binary
format with a length of 16.
注: The optional raw_output parameter was added in PHP 5.0.0 and defaults to FALSE
3.md5_file
(PHP 4 >= 4.2.0, PHP 5, PECL hash:1.1-1.3)
md5_file — Calculates the md5 hash of a given file
Description
string md5_file ( string $filename [, bool $raw_output=false ] )
Calculates the MD5 hash of the file specified by the filename parameter using the » RSA Data Security, Inc. MD5 Message-Digest Algorithm, and returns that hash. The hash is a 32-character hexadecimal number.
Parameters
filename
The filename
raw_output
When TRUE, returns the digest in raw binary format with a length of 16. Defaults to FALSE.
Return Values
Returns a string on success, FALSE otherwise.
4.sha1
1 SHA1演算法簡介
安全雜湊演算法(Secure Hash Algorithm)主要適用於數位簽章標準 (DSS)(Digital Signature Standard DSS)裡面定義的數位簽章演算法(Digital Signature Algorithm DSA)。對於長度小於2^64位的訊息,SHA1會產生一個160位的訊息摘要。當接收到訊息的時候,這個訊息摘要可以用來驗證資料的完整性。在傳輸的過程中,資料很可能會發生變化,那麼這時候就會產生不同的訊息摘要。Via
(PHP 4 >= 4.3.0, PHP 5, PECL hash:1.1-1.3)
sha1 — Calculate the sha1 hash of a string
Description
string sha1 ( string $str [, bool $raw_output=false ] )
Calculates the sha1 hash of str using the » US Secure Hash Algorithm 1.
Parameters
str
The input string.
raw_output
If the optional raw_output is set to TRUE, then the sha1 digest is instead returned in raw binary format with a length of 20, otherwise the returned value is a 40-character hexadecimal number. Defaults to FALSE.
Return Values
Returns the sha1 hash as a string.
5.sha1_file
(PHP 4 >= 4.3.0, PHP 5, PECL hash:1.1-1.3)
sha1_file — Calculate the sha1 hash of a file
Description
string sha1_file ( string $filename [, bool $raw_output=false ] )
Calculates the sha1 hash of filename using the » US Secure Hash Algorithm 1, and returns that hash. The hash is a 40-character hexadecimal number.
Parameters
filename
The filename
raw_output
When TRUE, returns the digest in raw binary format with a length of 20. Defaults to FALSE.
Return Values
Returns a string on success, FALSE otherwise.
6.crc32
CRC校正公用程式庫在資料存放區和資料通訊領域,為了保證資料的正確,就不得不採用檢錯的手段。在諸多檢錯手段中,CRC是最著名的一種。CRC的全稱是迴圈冗餘校正,其特點是:檢錯能力極強,開銷小,易於用編碼器及檢測電路實現。從其檢錯能力來看,它所不能發現的錯誤的幾率僅為0.0047%以下。從效能上和開銷上考慮,均遠遠優於同位及算術和校正等方式。因而,在資料存放區和資料通訊領域,CRC無處不在:著名的通訊協議X.25的FCS(幀檢錯序列)採用的是CRC- CCITT,ARJ、LHA等壓縮公用程式軟體採用的是CRC32,磁碟機的讀寫採用了CRC16,通用的映像儲存格式GIF、TIFF等也都用CRC作為檢錯手段。Via
(PHP 4 >= 4.0.1, PHP 5)
crc32 — Calculates the crc32 polynomial of a string
Description
int crc32 ( string $str )
Generates the cyclic redundancy checksum polynomial of 32-bit lengths of the str . This is usually used to validate the integrity of data being transmitted.
Because PHP's integer type is signed, and many crc32 checksums will result in negative integers, you need to use the "%u" formatter of sprintf() or printf() to get the string representation of the unsigned crc32 checksum.
Parameters
str
The data.
Return Values
Returns the crc32 checksum of str as an integer.
6.RSA
RSA演算法是第一個能同時用於加密和數位簽章的演算法,也易於理解和操作。
RSA是被研究得最廣泛的公開金鑰演算法,從提出到現在已近二十年,經曆了各種攻擊的考驗,逐漸為人們接受,普遍認為是目前最優秀的公開金鑰方案之一。RSA的安全性依賴於大數的因子分解,但並沒有從理論上證明破譯RSA的難度與大數分解難度等價。即RSA的重大缺陷是無法從理論上把握它的保密效能如何,而且密碼學界多數人士傾向於因子分解不是 NPC問題。
RSA的缺點主要有:A)產生密鑰很麻煩,受到素數產生技術的限制,因而難以做到一次一密。 B)分組長度太大,為保證安全性,n 至少也要 600 bits以上,使運算代價很高,尤其是速度較慢,較對稱密碼演算法慢幾個數量級;且隨著大數分解技術的發展,這個長度還在增加,不利於資料格式的標準化。目前,SET(Secure Electronic Transaction)協議中要求CA採用2048位元長的密鑰,其他實體使用1024位元的密鑰。
這種演算法1978年就出現了,它是第一個既能用於資料加密也能用於數位簽章的演算法。它易於理解和操作,也很流行。演算法的名字以發明者的名字命名:Ron Rivest, AdiShamir 和Leonard Adleman。
RSA演算法是一種非對稱密碼演算法,所謂非對稱,就是指該演算法需要一對密鑰,使用其中一個加密,則需要用另一個才能解密。 Via
7.對稱式加密演算法
簡介:
對稱式加密演算法對稱式加密演算法是應用較早的密碼編譯演算法,技術成熟。在對稱式加密演算法中,資料發信方將明文(未經處理資料)和加密金鑰一起經過特殊密碼編譯演算法處理後,使其變成複雜的加密密文發送出去。收信方收到密文後,若想解讀原文,則需要使用加密用過的密鑰及相同演算法的逆演算法對密文進行解密,才能使其恢複成可讀明文。在對稱式加密演算法中,使用的密鑰只有一個,發收信雙方都使用這個金鑰組資料進行加密和解密,這就要求解密方事先必須知道加密金鑰。
特點:
對稱式加密演算法的特點是演算法公開、計算量小、加密速度快、加密效率高。
不足之處是,交易雙方都使用同樣鑰匙,安全性得不到保證。此外,每對使用者每次使用對稱式加密演算法時,都需要使用其他人不知道的惟一鑰匙,這會使得發收信雙方所擁有的鑰匙數量成幾何級數增長,密鑰管理成為使用者的負擔。對稱式加密演算法在分布式網路系統上使用較為困難,主要是因為密鑰管理困難,使用成本較高。而與公開祕密金鑰加密演算法比起來,對稱式加密演算法能夠提供加密和認證卻缺乏了簽名功能,使得使用範圍有所縮小。在電腦專網系統中廣泛使用的對稱式加密演算法有DES和IDEA等。美國國家標準局倡導的AES即將作為新標準取代DES。
具體演算法:
3DES演算法,Blowfish演算法,RC5演算法。Via
8.Base64
Base64是網路上最常見的用於傳輸8Bit位元組代碼的編碼方式之一,大家可以查看RFC2045~RFC2049,上面有MIME的詳細規範。
Base64編碼可用於在HTTP環境下傳遞較長的標識資訊。例如,在Java Persistence系統Hibernate中,就採用了Base64來將一個較長的唯一識別碼(一般為128-bit的UUID)編碼為一個字串,用作HTTP表單和HTTP GET URL中的參數。在其他應用程式中,也常常需要把位元據編碼為適合放在URL(包括隱藏表單域)中的形式。此時,採用Base64編碼不僅比較簡短,同時也具有不可讀性,即所編碼的資料不會被人用肉眼所直接看到。
然而,標準的Base64並不適合直接放在URL裡傳輸,因為URL編碼器會把標準Base64中的“/”和“+”字元變為形如“%XX”的形式,而這些“%”號在存入資料庫時還需要再進行轉換,因為ANSI SQL中已將“%”號用作萬用字元。
為解決此問題,可採用一種用於URL的改進Base64編碼,它不在末尾填充'='號,並將標準Base64中的“+”和“/”分別改成了“*”和“-”,這樣就免去了在URL編解碼和資料庫儲存時所要作的轉換,避免了編碼資訊長度在此過程中的增加,並統一了資料庫、表單等處物件識別碼的格式。Via
9.Urlencode
Percent-encoding, also known as URL encoding, is a mechanism for encoding information in a Uniform Resource Identifier (URI) under certain circumstances. Although it is known as URL encoding it is, in fact, used more generally within the main Uniform Resource
Identifier (URI) set, which includes both Uniform Resource Locator (URL) and Uniform Resource Name (URN). As such it is also used in the preparation of data of the "application/x-www-form-urlencoded" media type, as is often used in email messages and the submission
of HTML form data in HTTP requests. Via