Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const alphabet = (() => {
- const letters = 'АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ'
- const chars = ['.', ',', '!', '?', '-', ' ']
- return [...letters, ...letters.toLowerCase(), ...chars].reduce(
- (acc, char) => ({ ...acc, [char]: Object.keys(acc).length + 2 }),
- {}
- )
- })()
- class RSA {
- #encodedChars
- #decodedChars
- #p
- #q
- #N
- #d
- #e
- constructor(message) {
- this.message = message
- this.#computeEnv()
- }
- encodeChars() {
- this.#encodedChars = [...this.message].map(char => {
- const largeInteger =
- BigInt(alphabet[char]) ** BigInt(this.#e) %
- BigInt(this.#N)
- return largeInteger.valueOf()
- })
- return this.#encodedChars
- }
- decodeChars() {
- if (!this.#encodedChars)
- throw Error('Вначале необходимо закодировать символы')
- this.#decodedChars = this.#encodedChars.map((encodedChar) => {
- const num = BigInt(encodedChar) ** BigInt(this.#d) % BigInt(this.#N)
- return num.valueOf()
- })
- return this.#decodedChars
- }
- decodeMessage() {
- if (!this.#decodedChars)
- throw Error('Вначале необходимо декодировать символы')
- return this.#decodedChars.reduce((word, decodedChar) => {
- const [char] = Object.entries(alphabet).find(
- ([, digit]) => digit === +decodedChar.toString()
- )
- return word + char
- }, '')
- }
- #computeEnv() {
- ;[this.#p, this.#q] = this.#generatePandQ()
- this.#N = this.#p * this.#q
- const fiN = (this.#p - 1) * (this.#q - 1)
- this.#e = this.#generateE(fiN)
- this.#d = this.#gcdex(fiN, this.#e)[2]
- }
- #generatePandQ() {
- const [p, q] = [this.#randomInt(1, 100), this.#randomInt(1, 100)]
- if (this.#isPrime(p) && this.#isPrime(q) && 100 <= p * q && p * q < 1000) {
- return [p, q]
- }
- return this.#generatePandQ()
- }
- #generateE(fiN) {
- const e = this.#randomInt(100, 1000)
- if (this.#gcd(fiN, e) !== 1 || this.#gcdex(fiN, e)[2] < 0) {
- return this.#generateE(fiN)
- }
- return e
- }
- #gcd(a, b) {
- let r = a % b
- while (r) {
- ;[a, b] = [b, r]
- r = a % b
- }
- return b
- }
- #gcdex(a, b) {
- if (!b) return [a, 1, 0]
- const [nod, x, y] = this.#gcdex(b, a % b)
- return [nod, y, x - y * Math.floor(a / b)]
- }
- #isPrime(num) {
- for (let i = 2; i < num; i++) {
- if (!(num % i)) return false
- }
- return num > 1
- }
- #randomInt(min, max) {
- const rand = min + Math.random() * (max + 1 - min)
- return Math.floor(rand)
- }
- }
- const message =
- 'пук-пук-ппривет, а у тебя сколько отчимов? мать найдешь - напишешь'
- console.log({ message })
- const rsa = new RSA(message)
- const encodedChars = rsa.encodeChars()
- console.log({ encodedChars })
- const decodedChars = rsa.decodeChars()
- console.log({ decodedChars })
- const decodedWord = rsa.decodeMessage()
- console.log({ decodedWord })
- // Number.prototype.isPrime = function () {
- // for (let i = 2; i < this; i++) {
- // if (this % i === 0) return false
- // }
- // return this > 1
- // }
- // function randomInt(min, max) {
- // const rand = min + Math.random() * (max + 1 - min)
- // return Math.floor(rand)
- // }
- // function gcd(a, b) {
- // let r = a % b
- // while (r) {
- // ;[a, b] = [b, r]
- // r = a % b
- // }
- // return b
- // }
- // function gcdex(a, b) {
- // if (b === 0) return [a, 1, 0]
- // const [nod, x, y] = gcdex(b, a % b)
- // return [nod, y, x - y * Math.floor(a / b)]
- // }
- // function generatePandQ() {
- // const [p, q] = [randomInt(1, 100), randomInt(1, 100)]
- // if (p.isPrime() && q.isPrime() && 100 <= p * q && p * q < 1000) {
- // return [p, q]
- // }
- // return generatePandQ()
- // }
- // function generateE() {
- // const e = randomInt(100, 1000)
- // if (gcd(fiN, e) !== 1 || gcdex(fiN, e)[2] < 0) {
- // return generateE()
- // }
- // return e
- // }
- // let [code, decode] = [[], []]
- // const [p, q] = generatePandQ()
- // const N = p * q
- // const fiN = (p - 1) * (q - 1)
- // const e = generateE()
- // const d = gcdex(fiN, e)[2]
- // ;[...message].forEach((_, index) => {
- // // const largeInteger = BigInteger(alphabet[message[index]]).pow(e).mod(N)
- // const largeInteger = BigInt(alphabet[message[index]]) ** BigInt(e) % BigInt(N)
- // code[index] = largeInteger.valueOf()
- // })
- // console.log('зашифрованный код:', code)
- //
- // ;[...code].forEach((_, index) => {
- // const char = BigInt(code[index]) ** BigInt(d) % BigInt(N)
- // decode[index] = char.valueOf()
- // // decode[index] = code[index] ** d % N
- // })
- // console.log('расшифрованный код:', decode)
- // let word = ''
- // decode.forEach((decodedChar) => {
- // const [char] = Object.entries(alphabet).find(
- // ([, digit]) => digit === +decodedChar.toString()
- // )
- // word += char
- // })
- // console.log('исходное слово:', word)
Advertisement
Add Comment
Please, Sign In to add comment