Giter VIP home page Giter VIP logo

onetimepadjs's Introduction

OneTimePadJS

NPM version NPM downloads jSDelivr CDN tests MIT License

Library of helper-functions for encrypting and decrypting messages with OTPs - One-time pads. Funcions for:

  • Generating one-time-pads - encryption/decryption keys.
  • Converting plaintext to plaincode.
  • Converting plaincode to plaintext.
  • Encrypting plaincode text
  • Decrypting encrypoted text
  • Check length of message is too long for the encryption key.
  • Language conversion tables, regular expressions for plaintext <-> plaincode and codebook for emojis.

Example-code

Will be core functionality together with nfc-json-transfer for creating otp-encryption-toy

Getting the script in your environment

CJS - CommonJS

const { textToPlaincode, plaincodeToText, createOnetimePad, nob, codebook, checkLength, encryptPlaincode, decryptEncryptedMsg } = require('otp-encryption-decryption-lib')

ESM - Ecmascript Modules

import { textToPlaincode, plaincodeToText, createOnetimePad, nob, codebook, checkLength, encryptPlaincode, decryptEncryptedMsg } from 'otp-encryption-decryption-lib'

Usage

index.mjs:

import { textToPlaincode, plaincodeToText, createOnetimePad, nob, codebook, checkLength, encryptPlaincode, decryptEncryptedMsg } from 'otp-encryption-decryption-lib'

// The message
const txt = 'Hello πŸ‘¨β€πŸ‘©β€πŸ‘¦β€πŸ‘¦πŸ³οΈβ€πŸŒˆπŸ˜€πŸ‡ΏπŸ‡Ό  world 123 æøΓ₯!'
console.log('\n\nInput:               ' + txt)

// ### Text to plaincode
const plaincodeConverted = textToPlaincode(txt, nob, codebook)
console.log('Plaincode:           ' + plaincodeConverted)

// ### Creating a one-time pad
const otp = createOnetimePad(96)
console.log('One-time pad:        ' + otp)

// ### Checking length of plaincode vs. one-time pad
const lengthObj = checkLength(plaincodeConverted, otp)
console.log('Length:              ' + JSON.stringify(lengthObj))

// ### Encrypting plaincode
const encryptedMsg = encryptPlaincode(plaincodeConverted, otp)
console.log('Encrypted plaincode: ' + encryptedMsg.join(''))

// ### Decrypting encrypted message
const decryptedPlaincode = decryptEncryptedMsg(encryptedMsg.join(''), otp)
console.log('Decrypted plaincode: ' + decryptedPlaincode.join(''))

// ### Plaincode to text - The message delivered!
const textConverted = plaincodeToText(decryptedPlaincode.join(''), nob, codebook)
console.log('Decrypted msg:       ' + textConverted + '\n\n')

When doing ESM-version of the library, run it with:

node --experimental-json-modules index.mjs

API

textToPlaincode()

Converts plaintext to plaincode. Plaincode is just numbers, and not encrypted. It's a step that uses a conversion table to change the text, numbers and emojis into numbers, which makes it possible to do one-time-pad encryption.

textToPlaincode(text, conversionLanguage, codebook)
// Returns plaincode string from a string of text.

plaincodeToText()

Converts plaincode back to plaintext.

plaincodeToText(plaincode, conversionLanguage, codebook)
// Returns text string from plaincode string.

createOnetimePad()

The length of the should be equal to or larger than your plaincode. And it should only be used once. This ensures that it is impossible to break the code and read the encrypted message.

createOnetimePad(length)
// Returns a one-time pad of desired length, as a string of digits.

checkLength()

Helper function to check if plaincode length (and thus your message length) is too long, and also show the user how close they are to exceed length of one-time pad.

checkLength(plaincode, otp)
// Returns { plaincodeLength: plaincodeLength, otpLength: otpLength, tooLong: tooLong }

encryptPlaincode()

Encrypt the plaincode using a one-time-pad.

encryptPlaincode(plaincode, otp)
// Returns encrypted message as an string of digits. This is the encrypted message.

decryptEncryptedMsg()

Decrypts the encrypted message with the same one-time-pad that it was encrypted with.

decryptEncryptedMsg(encryptedMsg, otp)
// Returns message as a string of digits - The message in plainccode.

Language conversion tables, regular expressions and codebook

Each language contains variables for conversion tables and regular expressions. Most used letters differs from language to language. To be able to keep the plaincode short and thus needing shorter one-time-pads, the five most used letters are assigned to 0-5 in plaincode. Numbers starts with the digit 9 and consists of 3 digits.

The table is used for converting letters, digits and emojis to plaincode and the other way around. There are two regular expressions for each language. One is to split up text strings containing text, numbers and emojis into single letters, digits and emojis. The other one is to split up a plaincode-string into an array of plaincodes so that you it can use the conversion table to get a plaincode-string to a text-string (text, numbers and emojis).

For each language three variables/arrays are available

[language-code].table
[language-code].textRegex
[language-code].plaincodeRegex

Language codes

  • eng - English
  • nob - Norwegian

If you need it we can helpo add more languages.

Layout of conversion table

  • 00000 - 09999: Codebook, which consists of Unicode emojis
  • 1 - 5: 5 most used letters for this language
  • 60 - 89: Other letters and symbols
  • 900 - 909: Numbers from 0-9
  • 91 -99: More symbols

[language-code].table

Example from eng. It differs from each language depending on the what's the most used letter, and how many letter the alphabet consists of.

table: [
    { unicode: 'a', plaincode: '1' },
    { unicode: 'e', plaincode: '2' },
    { unicode: 'i', plaincode: '3' },
    { unicode: 'n', plaincode: '4' },
    { unicode: 'o', plaincode: '5' },
    { unicode: 't', plaincode: '60' },
    { unicode: 'b', plaincode: '61' },
    { unicode: 'c', plaincode: '62' },
    { unicode: 'd', plaincode: '63' },
    { unicode: 'f', plaincode: '64' },
    { unicode: 'g', plaincode: '65' },
    { unicode: 'h', plaincode: '66' },
    { unicode: 'j', plaincode: '67' },
    { unicode: 'k', plaincode: '68' },
    { unicode: 'l', plaincode: '69' },
    { unicode: 'm', plaincode: '70' },
    { unicode: 'p', plaincode: '71' },
    { unicode: 'q', plaincode: '72' },
    { unicode: 'r', plaincode: '73' },
    { unicode: 's', plaincode: '74' },
    { unicode: 'u', plaincode: '75' },
    { unicode: 'v', plaincode: '76' },
    { unicode: 'w', plaincode: '77' },
    { unicode: 'x', plaincode: '78' },
    { unicode: 'y', plaincode: '79' },
    { unicode: 'z', plaincode: '80' },
    { unicode: ',', plaincode: '84' },
    { unicode: '@', plaincode: '85' },
    { unicode: '#', plaincode: '86' },
    { unicode: '+', plaincode: '87' },
    { unicode: '-', plaincode: '88' },
    { unicode: '/', plaincode: '89' },
    { unicode: '0', plaincode: '900' },
    { unicode: '1', plaincode: '901' },
    { unicode: '2', plaincode: '902' },
    { unicode: '3', plaincode: '903' },
    { unicode: '4', plaincode: '904' },
    { unicode: '5', plaincode: '905' },
    { unicode: '6', plaincode: '906' },
    { unicode: '7', plaincode: '907' },
    { unicode: '8', plaincode: '908' },
    { unicode: '9', plaincode: '909' },
    { unicode: '.', plaincode: '91' },
    { unicode: ':', plaincode: '92' },
    { unicode: '\'', plaincode: '93' },
    { unicode: '!', plaincode: '94' },
    { unicode: '(', plaincode: '95' },
    { unicode: ')', plaincode: '96' },
    { unicode: '=', plaincode: '97' },
    { unicode: '?', plaincode: '98' },
    { unicode: ' ', plaincode: '99' }
  ]

[language-code].textRegex

Example from eng. It differs a little bit for each language.

eng.textRegex: '[a-z0-9\\s]|[,@#+-/.:!(=?)]'

[language-code].plaincodeRegex

Example from eng which for latin character based languages should be mostly the same.

eng.plaincodeRegex: '0\\d{4}|[1-5]|(90[0-9]{1})|(6[0-9]{1})|(7[0-9]{1})|(8[0-9]{1})|(9[1-9]{1})'

codebook

  • 00000 - 09999: Unicode emojis

Codebook for emojis. Not language specific. Starts with a 0 in plaincode and then 4 digits. Traditionallhy it has been used to be able to write shorter messages, having a short code for longer, often used words. Here it is to be able to express all Unicode emojis.

Example of three first entries:

codebook: [
  {
    unicode: 'πŸ˜€',
    plaincode: 00000
  },
  {
    unicode: 'πŸ˜ƒ',
    plaincode: 00001
  },
  {
    unicode: 'πŸ˜„',
    plaincode: 00002
  }
]

Maintenance

If unicode emojis are updated (to i.e. v.16 from v.15)

cd scipts
node ./fetch-emojis.mjs && node ./create-emoji-regex.mjs && node ./create-emoji-codebook.mjs

Possible issue

  • It's an untested toy. Don't bet your life on it. But it can be used to teach kids and minors about the importance of encryption. Discussion about the library and one-time-pad encryption on Reddit.
  • Exchanging one-time-pads is a problem. May be tackled with nfc-json-transfer.
  • I haven't found a unique way of numbering/addressing the unicode emojis that will work cross unicode emoji versions, so then stuff won't encrypt/decrypt properly if you use different versions of the library.

onetimepadjs's People

Contributors

dependabot[bot] avatar eklem avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Forkers

manniru

onetimepadjs's Issues

function: encryption

Function takes the following arguments:

  • message
  • one-time pad
  • plain-code conversion table
  • codebook (unicode emoji codebook)

What should the function do:

  • Convert text (+ emojis) to plaincode
  • Check that plaincode is not too long
  • Encrypt plaincode to encrypted text

Better codebook JSON maintenance

Need to read existing .json and new .txt-file and check if there are any new emjis in the .txt-file to add. Then the existing ones will keep their ID or plaincode if you will.

Warning: "Missing global variable names"

(!) Missing global variable name
Use output.globals to specify browser global variable names corresponding to external modules
crypto (guessing 'require$$0$1')

browser demo

UMD based

  • encrypt
  • decrypt
  • generate OTP or paste in existing
  • Show if plaincode is too long

Document functions

Not that many, so readme.md will do fine. Expected input and output

  • Text to plaincode
  • Plaincode to text
  • Check that message is not longer than encryption key/one-time pad
  • languages txt <-> plaincode table + reflex
  • Emoji codebook
  • One-time pad generator
  • Encrypt
  • Decrypt
  • Add OTP-ID to OTP and generate many keys

function: decryption

Function takes the following arguments:

  • encrypted text
  • one-time pad
  • plain-code conversion table
  • codebook (unicode emoji codebook)

What should the function do:

  • Convert encrypted text (+ emojis) to plaincode
  • Decrypt plaincode to encrypted text

unicode emoji codebook

Original use for this has been often used, strategic words. To be able to get more info into a short message. Has been consisting of control character (0) + 3 digit number.

For emojis, it could be good with a control character (0) + 5 digit number since you have almost 2000 unicode emojis + skintones for a lot of them by now.

Use the unicode number and pad it with zeros. The original smiley 1 will then be 00001. The first 0 is the codebook prefix.

Plain code conversion tables

List / table for converting back and forth from plaintext to plaincode

Start with english and norwegian

Since it's all digital, there the logic of encrypting/decrypting are a little different. Less errors, more space etc.

  • figures (digits) don't need to be written out three times.
  • when marking a digit: , not shown character as a space, since numbers can be mixed with letters. This is not WW2 but kids language. This means it needs to be a 90 before each single figure/digit.
  • If creating one for Norwegian, only 5 letters will fit on the first row, since the alphabet has three more letters (Γ¦, ΓΈ and Γ₯)
  • Longer one-time pads are okay as long as not stored on RFID-chips. Instead of 250, maybe 1000 or 1024?
  • Skipping one number for both opening and closing parenthesis

plaincode conversion table for English

  • 0 - CODE, use for i.e. emojis/smileys. If I need a codebook, it's for this
  • 1,2,3,4 and 5 - most frequent letters, aeino
  • 60-80 - tbcdfghjklmpqrsuvwxyz
  • 85 - @
  • 86 - #
  • 87 - +
  • 88 - -
  • 89 - /
  • 90 - to mark a coming figure (digit)
  • 91 - .
  • 92 - :
  • 93 - '
  • 94 - !
  • 95 - (
  • 96 - )
  • 97 - =
  • 98 - ?
  • 99 - SPACE

Not sure what to do with 81, 82, 83 and 84, maybe some of these could be good: &%*^Β§

plaincode conversion table for Norwegian

  • 0 - CODE, use for i.e. emojis/smileys. If I need a codebook, it's for this
  • 1,2,3,4 and 5 - most frequent letters, ertns
  • 60-83 - ialodgkmvpfuhΓ₯bjΓΈycΓ¦wxzq
  • 84 - *
  • 85 - @
  • 86 - #
  • 87 - +
  • 88 - -
  • 89 - /
  • 90 - to mark a coming figure (digit)
  • 91 - .
  • 92 - :
  • 93 - '
  • 94 - !
  • 95 - (
  • 96 - )
  • 97 - =
  • 98 - ?
  • 99 - SPACE

Maintenance: Function for writing emoji-regex to js-file

Check out how it's done at emoji-regex library:

build.js

const fs = require('fs');

const input = fs.readFileSync('./src/index.js', 'utf8').toString().trim();
const pattern = fs.readFileSync('./node_modules/emoji-test-regex-pattern/dist/latest/javascript.txt', 'utf8').toString().trim();
const output = input.replace('<% pattern %>', pattern) + '\n';

fs.writeFileSync('./index.js', output);

index.js

module.exports = () => {
	// https://mths.be/emoji
	return /<% pattern %>/g;
};

Will fix the unicode property escapes bug.

Document conversion tables + regular expressions

General layout of conversion table:

  • 00000 - 09999:
    Codebook, which consists of Unicode emojis
  • 1 - 5:
    5 most used letters for this language
  • 60 - 89:
    Other letters and symbols
  • 900 - 909:
    Numbers from 0-9
  • 91 -99:
    More symbols

textRegex for English. It differs a little bit for each language.

eng.textRegex: '[a-z0-9\\s]|[,@#+-/.:!(=?)]'

plaincodeRegex for English, which for latin character based languages should be mostly the same.

eng.plaincodeRegex: '0\\d{4}|[1-5]|(90[0-9]{1})|(6[0-9]{1})|(7[0-9]{1})|(8[0-9]{1})|(9[1-9]{1})'

Function: text - plaincode conversion

Function takes:

  • plaincode conversion table
  • emoji codebook
  • text

Does:

  • split up text in array of characters
  • convert according to joined table (plaincode conversion table and emoji codebook)

Guessing plaincode conversion table needs to be an array of objects, where each object key is the plaincode character

Function: plaincode to text converter

Function takes:

  • plaincode conversion table
  • emoji codebook array (key: id-number + value: emoji)
  • text

Does:

  • Checks that it's all numbers.
  • split up plaincode into blocks of numbers according to plaincode conversion table. If 0, grab the next 5 numbers, if 1 - 5, just take that number, if 60 - 79, etc. Regex with look behind? Do stuff like 0\d{5} to get the numbe for an emoji
  • convert according to joined table (plaincode conversion table and emoji codebook

Maybe the codebook needs to have regex'es to go from plaincode to numbers (1, 2, 5)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.