TungNT (Blue)

tungnt.blue@gmail.com

User Tools

Site Tools


development:security:rsa

This is an old revision of the document!


1. Tạo key RSA

Một số lệnh tạo key file:

openssl genrsa -des3 -out private_key.key 2048
openssl genrsa -out private_key.key 2048 # without password
openssl req -new -key private_key.key -out private_key.csr
openssl req -new -x509 -days 1001 -key private_key.key -sha256  -out public_key.cer
openssl req -new -x509 -nodes -sha256 -days 365 -key private_key.key -out public_key.cert
openssl pkcs12 -export -in public_key.cer -inkey private_key.key -out private_key.pfx
openssl pkcs12 -in private_key.pfx -nocerts -out private_key.pem
openssl pkcs12 -export -in public_key.cer -inkey private_key.key -out private_key.p12 
openssl x509 -in public_key.cer -out public_key.der -outform DER
openssl x509 -inform der -in public_key.der -out public_key.cer
 
keytool -importkeystore -srckeystore private_key.p12 -srcstoretype PKCS12 -destkeystore private_key.jks -deststoretype JKS 
keytool -importkeystore -srckeystore private_key.jks -destkeystore private_key.jks -deststoretype pkcs12
keytool -changealias -alias 1 -destalias custom-alias-name -keystore private_key.jks

Tham khảo:

2. Sử dụng RSA từ postman

Cài đặt thư viện: https://joolfe.github.io/postman-util-lib/dist/bundle.js

Add đoạn code mẫu sau vào tab scripts của postman:

const uuid = require('uuid'); 
eval(pm.globals.get('pmlib_code'));
const requestId = uuid.v4();    
const partnerId = pm.environment.get("partner_id");
const amount = 20000;
const expires_time = 12;
// Merchant's Private Key
const merchantPrivateKey = pm.environment.get("merchant_private_key");
const sig = new pmlib.rs.crypto.Signature({"alg": "SHA1withRSA"}); // If use rsa sha1 algorithm
//const sig = new pmlib.rs.crypto.Signature({"alg": "SHA256withRSA"}); // If use rsa sha256 algorithm
 
const payload = requestId + "|" + partnerId + "|" + amount;
 
sig.init(merchantPrivateKey);
sig.updateString(payload);
 
var sigVal = sig.sign();
var sigStr = pmlib.rs.hextob64u(sigVal);
 
sigStr = sigStr.replace(/_/g,"/");
sigStr = sigStr.replace(/-/g,"+") + '==';
 
pm.environment.set("request_id", requestId);
pm.environment.set("partner_id", partnerId);
pm.environment.set("amount", amount);
pm.environment.set("expires_time", expires_time);
pm.environment.set("signature", sigStr);

3. Tra cứu chứng thư số

openssl x509 -in public_key.cer -text -noout
openssl x509 -noout -serial -in public_key.cer

https://i-ca.vn/chung-thu-so/tra-cuu

4. Tra cứu Remote Certificates

openssl s_client -connect www.example.com:443 < /dev/null | openssl x509 -noout -dates
development/security/rsa.1724213160.txt.gz · Last modified: 2024/08/21 04:06 by tungnt

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki