RSA算法在Python Django中的簡單應用


說明

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則為解密后的數據


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM