Skip to content
This repository has been archived by the owner on Sep 27, 2020. It is now read-only.

Commit

Permalink
Merge pull request #33 from rc452860/master
Browse files Browse the repository at this point in the history
添加RC4加密方式
  • Loading branch information
Srar authored Jun 30, 2018
2 parents 9ffab6c + 2360b7a commit 678c084
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
65 changes: 65 additions & 0 deletions test/shadowsocks/crypto/CryptoMethods/RC4.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import * as crypto from "crypto";
import CryptoTools from "../CryptoTools";
import ICryptoKeyIV from "../ICryptoKeyIV";
import ISSCryptoMethod from "../ISSCryptoMethod";

export default class RC4 implements ISSCryptoMethod {

private static readonly keyLength: number = 16;
private static readonly ivLength: number = 0;
private static readonly cryptoName: string = "rc4";
private cryptoKeyIV: ICryptoKeyIV;

private isFirstEncryptData: boolean = true;
private isFirstDecryptData: boolean = true;

private encryptProcess: crypto.Cipher = null;
private decryptProcess: crypto.Decipher = null;

constructor(private password?: string) {
if (!password) {
return;
}
this.cryptoKeyIV = CryptoTools.generateKeyIVByPassword(this.password, RC4.keyLength, RC4.ivLength);
}

public encryptData(data: Buffer): Buffer {
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) {
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)]);
}

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));
}

public getCryptoName(): string {
return RC4.cryptoName;
}
}

3 changes: 3 additions & 0 deletions test/shadowsocks/crypto/SSCrypto.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import ISSCryptoMethod from "./ISSCryptoMethod";

import RC4 from "./CryptoMethods/RC4";
import RC4MD5 from "./CryptoMethods/RC4MD5";
import AES256CFB from "./CryptoMethods/AES256CFB";
import AES128GCM from "./CryptoMethods/AES128GCM";
Expand All @@ -8,13 +9,15 @@ import AES256GCM from "./CryptoMethods/AES256GCM";

const cryptoMethods: { [methodName: string]: any } = {};

cryptoMethods[new RC4().getCryptoName().toLocaleLowerCase()] = RC4;
cryptoMethods[new RC4MD5().getCryptoName().toLocaleLowerCase()] = RC4MD5;
cryptoMethods[new AES256CFB().getCryptoName().toLocaleLowerCase()] = AES256CFB;

cryptoMethods[new AES128GCM().getCryptoName().toLocaleLowerCase()] = AES128GCM;
cryptoMethods[new AES192GCM().getCryptoName().toLocaleLowerCase()] = AES192GCM;
cryptoMethods[new AES256GCM().getCryptoName().toLocaleLowerCase()] = AES256GCM;


export default class SSCrypto {

public static getAllCryptoMethods(): Array<string> {
Expand Down

0 comments on commit 678c084

Please sign in to comment.