RSA算法在Python Django中的简单应用

时间:2023-03-08 20:09:38
说明

RSA算法是当今使用最广泛,安全度最高的加密算法。


• RSA算法的安全性理论基础

[引]根据百科介绍,对极大整数做因数分解的难度决定了RSA算法的可靠性。换言之,对一极大整数做因数分解愈困难,RSA算法愈可靠。假如有人找到一种快速因数分解的算法的话,那么用RSA加密的信息的可靠性就肯定会极度下降。但找到这样的算法的可能性是非常小的。今天只有短的RSA钥匙才可能被强力方式解破。到目前为止,世界上还没有任何可靠的攻击RSA算法的方式。只要其钥匙的长度足够长,用RSA加密的信息实际上是不能被解破的。

• RSA算法使用
  1. 通常使用公钥进行加密,使用私钥进行解密。
  2. 如上所说,钥匙的长度尽量设置长一些,在实际应用过程中,通常设置为1024或2048
• 在Python Django中简单应用RSA算法

以下是RSA在Python Django中的简单应用实例

• 安装pycrypto加密算法库

下载pycrypto-2.6.1.tar.gz并解压,使用python setup.py install进行安装即可,执行from Crypto.PublicKey import RSA无报错,则说明安装成功

• 生成秘钥对

1.通过以下代码得到公钥和私钥

from Crypto import Random
from Crypto.PublicKey import RSA random_generator = Random.new().read
rsa = RSA.generate(1024, random_generator)
rsa_private_key = rsa.exportKey()
rsa_public_key = rsa.key().exportKey()

以上代码,rsa_private_key就是生成的公钥,格式如下:

-----BEGIN PUBLIC KEY-----

...

-----END PUBLIC KEY-----

rsa_public_key 就是生成的私钥,格式如下:

-----BEGIN RSA PRIVATE KEY-----

...

-----END RSA PRIVATE KEY-----

2.把生成的秘钥对保存在配置文件中

• 在Django表单中使用公钥对信息进行加密

1.user.py

from django.conf import settings

public_key = settings.RSA_PUBLIC_KEY
kwargs = {
'public_key' : public_key,
}
return render(request, 'accounts/login.html', kwargs)

2.login.html

2.1 引入js

<script src="/static/js/jsencrypt.min.js"></script>

2.2 页面form表单部分

        <form class="form-horizontal" role="form" id="login_form" action="{% url "login" %}" method="POST" >
<div class="box-body">
{% csrf_token %}
<div class="form-group">
<label class="control-label col-md-3">账号:</label>
<div class="col-sm-12">
<input type="text" class="form-control" id="user_name" name="user_name" value="">
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3">密码:</label> <div class="col-sm-12">
<input type="password" class="form-control" name="password" id="password" value="" onKeyPress="keypress(event)">
</div>
</div>
{% for item in form %}
<div class="text-left">
<div class="col-md-offset-1 col-md-10">
{{item.errors}} {{error_msg}}
</div>
</div>
{% endfor %}
<input type="hidden" name="public_key" id="public_key" value="{{ public_key }}">
</div>
<div class="box-footer">
<input type="hidden" name="next" value="{{ next }}" />
<input style="width: 340px;" type="submit" class="btn btn-info pull-right submit" onfocus="this.blur()" onclick="doLogin()" value="登录" >
</div>
</form>

2.3 js部分

<script type="text/javascript">
function doLogin()
{
var password_old = document.getElementById("password").value;
var encrypt = new JSEncrypt();
encrypt.setPublicKey($('#public_key').val());
var password_new = encrypt.encrypt(password_old);
document.getElementById("password").value = password_new;
login_form.submit();
}
</script>
• 在Django后台中使用私钥对前端POST的加密信息进行解密
from Crypto import Random
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_v1_5, PKCS1_OAEP
from django.conf import settings
import base64 random_generator = Random.new().read
RSA.generate(1024, random_generator)
rsakey = RSA.importKey(settings.RSA_PRIVATE_KEY)
cipher = PKCS1_v1_5.new(rsakey)
password = cipher.decrypt(base64.b64decode(password), random_generator)

以上代码,password则为解密后的数据