-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
249 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
class AffineCipher { | ||
constructor(a, b) { | ||
this.ciphers = []; | ||
this.N = 48; | ||
this.M = 75; | ||
this.encrypt = (text) => { | ||
return text.split('') | ||
.map(c => this.ciphers[c.charCodeAt(0) - this.M] || ' ') | ||
.reduce((p, n) => p + n); | ||
}; | ||
this.decrypt = (text) => { | ||
return text.split('') | ||
.map(c => this.decryptChar(c) || ' ') | ||
.reduce((p, n) => p + n); | ||
}; | ||
this.decryptChar = (cipher) => { | ||
const index = this.ciphers.indexOf(cipher); | ||
return (index == -1) ? null : String.fromCharCode(index + this.M); | ||
}; | ||
this.affine = (a, b) => { | ||
for (let i = this.N; i < this.M + this.N; i++) { | ||
const x = i - this.N; | ||
const x1 = a * x + b; | ||
const x2 = x1 % this.M; | ||
const cipher = String.fromCharCode(x2 + this.N); | ||
if (this.ciphers.indexOf(cipher) == -1) { | ||
this.ciphers.push(cipher); | ||
} | ||
else { | ||
throw Error(`The value of a has to be coprime with ${this.M}`); | ||
} | ||
} | ||
}; | ||
this.affine(a, b); | ||
} | ||
} | ||
AffineCipher.encrypt = (text) => { | ||
return ''; | ||
}; | ||
AffineCipher.decrypt = (text) => { | ||
return ''; | ||
}; | ||
exports.AffineCipher = AffineCipher; | ||
exports.default = AffineCipher; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Cipher } from './Cipher'; | ||
export declare class AffineCipher implements Cipher { | ||
private ciphers; | ||
private N; | ||
private M; | ||
constructor(a: number, b: number); | ||
static encrypt: (text: string) => string; | ||
static decrypt: (text: string) => string; | ||
encrypt: (text: string) => string; | ||
decrypt: (text: string) => string; | ||
private decryptChar; | ||
private affine; | ||
} | ||
export default AffineCipher; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export interface Cipher { | ||
encrypt(text: string): string; | ||
decrypt(text: string): string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export { Rot13Cipher } from './Rot13Cipher'; | ||
export { CaesarCipher } from './CaesarCipher'; | ||
export { MorseCipher } from './MorseCipher'; | ||
export { AffineCipher } from './AffineCipher'; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { Cipher } from './Cipher'; | ||
|
||
export class AffineCipher implements Cipher { | ||
|
||
private ciphers: Array<string> = []; | ||
|
||
private N = 48; | ||
private M = 75; | ||
|
||
constructor(a: number, b: number) { | ||
this.affine(a, b); | ||
} | ||
|
||
public static encrypt = (text: string) => { | ||
return ''; | ||
} | ||
|
||
public static decrypt = (text: string) => { | ||
return ''; | ||
} | ||
|
||
public encrypt = (text: string): string => { | ||
return text.split('') | ||
.map(c => this.ciphers[c.charCodeAt(0) - this.M] || ' ') | ||
.reduce((p, n) => p + n); | ||
} | ||
|
||
public decrypt = (text: string) => { | ||
return text.split('') | ||
.map(c => this.decryptChar(c) || ' ') | ||
.reduce((p, n) => p + n); | ||
} | ||
|
||
private decryptChar = (cipher: string) => { | ||
const index = this.ciphers.indexOf(cipher); | ||
return (index == -1) ? null : String.fromCharCode(index + this.M); | ||
} | ||
|
||
private affine = (a: number, b: number) => { | ||
for (let i = this.N; i < this.M + this.N; i++) { | ||
const x = i - this.N; | ||
const x1 = a * x + b; | ||
const x2 = x1 % this.M; | ||
|
||
const cipher = String.fromCharCode(x2 + this.N); | ||
if (this.ciphers.indexOf(cipher) == -1) { | ||
this.ciphers.push(cipher); | ||
} else { | ||
throw Error(`The value of a has to be coprime with ${this.M}`); | ||
} | ||
} | ||
} | ||
} | ||
|
||
export default AffineCipher; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export interface Cipher { | ||
encrypt(text: string): string; | ||
decrypt(text: string): string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { expect } from 'chai'; | ||
|
||
import { AffineCipher } from '../src/index'; | ||
|
||
describe(`Test "AffineCipher"`, () => { | ||
const encrypter = new AffineCipher(7, 10); | ||
let encrypted = 'oZ@@U BUj@S'; | ||
let decrypted = 'hello world'; | ||
|
||
// it('Check static "AffineCipher.encrypt"', () => { | ||
// expect(encrypted).to.be.equal(AffineCipher.encrypt(decrypted, 10)); | ||
// }); | ||
|
||
// it('Check static "AffineCipher.decrypt"', () => { | ||
// expect(decrypted).to.be.equal(AffineCipher.decrypt(encrypted, 10)); | ||
// }); | ||
|
||
it('Check "AffineCipher.encrypt"', () => { | ||
expect(encrypted).to.be.equal(encrypter.encrypt(decrypted)); | ||
}); | ||
|
||
it('Check "AffineCipher.decrypt"', () => { | ||
expect(decrypted).to.be.equal(encrypter.decrypt(encrypted)); | ||
}); | ||
}); |