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
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 .
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 .
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 .
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());