说明
RSA 算法是当今使用最广泛, 安全度最高的加密算法.
• RSA 算法的安全性理论基础
[引] 根据百科介绍, 对极大整数做因数分解的难度决定了 RSA 算法的可靠性. 换言之, 对一极大整数做因数分解愈困难, RSA 算法愈可靠. 假如有人找到一种快速因数分解的算法的话, 那么用 RSA 加密的信息的可靠性就肯定会极度下降. 但找到这样的算法的可能性是非常小的. 今天只有短的 RSA 钥匙才可能被强力方式解破. 到目前为止, 世界上还没有任何可靠的攻击 RSA 算法的方式. 只要其钥匙的长度足够长, 用 RSA 加密的信息实际上是不能被解破的.
• RSA 算法使用
通常使用公钥进行加密, 使用私钥进行解密.
如上所说, 钥匙的长度尽量设置长一些, 在实际应用过程中, 通常设置为 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 则为解密后的数据
来源: https://www.cnblogs.com/himismad/p/9722419.html