RSA公開密鑰密碼體制
目錄
<script src="https://cdn.bootcdn.net/ajax/libs/jsencrypt/3.1.0/jsencrypt.min.js"></script>
<script>
// 公鑰
const publicKey =
'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDlOJu6TyygqxfWT7eLtGDwajtNFOb9I5XRb6khyfD1Yt3YiCgQWMNW649887VGJiGr/L5i2osbl8C9+WJTeucF+S76xFxdU6jE0NQ+Z+zEdhUTooNRaY5nZiu5PgDB0ED/ZKBUSLKL7eibMxZtMlUDHjm4gwQco1KRMDSmXSMkDwIDAQAB';
var encrypt = new JSEncrypt();
encrypt.setPublicKey(publicKey);
var encrypted = encrypt.encrypt('hello');
console.log(encrypted);
</script>
安裝
npm i jsencrypt
配合vue使用
參考:
https://www.jb51.net/article/159904.htm<template>
</template>
<script>
import JSEncrypt from 'jsencrypt';
export default {
name: '',
methods: {
encryptData() {
// 引入加密子產品,對密碼進行處理
// 公鑰
const publicKey =
'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDlOJu6TyygqxfWT7eLtGDwajtNFOb9I5XRb6khyfD1Yt3YiCgQWMNW649887VGJiGr/L5i2osbl8C9+WJTeucF+S76xFxdU6jE0NQ+Z+zEdhUTooNRaY5nZiu5PgDB0ED/ZKBUSLKL7eibMxZtMlUDHjm4gwQco1KRMDSmXSMkDwIDAQAB';
var encrypt = new JSEncrypt();
encrypt.setPublicKey(publicKey);
var encrypted = encrypt.encrypt('hello');
console.log(encrypted);
},
},
created() {
this.encryptData();
},
};
</script>
<style scoped>
</style>
三、 Python 實作1
# -*- coding: utf-8 -*-
import base64
from Crypto.Cipher import PKCS1_v1_5 as Cipher_pksc1_v1_5
from Crypto.PublicKey import RSA
def encrpt(password, public_key):
public_key = '-----BEGIN PUBLIC KEY-----\n' + public_key + '\n-----END PUBLIC KEY-----'
rsakey = RSA.importKey(public_key)
cipher = Cipher_pksc1_v1_5.new(rsakey)
cipher_text = base64.b64encode(cipher.encrypt(password.encode()))
return cipher_text.decode()
# key需要修改成自己的
key = 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDlOJu6TyygqxfWT7eLtGDwajtNFOb9I5XRb6khyfD1Yt3YiCgQWMNW649887VGJiGr/L5i2osbl8C9+WJTeucF+S76xFxdU6jE0NQ+Z+zEdhUTooNRaY5nZiu5PgDB0ED/ZKBUSLKL7eibMxZtMlUDHjm4gwQco1KRMDSmXSMkDwIDAQAB'
password = encrpt('hello', key)
print(password)
python 3 實作js中JSEncrypt encrypt方法,rsa子產品根據字元串公鑰生成加密字元串
# -*- coding: utf-8 -*-
# coding=utf-8
import base64
import rsa
__all__ = ['rsa_encrypt']
def _str2key(s):
# 對字元串解碼
b_str = base64.b64decode(s)
if len(b_str) < 162:
return False
hex_str = ''
# 按位轉換成16進制
for x in b_str:
h = hex(x)[2:]
h = h.rjust(2, '0')
hex_str += h
# 找到模數和指數的開頭結束位置
m_start = 29 * 2
e_start = 159 * 2
m_len = 128 * 2
e_len = 3 * 2
modulus = hex_str[m_start:m_start + m_len]
exponent = hex_str[e_start:e_start + e_len]
return modulus, exponent
def rsa_encrypt(s, pubkey_str):
'''
rsa加密
:param s:
:param pubkey_str:公鑰
:return:
'''
key = _str2key(pubkey_str)
modulus = int(key[0], 16)
exponent = int(key[1], 16)
pubkey = rsa.PublicKey(modulus, exponent)
return base64.b64encode(rsa.encrypt(s.encode(), pubkey)).decode()
key = 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDlOJu6TyygqxfWT7eLtGDwajtNFOb9I5XRb6khyfD1Yt3YiCgQWMNW649887VGJiGr/L5i2osbl8C9+WJTeucF+S76xFxdU6jE0NQ+Z+zEdhUTooNRaY5nZiu5PgDB0ED/ZKBUSLKL7eibMxZtMlUDHjm4gwQco1KRMDSmXSMkDwIDAQAB'
password = rsa_encrypt('hello', key)
print(password)
其他文章
python 實作RSA公鑰加密,私鑰解密 Python實作RSA(jsencrypt)加密的兩種方式