(转载)常用的加密解密及编码解码算法

常用的加密解密及编码解码算法

 
 
今天从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)主要适用于数字签名标准(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
 
 
posted on 2016-08-10 09:23  flying06  阅读(1012)  评论(0编辑  收藏  举报