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

ECC(Ellipse Curve Cryptography)+AES(Advanced Encryption Standard) Front end communication encryption simulation ( Use eccrypto-js) More articles about

  1. AES advanced encryption standard

    // advanced encryption standard // author: karl malbrain, [email protected] typedef unsigned char u ...

  2. AES advanced encryption standard 3

    This optimized <../aesbench/> AES implementation conforms to FIPS-. aes.h #ifndef _AES_H #defi ...

  3. AES advanced encryption standard 2

    /* * FIPS-197 compliant AES implementation * * Copyright (C) 2006-2007 Christophe Devine * * Redistr ...

  4. java AES Encryption and decryption tools (Advanced Encryption Standard) It is found that the plaintext is the same, but the ciphertext will be different every time the service is restarted, so it has been improved

    1. Common methods package com.qlkj.hzd.commom.utils; import javax.crypto.*; import java.io.UnsupportedEncodingE ...

  5. Advanced encryption standard ( English :Advanced Encryption Standard, abbreviation :AES)

    2006 year , Advanced encryption standard has become one of the most popular algorithms in symmetric key encryption .

  6. php AES cbc Pattern pkcs7 128 Bit encryption and decryption ( Wechat applet )

    PHP AES CBC Pattern PKCS7 128 Bit encryption encryption : $key = '1234567812345678'; $iv = '1234567890123456'; $message = '12345 ...

  7. Multithreading System.Security.Cryptography.Aes CreateDecryptor newspaper “Safe handle has been closed” Solutions for

    Because the system needs to preload some core data to ensure the query speed . So in application_start Event enables the background thread to load and decrypt the relevant data ( In order to ensure the efficiency of decryption , take AES Make a static object to save :pr ...

  8. At present, the commonly used encryption algorithms are DES(Data Encryption Standard) and IDEA(International Data Encryption Algorithm) International data encryption algorithm, etc , Please use the factory method to implement the encryption algorithm system . Submit the code of the system , The system must be a system that can be used directly , Consult the data to complete the implementation of the corresponding encryption algorithm ;

    1. Class diagram structure of encryption algorithm 2. Source code 2.1 Screenshot of code running 2.2 The directory structure of the code 2.3 Specific code MethodFactory.java package jiami; public interface Me ...

  9. DES(Data Encryption Standard) Data encryption standard

    DES Algorithm entry parameters DES There are three entry parameters of the algorithm :Key.Data.Mode. among Key by 7 Bytes in total 56 position , yes DES Working key of algorithm .Data by 8 Bytes 64 position , It's data to be encrypted or decrypted ;Mode by DES Work of ...

  10. base64,AES,RSA,SHA and MD5 And so on (jdk)

    import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; import javax.crypto.Cipher; import jav ...

Random recommendation

  1. Life and technology are compatible :( One )Win7 Use Microsoft SkyDrive Net disk simple tutorial

    I have to write one today Win7 Use Microsoft SkyDrive The simple course of net disk , Mainly for my wife , By the way, post it for you to share :)     Use Microsoft SkyDrive The network disk has two levels . One is to use the web version , This is the same as using 163 perhaps QQ Network disk ...

  2. ASP A possible vulnerability in (Server.execute)

    author: bin <% Server.execute(request(“file”)) %> And include The difference between , It can dynamically contain files . Executable in the included file ASP Code , Sources abroad ...

  3. Sqoop Import MySQL data

    Import all tables : sqoop import-all-tables –connect jdbc:mysql://ip:port/dbName --username userName --password p ...

  4. SQL Preliminary notes

    DataBase Management Stystem( Database management system ) abbreviation :DBSM: although DBSM It's not the same as a database , But the industry will usually DBSM It's called a database , So generally speaking, database means DBSM. simply DB ...

  5. Linux Run different versions of qt Program

    Because of the needs of the project , There may be different versions of qt The program runs on the same machine , This experiment is qt4.8.5 and qt5.3.1 The developed program runs on the same machine at the same time , This machine may not follow qt Any version of , Of course , Two versions of qt And the number of digits of the machine ...

  6. PHPthinking Official forum

    PHPthinking The official forum was officially launched for a month ! At the moment . We already have hundreds of regular users . The forum has developed rapidly , All share some technical posts , We hope , Many other users added to , Create learning . Communication is more convenient . Rich content PHP forum ! PHPt ...

  7. assembly debug And masm command

    Assembly language is what I wrote on Netease blog before , But that account is basically invalid , So take time to move the blog to CSDN On . Assembly command ( compiler masm command ): find masm Folder in which , Mine is d:\MASM in , use cm ...

  8. git reset --hard xxxxxxx

    About git reset --hard xxxxxxx Order Git Version rollback I'm a little confused when I write the code tonight , My fingers didn't work, and I knocked it out git reset --hard command , Then there's all the previous ...

  9. The next bigger number Next Greater Element

    2018-09-24 21:52:38 One .Next Greater Element I Problem description : problem solving : This question only needs to be nums2 The next larger number of elements in is passed through map preserved , Then go through it again nums ...

  10. Java Cast in

    for example , When the program needs to double The value of a type variable is assigned to a int Type variable , How to achieve it ? obviously , This conversion is not automatic ! because int The storage range of model is larger than that of double The small size of the model . At this point, you need to force type conversion to ...