nodejs 버 전 DESede / CBC / PKCS5Padding 알고리즘 패키지 (3des)
최근 에 제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 동화 의 열정 적 인 공연 에 감 사 드 립 니 다. 주 소 를 설명해 주 십시오.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.