본문 바로가기
etc

Postman에서 RSA로 매개변수 암호화 하기

by Sonagiya 2024. 3. 12.
반응형

1. 암호 스크립트 다운로드

1-1

 

#그림 1-1 코드

더보기
pm.test("Status code should be 200", function () {
    pm.response.to.have.status(200)
    pm.globals.set("jsrsasign_code", responseBody)
});

 

1-2

 

2. 서버에서 publicKey 받기(프로젝트 별 서버구성 필요)

  • 그림 2-1] Tests에 collection 에 저장하는 코드 추가
  • PublicKey 를 요청
  • 그림 2-2] Collections > 테스트(컬렉션 명) > Edit > Variables 선택 후 PublicKey가 정상적으로 추가된 것을 확인

2-1

 

#그림 2-1 코드

더보기
var jsonData = JSON.parse(responseBody);
pm.collectionVariables.set("pubKey", jsonData.data.publicKey);

 

2-2

 

3. RSA로 메개변수 암호화

  • 그림 3-1] Request Body 작성
  • 그림 3-2] Pre-request Script 작성
  • Send
  • 그림 3-3] Send 후 Collections > 테스트(컬렉션 명) > Edit > Variables 선택 후 값들을 확인하면 정상적으로 암호화가 된것으 확인 할 수 있음

3-1

 

3-2

#그림 3-2 코드

더보기
var pubkeyFromServer = pm.collectionVariables.get("pubKey");
console.log(pubkeyFromServer);

const pubkey = `
-----BEGIN PUBLIC KEY-----
${pubkeyFromServer}
-----END PUBLIC KEY-----
`;

var navigator = {}; //fake a navigator object for the lib
var window = {}; //fake a window object for the lib
eval(pm.globals.get('jsrsasign_code'))

const id = 'id'
const pwd = 'pwd'

let keyObj = KEYUTIL.getKey(pubkey);
let encId = KJUR.crypto.Cipher.encrypt(id, keyObj);
let encPwd = KJUR.crypto.Cipher.encrypt(pwd, keyObj);

var base64Id = hextob64(encId);
var base64Pwd = hextob64(encPwd);

pm.collectionVariables.set("base64Id", base64Id);
pm.collectionVariables.set("base64Pwd", base64Pwd);

 

 

3-3

 

반응형

댓글