Skip to content

Instantly share code, notes, and snippets.

@rococtz
Created September 9, 2021 08:08
Show Gist options
  • Save rococtz/5b366cbd9e9a52dd7d56ce574f3dacd0 to your computer and use it in GitHub Desktop.
Save rococtz/5b366cbd9e9a52dd7d56ce574f3dacd0 to your computer and use it in GitHub Desktop.
encryption_one_to_many
// https://jsbin.com/rasuwilojo/6/edit?js,console
(async () => {
/////////////////////////////////////////////////////////////////
// Instead of logging in with actual users, we are
// going to generate SEA pairs which is basically the same thing
/////////////////////////////////////////////////////////////////
// User 1 encrypts one message
const user1 = await SEA.pair();
const plainMessage = 'Hello, how are you?';
const encryptionKey = 'this is my encryption key which is a normal string';
const encryptedMessage = await SEA.encrypt(plainMessage, encryptionKey);
// User 2, 3 and 4 will receive the message and decrypt it
const user2 = await SEA.pair();
const user3 = await SEA.pair();
const user4 = await SEA.pair();
// Each user gets an encrypted encryption key. If you print them, they all different
const encryptedEncryptionKeyUser2 = await SEA.encrypt(encryptionKey, await SEA.secret(user2.epub, user1));
const encryptedEncryptionKeyUser3 = await SEA.encrypt(encryptionKey, await SEA.secret(user3.epub, user1));
const encryptedEncryptionKeyUser4 = await SEA.encrypt(encryptionKey, await SEA.secret(user4.epub, user1));
// Each user decrypts his own encrypted encryption key
// These three decrypted encryptions keys that we get are all the same
const decryptedEncryptionKeyUser2 = await SEA.decrypt(
encryptedEncryptionKeyUser2,
await SEA.secret(user1.epub, user2)
);
const decryptedEncryptionKeyUser3 = await SEA.decrypt(
encryptedEncryptionKeyUser3,
await SEA.secret(user1.epub, user3)
);
const decryptedEncryptionKeyUser4 = await SEA.decrypt(
encryptedEncryptionKeyUser4,
await SEA.secret(user1.epub, user4)
);
// Each user decrypts the encrypted message using the decrypted encryption key
const decryptedMessageUser2 = await SEA.decrypt(encryptedMessage, decryptedEncryptionKeyUser2);
const decryptedMessageUser3 = await SEA.decrypt(encryptedMessage, decryptedEncryptionKeyUser3);
const decryptedMessageUser4 = await SEA.decrypt(encryptedMessage, decryptedEncryptionKeyUser4);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment