接口测试中加密算法-非对称加密(RAS)

非对称加密(RSA)

  • 非对称加密。加密和解密的秘钥不是同一个秘钥。这里需要两把钥匙。一个公钥,一个私钥。公钥发送给客户端。发送端用公钥对数据进行加密。在发送给接收端,接收端使用私钥来对数据解密。由于私钥只存放在接收端这边。所以即使数据被截获了,也是无法进行解密的。
  • 公钥和私钥
  • 常见的非对称加密算法:RSA/DSA等等,我们就介绍一个,RSA加密,也是最常见的一种加密方案。
  • 创建公钥和私钥
from Crypto.PublicKey import RSA
#生成秘钥
rsakey=RSA.generate(1024)
with open("rsa.public.pem",mode="wb") as f:
	f.write(rsakey.publickey().exportKey())
with open("rsa.private.pem",mode="wb") as f:
	f.write(rsakey.exportKey())
  • 加密
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_V1_5
import base64
#加密
data='我喜欢你'
with open("rsa.public.pem",mode='r') as f:
	pk=f.read()
	rsa_pk=RSA.importKey(pk)
	rsa=PKCS1_v1_5.new(rsa_pk)
	result=rsa.encrypt(data.encode("utf-8"))
#处理成b64方便传输
    b64_result=base64.b64encode(result).decode("utf-8")
    print(b64_result)
  • 解密
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_V1_5
import base64
data='BkiKG8jzVGzbWOl4m8NXJEYglgtxhOB05MGmap8JSP97GzoewPBmDTs7c5iACUof3k/uJf0H88GygajVgBvkcbckJp7oO+Qj6VSUQYTOHhKN/VG2a8v+WzL34EO/S7BYoj2oOxIDAr8wDLxYxjBeXq/Be6Q1yBbnZcKaMkifhP8='
#解密
with open("rsa.private.pem",mode="r") as f:
	prikey=f.read()
	rsa_pk=RSA.importKey(prikey)
	rsa=PKCS1_V1_5.new(rsa_pk)
	result=rsa.decrypt(base64.b64decode(data),None)
	print("rsa解密数据:::",result.decode("utf-8"))
  • 9
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
抱歉,作为AI助手,我不会编写代码,但我可以为您提供RAS加密算法的基本原理和C语言实现的步骤供您参考。 RAS加密算法基本原理: RAS(Rivest-Shamir-Adleman)是一种非对称加密算法,具有高安全性。它使用两个密钥:公钥和私钥。公钥可以用于加密,私钥可以用于解密,因此可以用于安全地传输信息。 C语言实现步骤: 1. 生成公钥和私钥:使用数学算法生成一对公钥和私钥,公钥可以公开,私钥必须保密。 2. 加密明文:使用公钥将明文加密,生成密文。 3. 解密密文:使用私钥将密文解密,得到原始明文。 下面是一个简单的C语言实现RAS加密算法的代码示例: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <openssl/rsa.h> #include <openssl/pem.h> int main() { RSA *rsa = NULL; unsigned char *plaintext = "Hello, world!"; unsigned char *ciphertext = NULL; unsigned char *decryptedtext = NULL; int ciphertext_len, decryptedtext_len; // 生成公钥和私钥 rsa = RSA_new(); BIGNUM *e = BN_new(); BN_set_word(e, RSA_F4); RSA_generate_key_ex(rsa, 2048, e, NULL); // 加密明文 ciphertext = (unsigned char*)malloc(RSA_size(rsa)); ciphertext_len = RSA_public_encrypt(strlen(plaintext)+1, plaintext, ciphertext, rsa, RSA_PKCS1_PADDING); if(ciphertext_len == -1) { printf("加密失败!\n"); return -1; } // 解密密文 decryptedtext = (unsigned char*)malloc(ciphertext_len); decryptedtext_len = RSA_private_decrypt(ciphertext_len, ciphertext, decryptedtext, rsa, RSA_PKCS1_PADDING); if(decryptedtext_len == -1) { printf("解密失败!\n"); return -1; } // 输出结果 printf("明文:%s\n", plaintext); printf("密文:%s\n", ciphertext); printf("解密后的明文:%s\n", decryptedtext); RSA_free(rsa); free(ciphertext); free(decryptedtext); return 0; } ``` 需要注意的是,这段代码需要使用OpenSSL库,可以使用以下命令安装: ``` sudo apt-get install libssl-dev ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值