nodejs 버 전 DESede / CBC / PKCS5Padding 알고리즘 패키지 (3des)

2150 단어
이야기 의 배경
최근 에 제3자 결 제 를 받 았 습 니 다. 관 명 PKU 의 결제 항목 에 사 용 된 암호 화 알고리즘 은 다음 과 같 습 니 다. DESede/CBC/PKCS5Padding 사실은 AES / DES 와 같은 대칭 암호 화 입 니 다. 이 알고리즘 은 정말 구덩이 아버지 입 니 다. 인터넷 에서 자바 버 전 만 정상 적 인 것 으로 검색 되 었 습 니 다. nodejs 버 전의 여러 가지 문 제 를 검 색 했 습 니 다. 저 는 순 순 했 습 니 다. 억지로 반나절 동안 조정 하고 N 개의 구 덩이 를 밟 았 습 니 다. 정말 어이 가 없습니다.talk is cheap, 코드 올 려!
핵심 코드
const crypto = require('crypto');

/**
 * base64  
 * @param text
 * @returns {Buffer}
 */
function base64(text) {
    return Buffer.from(text, "base64");
};

/**
 *   
 *
 * @param text
 * @param secretKey
 * @returns {string}
 */
function encode(text, secretKey) {
    secretKey = base64(secretKey);
    const cipher = crypto.createCipheriv('des-ede3-cbc', secretKey, Buffer.alloc(8));
    const encrypted = cipher.update(text, 'utf8', 'base64');

    return encrypted + cipher.final('base64');
};


/**
 *   
 * @param encryptedBase64
 * @param secretKey
 * @returns {string}
 */
function decode(encryptedBase64, secretKey) {
    secretKey = base64(secretKey);
    const decipher = crypto.createDecipheriv('des-ede3-cbc', secretKey, Buffer.alloc(8));
    let decrypted = decipher.update(encryptedBase64, 'base64', 'utf8');
    decrypted += decipher.final('utf8');
    return decrypted;
};

실행 결과
저희 가 운행 을 해 보도 록 하 겠 습 니 다.
//     
let json = `{"name":"chenqionghe","cn":"    ","content":"no pain no gain, light weight baby"}`;
//  
let key = 'mdgIaBrQjIKU30IIEpZS1dsFNOLX73nQ';
//  
let encrypted = encode(json, key);
console.log(encrypted);
//  
console.log(decode(encrypted, key));

출력
EPvugsT71sqeIDPuVuP0mx+cotWTJ3Bt+k5vIConcQmdyjIgF3GtQLSL+yCyxTfRQIjBFmkq7bQn6Nh0xOjMSm3C23AM1m3QwZLFQHH2t41X/4YyvCnv3YFtgDu+SosO
{"name":"chenqionghe","cn":"    ","content":"no pain no gain, light weight baby"}

이상 의 내용 은 chenqionghe 가 구 덩이 를 밟 아 포장 한 것 입 니 다. lidong 동화 의 열정 적 인 공연 에 감 사 드 립 니 다. 주 소 를 설명해 주 십시오.

좋은 웹페이지 즐겨찾기