微信小程序ras加密(比如密码加密)

1、小程序公共js文件引入wx_rsa.js文件   下载地址:https://download.csdn.net/download/huanhunacsdn/10433043

2、自己写的js

var rsa = require("../js/wx_rsa.js");

var  publicKey ='';(后端提供的publicKey)

function pubRSA(text){

var input_rsa = text;

if(!text){

// console.log(1);

var input_rsa = '加密文本';

}

var encStr = ""

var encrypt_rsa = rsa.RSAKey();

encrypt_rsa = rsa.KEYUTIL.getKey(publicKey);

encStr = encrypt_rsa.encrypt(input_rsa);

encStr = rsa.hex2b64(encStr);

return encStr;

console.log(encStr);

// console.log("加密结果:" + encStr)

}

module.exports = { pubRSA: pubRSA}

 

 

//页面调用:

var pubRSA = require("../../js/public_rsa.js");

//this.data.pwd为输入框输入的密码,这样就完成了小程序的密码加密提交给后台

var encStr = pubRSA.pubRSA(this.data.pwd);

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
好的,以下是一个简单的RAS加密解密程序的C++实现: ``` #include <iostream> #include <openssl/rsa.h> #include <openssl/pem.h> #include <openssl/err.h> using namespace std; // 生成RSA密钥对 RSA* generateRSAKeyPair(int keyLength) { RSA* rsa = NULL; BIGNUM* bn = NULL; FILE* fp = NULL; int ret = 0; // 创建RSA结构体 rsa = RSA_new(); if (rsa == NULL) { return NULL; } // 生成大数 bn = BN_new(); ret = BN_set_word(bn, RSA_F4); if (ret != 1) { goto free_all; } // 生成RSA密钥对 ret = RSA_generate_key_ex(rsa, keyLength, bn, NULL); if (ret != 1) { goto free_all; } // 返回生成的RSA密钥对 return rsa; free_all: if (rsa != NULL) { RSA_free(rsa); } if (bn != NULL) { BN_free(bn); } if (fp != NULL) { fclose(fp); } return NULL; } // RSA加密 int RSAEncrypt(const unsigned char* inData, int inLen, unsigned char* outData, RSA* rsa) { int ret = 0; // 获取RSA公钥 const BIGNUM* e = RSA_get0_e(rsa); const BIGNUM* n = RSA_get0_n(rsa); // 创建RSA公钥结构体 RSA* pubRSA = RSA_new(); ret = RSA_set0_key(pubRSA, const_cast<BIGNUM*>(n), const_cast<BIGNUM*>(e), NULL); if (ret != 1) { goto free_all; } // RSA加密 ret = RSA_public_encrypt(inLen, inData, outData, pubRSA, RSA_PKCS1_PADDING); if (ret < 0) { goto free_all; } // 释放RSA公钥结构体 RSA_free(pubRSA); // 返回加密数据长度 return ret; free_all: if (pubRSA != NULL) { RSA_free(pubRSA); } return -1; } // RSA解密 int RSADecrypt(const unsigned char* inData, int inLen, unsigned char* outData, RSA* rsa) { int ret = 0; // 获取RSA私钥 const BIGNUM* d = RSA_get0_d(rsa); const BIGNUM* n = RSA_get0_n(rsa); // 创建RSA私钥结构体 RSA* priRSA = RSA_new(); ret = RSA_set0_key(priRSA, const_cast<BIGNUM*>(n), const_cast<BIGNUM*>(d), NULL); if (ret != 1) { goto free_all; } // RSA解密 ret = RSA_private_decrypt(inLen, inData, outData, priRSA, RSA_PKCS1_PADDING); if (ret < 0) { goto free_all; } // 释放RSA私钥结构体 RSA_free(priRSA); // 返回解密数据长度 return ret; free_all: if (priRSA != NULL) { RSA_free(priRSA); } return -1; } int main() { RSA* rsa = NULL; unsigned char inData[1024] = "Hello, World!"; unsigned char outData[1024] = {0}; int outLen = 0; // 生成RSA密钥对 rsa = generateRSAKeyPair(1024); if (rsa == NULL) { cout << "Generate RSA key pair failed!" << endl; return -1; } // RSA加密 outLen = RSAEncrypt(inData, strlen((const char*)inData), outData, rsa); if (outLen < 0) { cout << "RSA encrypt failed!" << endl; return -1; } // RSA解密 outLen = RSADecrypt(outData, outLen, inData, rsa); if (outLen < 0) { cout << "RSA decrypt failed!" << endl; return -1; } // 输出解密结果 cout << "RSA decrypt result: " << inData << endl; // 释放RSA密钥对 RSA_free(rsa); return 0; } ``` 这个程序使用了OpenSSL库来实现RAS加密解密。在程序中,先使用`generateRSAKeyPair`函数生成RSA密钥对,然后使用`RSAEncrypt`函数来加密数据,使用`RSADecrypt`函数来解密数据。程序最终输出解密结果。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值