OpenSSL初学总结


http://blog.csdn.net/lixiaomin_235/article/details/3637977


最近一段时间看些关于OpenSSL加密函数的使用,现将一些使用总结如下:

1、OpenSSL简介:

openssl是一个功能丰富且自包含的开源安全工具箱。它提供的主要功能有:SSL协议实现(包括SSLv2SSLv3TLSv1)、大量软算法(对称/非对称/摘要)、大数运算、非对称算法密钥生成、ASN.1编解码库、证书请求(PKCS10)编解码、数字证书编解码、CRL编解码、OCSP协议、数字证书验证、PKCS7标准实现和PKCS12个人数字证书格式实现等功能。

openssl采用C语言作为开发语言,这使得它具有优秀的跨平台性能。openssl支持LinuxUNIXwindowsMac等平台。

2、OpenSSL加密算法――AES加密流程:

(1)加密

设置加密密钥:AES_set_encrypt_key

int AES_set_encrypt_key(const unsigned char *userKey, const int bits,

AES_KEY *key)

其中只需要填写一个 userKey这个buff,然后bits和这个buff匹配,如果这个buff是16字节的bits则是128,如果这个buff的字节数是32的,则bits是256,key是一个值结果,先不用管它


加密:AES_encrypt

(2)减密

设置减密密钥:AES_set_decrypt_key

int AES_set_decrypt_key(const unsigned char *userKey, const int bits,

AES_KEY *key);

其中 userKey和前面的 AES_set_encrypt_key填写的userKey是一模一样的,bits也和前面的一样,key是一个值结果,先不用管它




减密:AES_decrypt

3、OpenSSL加密算法――RSA加密流程:

(1)公钥加密,私钥减密

初始化RSA加密:RSA_new

设置密钥:RSA_general_key(自动生成)和BN_bin2bn(加载)

验证密钥:RSA_check_key

指定Padding和要加密的长度(通常选择RSA_PKCS1_PADDING

加密:RSA_public_encrypt

减密:RSA_private_decrypt

释放RSARSA_free

(2)私钥加密,公钥减密

初始化RSA加密:RSA_new

设置密钥:RSA_general_key(自动生成)和BN_bin2bn(加载)

验证密钥:RSA_check_key

指定Padding和要加密的长度(通常选择RSA_PKCS1_PADDING

加密:RSA_private_encrypt

减密:RSA_public_decrypt

释放RSARSA_free



编译完成以后,使用命令行,对两种加密解密流程做一下测试验证,其中hello.txt 中的内容可以随意填写...


私钥加密,公钥解密(确切的说是数字签名,公钥验证,主要目的不是为了让传送文件加密,

而是为了保证发送者不会被冒充)



//生成密钥文件,包括公钥和私钥

openssl genrsa -out apri.pem 1024


//生成公钥文件,存放在apub.pem这个文件中

openssl rsa -pubout -in apri.pem -out apub.pem


//通过自己的私钥文件给要发送的文件hello.txt 签名加密,生成的是signature.out

//这个文件,然后就是将signature.out 这个文件传递给接收方

openssl rsautl -sign -inkey apri.pem -in hello.txt -out signature.out


//接收方接收到signature.out,同时知道 apub.pem 这个公钥文件,通过apub.pem 这个

//公钥文件来进行验证解密,解密出来的文件 hello.out 这个文件和hello.txt 是一样的

openssl rsautl -verify -pubin -inkey apub.pem -in signature.out -out hello.out



公钥加密,私钥解密(主要是目的是为了保证传送文件即便被截取,截取者也不能得到加密文件

的内容)


//生成密钥文件,包括公钥和私钥

openssl genrsa -out apri.pem 1024


//生成公钥文件,存放在apub.pem这个文件中

openssl rsa -pubout -in apri.pem -out apub.pem



//发送方用接收方发布的公钥文件 apub.pem 加密要传送的文件 hello.txt ,生成hello.en加密

//文件以后发送给接收方

openssl rsautl -encrypt -in hello.txt -inkey apub.pem -pubin -out hello.en


//接收方接收到发送方使用自己的公钥文件 apub.pem 加密的加密文件 hello.en后使用自己的

//私钥文件apri.pem 进行解密,生成解密文件hello.de,hello.de 这个文件的内容和 hello.txt

//是一样的

openssl rsautl -decrypt -in hello.en -inkey apri.pem -out hello.de



需要注意的是,使用公钥加密,私钥解密的方式的时候,由于要加入随机数,虽然使用的是一样的

公钥文件,但是生成的密文是不同的


openssl rsautl -encrypt -in hello.txt -inkey apub.pem -pubin -out hello.en1


其中hello.en1 和 hello.en 是不同的


但是使用

openssl rsautl -decrypt -in hello.en1 -inkey apri.pem -out hello.de


解密出来的hello.de 却总是相同,这点和私钥加密,公钥解密生成的密文文件不一致,也增加了破解的

难度。








几个有用的链接,先记录一下,以备将来研究


http://co63oc.blog.51cto.com/904636/625459

http://www.cnblogs.com/aLittleBitCool/archive/2011/09/22/2185418.html

http://hi.baidu.com/chijr/item/3cda1ec32893d052ad00efcb

http://blog.csdn.net/turui/article/details/2048582


http://www.cnblogs.com/ymy124/archive/2012/04/04/2432432.html




你可能感兴趣的:(linux,加密,windows,工具箱)