|
<html> |
|
<head> |
|
<!-- The following package is required https://www.npmjs.com/package/simple-free-encryption-tool --> |
|
|
|
<script src="js/windowSfet.js"></script> |
|
<script language="javascript"> |
|
// SECURITY_LEVEL is the encryption key size in bits |
|
let SECURITY_LEVEL = 2048; |
|
// internet explorer can't handle 2048 bit key generation in a reasonable amount of time, so we use 1024 bit. |
|
// this will have minimal impact as the credentials are secured using an externally transmitted verification |
|
// code and cracking the client->server comms won't (usually) compromise server->client comms |
|
// if client->server comms being compromised is a serious problem, then simply force the user to wait |
|
if ((window.navigator.userAgent.indexOf('MSIE') > 0) || |
|
(window.navigator.userAgent.indexOf('Trident/7') > 0) || |
|
(window.navigator.userAgent.indexOf('Edge/') > 0)) { |
|
SECURITY_LEVEL = 1024; |
|
} |
|
|
|
// RSA keys used to secure the session |
|
let keys = { |
|
client: {}, |
|
server: {} |
|
}; |
|
|
|
// generate the client's keys for the session |
|
function generateSessionKeys { |
|
console.log('generating ' + SECURITY_LEVEL + '-bit key pair...'); |
|
keys.client = sfet.rsa.generateKeysSync(SECURITY_LEVEL); |
|
console.log('Keys Generated in ' + keys.client.time); |
|
} |
|
|
|
// load existing session keys from storage or generate new keys |
|
function loadSessionKeys() { |
|
// ensure html5 storage available |
|
if (typeof (Storage) !== "undefined") { |
|
|
|
if (sessionStorage.RSAKeys) { |
|
keys = JSON.parse(sessionStorage.RSAKeys); |
|
console.log('client keys loaded from session storage'); |
|
} else { |
|
generateSessionKeys(); |
|
sessionStorage.RSAKeys = JSON.stringify(keys); |
|
console.log('session keys saved to storage'); |
|
} |
|
} else { |
|
console.log('Sorry! No Web Storage support..'); |
|
// it's possible to continue with new keys generated per page, |
|
// but then you'll have to repeat the key exchange with a new code |
|
} |
|
} |
|
|
|
function packMessageData(data) { |
|
data = JSON.stringify(data); |
|
let packedData = {}; |
|
// generate aes secret |
|
let aesSecret = sfet.utils.randomstring.generate(); |
|
try { |
|
// add RSA-encrypted aes secret to output |
|
packedData.key = sfet.rsa.encrypt(keys.server.public, aesSecret); |
|
// add encrypted data to output |
|
packedData.encrypted = sfet.aes.encrypt(aesSecret, data); |
|
return packedData; |
|
} catch (dataEncryptionException) { |
|
console.log('failed to pack message: ' + dataEncryptionException.message); |
|
return {}; |
|
} |
|
} |
|
|
|
function unpackMessageData(data) { |
|
var secret = sfet.rsa.decrypt(keys.client.private, data.key); |
|
var message = JSON.parse(sfet.aes.decrypt(secret, data.encrypted)); |
|
} |
|
</script> |
|
</head> |
|
<body> |
|
</body> |
|
</html> |
This code still works with the new versions of the libraries used? You should specify the version used. It would be nice to have an example on how to use it. Currently I'm trying to encrypt/decrypt a json only on fron-end using the packMessageData/unpackMessageData to test this example and it only gives me non-redable string like this
熡yB6 A文+ķǓ1z-t-\m_ѰO@3}y(ꂛ/*堺i鷍%婅b꺦㷬뮡D䥭趣k 㱸Ń͞A6_s녻囀