current position:Home>ECC (ellipse curve cryptography) + AES (Advanced Encryption Standard) front end communication encryption simulation (using eccrypto JS)

ECC (ellipse curve cryptography) + AES (Advanced Encryption Standard) front end communication encryption simulation (using eccrypto JS)

2021-08-25 02:18:23 Durian Melaleuca

  Pre knowledge

If you don't know about symmetric encryption and asymmetric encryption, you can take a look at the following article , If you want to learn the encryption algorithms related to blockchain in detail, you can stamp here

Symmetric and asymmetric encryption

https://blog.csdn.net/u013320868/article/details/54090295

ECC

ECC Detailed explanation of elliptic curve ( There are specific examples ) - Kalafinaian - Blog Garden (cnblogs.com)

AES

Wikipedia :https://zh.wikipedia.org/wiki/%E9%AB%98%E7%BA%A7%E5%8A%A0%E5%AF%86%E6%A0%87%E5%87%86

ECC+AES

Alice to Bob Send a message ,Alice use AES secret key Key Encrypt the message to get the ciphertext M, use Bob Public key encryption AES Get your key Key', take M and Key' issue Bob,Bob Get it and decrypt it with your own private key Key' obtain Key, Reuse Key Decrypt M Get clear text .

Why can't you just use ECC Asymmetric encryption , Use ECC encryption AES The key of ?

If used alone AES, It's not easy to pass the key without encryption , It's easy to be intercepted in transmission .

If used alone ECC, Asymmetric encryption is slow 、 Encryption efficiency is low .

Code

ECC At present, the application of elliptic curve encryption algorithm is not very mature ,crypto-js There is no application in ECC Encryption function of .

adopt npm Search for eccrypto Will find some published by individuals package, Mostly involved JavaScript Medium Promise object , Unfamiliar friends can learn about Promise.

Promise - Liao Xuefeng's official website (liaoxuefeng.com)

I chose to use eccrypto-js(www.npmjs.com), This package Contains the implementation AES and ECC Function of , And the parameter types are buffer, If you can't include both, you have to import Different package, Parameter types, etc. may be incompatible , It will cause some trouble .

This example is an example of front-end encryption and decryption , It is suitable for end-to-end encryption in practical application , It does not involve the encryption and decryption of the server .

Because the article can only give one encryption example , There is no specific communication interface , So I use notes Alice To express Alice End ,Bob To express Bob End , And simulate Alice towards Bob The process of communication ,Bob towards Alice Communication is the reverse process , Don't go into details . The information obtained by encryption and decryption is output to the console .

PS:

1. In the code ,ECC Public and private keys and AES The keys are randomly generated , In actual production, the private key is usually associated with the wallet or account .

2. Encryption, decryption and communication are used buffer type , When displaying on the console, pay attention to the conversion to string.

import * as eccryptoJS from 'eccrypto-js'
// The parameters of the encryption and decryption function are buffer class 

//Alice
// A new random 32 Byte private key , The corresponding uncompressed private key (65 byte ) Public key .
const keyPairA = eccryptoJS.generateKeyPair();
console.info("privateKey:", keyPairA.privateKey);
console.info("publicKey:", keyPairA.publicKey);
//send publicKey to Bob

//Bob
// Random generation AES The key of 
const AESKey = eccryptoJS.randomBytes(32);
console.info("AESKey:", AESKey);
const iv = eccryptoJS.randomBytes(16);

// use ECC encryption AES secret key 
const EncryptedAESKey = await eccryptoJS.encrypt(keyPairA.publicKey, AESKey);
console.info("EncryptedAESKey:", EncryptedAESKey);

// Plaintext to be sent str, Turn into buffer Format 
const str = 'test message to encrypt';
const msg = eccryptoJS.utf8ToBuffer(str);

// Use unencrypted AES Key encrypts plaintext 
const ciphertext = await eccryptoJS.aesCbcEncrypt(iv, AESKey, msg);
console.info("ciphertext:", ciphertext);
//send  Ciphertext encrypt_str and AESKeyEncrypt to Alice,iv?

//Alice
// Decrypt the encrypted with the private key AES secret key 
const DecryptedAESKey = await eccryptoJS.decrypt(keyPairA.privateKey, EncryptedAESKey);
console.info("decryptedAESKey:", DecryptedAESKey);
// use AES Key decrypts plaintext 
const decrypted = await eccryptoJS.aesCbcDecrypt(iv, DecryptedAESKey, ciphertext);
console.info("decrypted:", decrypted.toString());

 

copyright notice
author[Durian Melaleuca],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2021/08/20210825021820067Q.html

Random recommended