From 845d842ca31c7ee11dee970caf8cbf15cb5a88b3 Mon Sep 17 00:00:00 2001 From: Srar Date: Sun, 1 Jul 2018 12:28:32 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/shadowsocks/crypto/CryptoMethods/RC4.ts | 19 +++++-------------- .../crypto/CryptoMethods/RC4MD5.ts | 6 ++---- test/shadowsocks/crypto/CryptoTools.ts | 2 +- 3 files changed, 8 insertions(+), 19 deletions(-) diff --git a/test/shadowsocks/crypto/CryptoMethods/RC4.ts b/test/shadowsocks/crypto/CryptoMethods/RC4.ts index 93804af..10c7523 100644 --- a/test/shadowsocks/crypto/CryptoMethods/RC4.ts +++ b/test/shadowsocks/crypto/CryptoMethods/RC4.ts @@ -24,38 +24,29 @@ export default class RC4 implements ISSCryptoMethod { } public encryptData(data: Buffer): Buffer { - if (this.isFirstEncryptData) { + if (this.isFirstEncryptData) { this.isFirstEncryptData = false; - this.cryptoKeyIV.iv = crypto.randomBytes(RC4.ivLength); this.encryptProcess = crypto.createCipheriv("rc4", this.cryptoKeyIV.key , ""); - return Buffer.concat([this.cryptoKeyIV.iv, this.encryptProcess.update(data)]); } - // tslint:disable-next-line:align return this.encryptProcess.update(data); } - public decryptData(data: Buffer): Buffer { - if (this.isFirstDecryptData) { + public decryptData(data: Buffer): Buffer { + if (this.isFirstDecryptData) { this.isFirstDecryptData = false; - const decryptIV: Buffer = data.slice(0, RC4.ivLength); this.decryptProcess = crypto.createDecipheriv("rc4", this.cryptoKeyIV.key, ""); - return this.decryptProcess.update(data.slice(RC4.ivLength)); } - // tslint:disable-next-line:align return this.decryptProcess.update(data); } public encryptDataWithoutStream(data: Buffer) { - this.cryptoKeyIV.iv = crypto.randomBytes(RC4.ivLength); - // const rc4Process: Buffer = CryptoTools.generateRc4Md5KeyByKV(this.cryptoKeyIV); const encryptProcess = crypto.createCipheriv("rc4", this.cryptoKeyIV.key , ""); - return Buffer.concat([this.cryptoKeyIV.iv, encryptProcess.update(data)]); + return Buffer.concat([encryptProcess.update(data), encryptProcess.final()]); } public decryptDataWithoutStream(data: Buffer) { - const decryptIV: Buffer = data.slice(0, RC4.ivLength); const decryptProcess = crypto.createDecipheriv("rc4", this.cryptoKeyIV.key, ""); - return decryptProcess.update(data.slice(RC4.ivLength)); + return decryptProcess.update(data); } public getCryptoName(): string { diff --git a/test/shadowsocks/crypto/CryptoMethods/RC4MD5.ts b/test/shadowsocks/crypto/CryptoMethods/RC4MD5.ts index 50a1823..424141c 100644 --- a/test/shadowsocks/crypto/CryptoMethods/RC4MD5.ts +++ b/test/shadowsocks/crypto/CryptoMethods/RC4MD5.ts @@ -24,26 +24,24 @@ export default class RC4MD5 implements ISSCryptoMethod { } public encryptData(data: Buffer): Buffer { - if (this.isFirstEncryptData) { + if (this.isFirstEncryptData) { this.isFirstEncryptData = false; this.cryptoKeyIV.iv = crypto.randomBytes(RC4MD5.ivLength); const rc4Process: Buffer = CryptoTools.generateRc4Md5KeyByKV(this.cryptoKeyIV); this.encryptProcess = crypto.createCipheriv("rc4", rc4Process , ""); return Buffer.concat([this.cryptoKeyIV.iv, this.encryptProcess.update(data)]); } - // tslint:disable-next-line:align return this.encryptProcess.update(data); } public decryptData(data: Buffer): Buffer { - if (this.isFirstDecryptData) { + if (this.isFirstDecryptData) { this.isFirstDecryptData = false; const decryptIV: Buffer = data.slice(0, RC4MD5.ivLength); const rc4Process: Buffer = CryptoTools.generateRc4Md5KeyByKV({ key: this.cryptoKeyIV.key, iv: decryptIV }); this.decryptProcess = crypto.createDecipheriv("rc4", rc4Process, ""); return this.decryptProcess.update(data.slice(RC4MD5.ivLength)); } - // tslint:disable-next-line:align return this.decryptProcess.update(data); } diff --git a/test/shadowsocks/crypto/CryptoTools.ts b/test/shadowsocks/crypto/CryptoTools.ts index e39aecd..f920ff0 100644 --- a/test/shadowsocks/crypto/CryptoTools.ts +++ b/test/shadowsocks/crypto/CryptoTools.ts @@ -18,7 +18,7 @@ export default class CryptoTools { } const hashBuffer: Buffer = Buffer.concat(hashBuffers); const key: Buffer = hashBuffer.slice(0, keyLength); - const iv: Buffer = hashBuffer.slice(keyLength, keyLength + ivLength); + const iv: Buffer = crypto.randomBytes(ivLength); return { key, iv,