crypto-html
v1.0.2
Published
Simple library that envelop native HTML `<input>`, `<select>` and `<textarea>` elements to encrypt its value. It can prevent js injection data theft or HTTP 'man-in-the-middle' attack.
Downloads
138
Maintainers
Readme
Crypto-HTML
Simple library that envelop native HTML <input>
, <select>
and <textarea>
elements to encrypt its value.
It can prevent js injection data theft or HTTP 'man-in-the-middle' attack.
document.getElementById('input').value
will return encrypted value
Library use JSEncrypt library for asymmetric encryption. Read more about private and public keys on their page.
Usage
Provide native tag with 'is' attribute.
- input
<input is='encrypted-input'>
- select
<select is='encrypted-select'> </select>
- textarea
<textarea is='encrypted-textarea'> </textarea>
// index.html
<head>
<script type="text/javascript" src="./node_modules/crypto-html/index.js"></script>
</head>
<body>
<script>
const PUBLIC_KEY = `-----BEGIN PUBLIC KEY-----
MY PUBLIC KEY
-----END PUBLIC KEY-----`
CryptoHTML(PUBLIC_KEY)
</script>
<input id="input" is='encrypted-input'>
</body>
// index.html
<body>
<script src="./init-crypto.js"></script>
<input id="input" is='encrypted-input'>
</body>
// JS init-crypto.js
const crypto = require('crypto-html')
const PUBLIC_KEY = `-----BEGIN PUBLIC KEY-----
MY PUBLIC KEY
-----END PUBLIC KEY-----`
crypto.CryptoHTML(PUBLIC_KEY)
Decrypting
You can decrypt value of your input on the server side. Crypto-HTML use JSEncrypt library for asymmetric encryption. You can study more about this library here
So to use it with NodeJS server you need install it.
npm i jsencrypt
then implement decryption as described on their page
// JS
const jsencrypt = require('jsencrypt')
const PRIVATE_KEY = `-----BEGIN RSA PRIVATE KEY-----
MY PRIVATE KEY
-----END RSA PRIVATE KEY-----`
const decrypt = new jsencrypt.JSEncrypt();
decrypt.setPrivateKey(PRIVATE_KEY)
// implement getting of encrypted valye from client side
const uncrypted = decrypt.decrypt(encryptedValue)