Rules for generating private key and public key of Ethereum Wall
Publish: 2021-04-28 15:48:03
1. I'll give you my address (address). You can find my zip code (public key). You can write to me with my zip code (public key) + address. The mail will be sent to my mail cabinet. I'll open the mail cabinet (private key) with the key I have. The key of express cabinet is stored in my wallet (wallet)
1. The mail cabinet is stolen (the database is stolen)
2. The key is stolen (the private key is stolen)
3. Knowing my home address (the public key is stolen), the lock of the mail cabinet is opened violently (the private key is cracked violently).
1. The mail cabinet is stolen (the database is stolen)
2. The key is stolen (the private key is stolen)
3. Knowing my home address (the public key is stolen), the lock of the mail cabinet is opened violently (the private key is cracked violently).
2. If it's lost, it's better to post the lost advertisement
3. Use OpenSSL, which comes with Linux. Common commands are as follows: - generate RSA private key (traditional format) OpenSSL genrsa - out RSA_ private_ Key.pem 1024 -- convert the traditional private key format to OpenSSL pkcs8 - topk8 - Information PEM - in RSA in PKCs × 8 format_ private_ Key. PEM - outform PEM - nocrypt -- generate RSA public key OpenSSL RSA - in RSA_ private_ key.pem -pubout -out rsa_ public_ key.pem
4. To achieve secure login, we can use the following three methods, one based on asymmetric encryption algorithm, one based on symmetric encryption algorithm, and the last one based on hash algorithm. Let's discuss these three methods separately
among the asymmetric encryption algorithms, RSA algorithm and ECC (elliptic curve encryption) algorithm are the most commonly used. In order to achieve secure login by using asymmetric encryption algorithm, firstly, when the client requests the login page from the server, the server generates the public key and private key, and then passes the public key along with the login page to the client browser. When the user enters the user name and password and clicks login, the public key and private key are generated by the server, JavaScript in login page calls asymmetric encryption algorithm to encrypt user name and password with public key. Then it is submitted to the server. The server decrypts it with the private key, and compares it with the user name and password in the database. If it is consistent, the login is successful. Otherwise, the login fails
it looks simple, but here are a few questions. At present, the key of 1024-2048 bits is considered to be secure in RSA algorithm. If the key length is less than this length, it is considered that it can be cracked. But this length is beyond the range of digital operation allowed by programming language itself, so it needs to realize large number operation by simulation. On the client side of the web system, if JavaScript is used to simulate the running of large numbers, the efficiency will be very low. Therefore, if such a key is used to encrypt data on the client side, many browsers will issue a warning that the execution time is too long and stop running. However, decryption or key generation takes longer than encryption. Although decryption and key generation are executed on the server side, if the server side is a scripting language such as PHP and ASP, they will not be competent for such work. The key length requirement of ECC algorithm is lower than that of RSA algorithm. The 160 bit key length of ECC algorithm is considered to be equivalent to the 1024 bit key length of RSA algorithm. Although the analog large number operation still needs to be involved, the computation amount of key length of ECC algorithm is acceptable, but ECC algorithm is much more complex than RSA algorithm, so it is difficult to implement
symmetric encryption algorithm is much faster than asymmetric encryption algorithm, but symmetric encryption algorithm requires the sender and receiver of data to share a key, and the key can not be directly transmitted through an insecure network, otherwise the key and the encrypted data can be monitored at the same time, The intruder can directly use the monitored key to decrypt the encrypted information
is it impossible to achieve secure login through symmetric encryption algorithm? In fact, as long as the key exchange algorithm can achieve secure login, the commonly used key exchange algorithm is Diffie Hellman key exchange algorithm. First, when the client requests the login page from the server, the server generates a large prime number P, its primitive root g, and a random number Xa, then calculates Ya = GXA mod p, sends P, G, Ya together with the login page to the client, and the client also generates a random number XB, Calculate Yb = gxb mod p, and then calculate k = yaxb mod P. now K is the key. Next, you can use K as the key to encrypt the user's input with symmetric encryption algorithm, and then send the encrypted information together with the calculated Yb to the server. The server calculates k = ybxa mod p, so you can get the same key K as the client, Finally, with the corresponding decryption algorithm of the client encryption algorithm, the encrypted information can be decrypted on the server. After decryption, the information is compared. If it is consistent, the login is successful, otherwise the login fails. Note that the random number XA generated by the server and the random number XB generated by the client are not passed to each other. Only P, G, ya, Yb and encrypted data are transmitted
however, if we use hash algorithm instead of encryption algorithm to process the login password, we can avoid directly decrypting the original text. However, if we use MD5 or SHA1 to process the login password and submit it, once the intruder listens to the hashed password, it is not necessary to decrypt the original text and submit the monitored data to the server directly, Then we can achieve the purpose of intrusion. Moreover, MD5 algorithm has been cracked, SHA1 algorithm has been proved to be cracked in theory, even if offline collision is used, the password equivalent to the original password can be found. So it is not feasible to hash the password directly with MD5 or SHA1
however, if a key is added to the hash algorithm, the situation will be different. HMAC algorithm just does this. Let's see how to use HMAC algorithm to achieve secure login. First, when the client requests the login page from the server, the server generates a random string, which is sent to the client browser together with the login page. After the user enters the user name password, the password uses MD5 or SHA1 to generate hash value as the key, and the random string sent by the server is used as the message data for HMAC operation. The results are then submitted to the server. The reason why we need to hash the user's password and then use it as the key instead of directly using it as the key is to ensure that the key is long enough and not too long. After the server receives the data submitted by the client, it performs the same operation on the random string stored in the server and the user password, and then compares them. If the results are consistent, the login is considered successful, otherwise, the login fails. Of course, if you don't use the HMAC algorithm, you can combine the password with the random number generated by the server and then do MD5 or SHA1
here, the random string sent by the server is different every time the client requests, so even if the intruder listens to the random string and the encrypted submitted data, it cannot submit the same data again to pass the verification. Moreover, the key cannot be calculated from the monitored data, so the login information cannot be forged
symmetric and asymmetric encryption algorithms are not only suitable for login verification, but also for initial password setting and subsequent password modification, while hash algorithm is only suitable for login verification. But hash algorithm is more efficient than symmetric and asymmetric encryption algorithm.
among the asymmetric encryption algorithms, RSA algorithm and ECC (elliptic curve encryption) algorithm are the most commonly used. In order to achieve secure login by using asymmetric encryption algorithm, firstly, when the client requests the login page from the server, the server generates the public key and private key, and then passes the public key along with the login page to the client browser. When the user enters the user name and password and clicks login, the public key and private key are generated by the server, JavaScript in login page calls asymmetric encryption algorithm to encrypt user name and password with public key. Then it is submitted to the server. The server decrypts it with the private key, and compares it with the user name and password in the database. If it is consistent, the login is successful. Otherwise, the login fails
it looks simple, but here are a few questions. At present, the key of 1024-2048 bits is considered to be secure in RSA algorithm. If the key length is less than this length, it is considered that it can be cracked. But this length is beyond the range of digital operation allowed by programming language itself, so it needs to realize large number operation by simulation. On the client side of the web system, if JavaScript is used to simulate the running of large numbers, the efficiency will be very low. Therefore, if such a key is used to encrypt data on the client side, many browsers will issue a warning that the execution time is too long and stop running. However, decryption or key generation takes longer than encryption. Although decryption and key generation are executed on the server side, if the server side is a scripting language such as PHP and ASP, they will not be competent for such work. The key length requirement of ECC algorithm is lower than that of RSA algorithm. The 160 bit key length of ECC algorithm is considered to be equivalent to the 1024 bit key length of RSA algorithm. Although the analog large number operation still needs to be involved, the computation amount of key length of ECC algorithm is acceptable, but ECC algorithm is much more complex than RSA algorithm, so it is difficult to implement
symmetric encryption algorithm is much faster than asymmetric encryption algorithm, but symmetric encryption algorithm requires the sender and receiver of data to share a key, and the key can not be directly transmitted through an insecure network, otherwise the key and the encrypted data can be monitored at the same time, The intruder can directly use the monitored key to decrypt the encrypted information
is it impossible to achieve secure login through symmetric encryption algorithm? In fact, as long as the key exchange algorithm can achieve secure login, the commonly used key exchange algorithm is Diffie Hellman key exchange algorithm. First, when the client requests the login page from the server, the server generates a large prime number P, its primitive root g, and a random number Xa, then calculates Ya = GXA mod p, sends P, G, Ya together with the login page to the client, and the client also generates a random number XB, Calculate Yb = gxb mod p, and then calculate k = yaxb mod P. now K is the key. Next, you can use K as the key to encrypt the user's input with symmetric encryption algorithm, and then send the encrypted information together with the calculated Yb to the server. The server calculates k = ybxa mod p, so you can get the same key K as the client, Finally, with the corresponding decryption algorithm of the client encryption algorithm, the encrypted information can be decrypted on the server. After decryption, the information is compared. If it is consistent, the login is successful, otherwise the login fails. Note that the random number XA generated by the server and the random number XB generated by the client are not passed to each other. Only P, G, ya, Yb and encrypted data are transmitted
however, if we use hash algorithm instead of encryption algorithm to process the login password, we can avoid directly decrypting the original text. However, if we use MD5 or SHA1 to process the login password and submit it, once the intruder listens to the hashed password, it is not necessary to decrypt the original text and submit the monitored data to the server directly, Then we can achieve the purpose of intrusion. Moreover, MD5 algorithm has been cracked, SHA1 algorithm has been proved to be cracked in theory, even if offline collision is used, the password equivalent to the original password can be found. So it is not feasible to hash the password directly with MD5 or SHA1
however, if a key is added to the hash algorithm, the situation will be different. HMAC algorithm just does this. Let's see how to use HMAC algorithm to achieve secure login. First, when the client requests the login page from the server, the server generates a random string, which is sent to the client browser together with the login page. After the user enters the user name password, the password uses MD5 or SHA1 to generate hash value as the key, and the random string sent by the server is used as the message data for HMAC operation. The results are then submitted to the server. The reason why we need to hash the user's password and then use it as the key instead of directly using it as the key is to ensure that the key is long enough and not too long. After the server receives the data submitted by the client, it performs the same operation on the random string stored in the server and the user password, and then compares them. If the results are consistent, the login is considered successful, otherwise, the login fails. Of course, if you don't use the HMAC algorithm, you can combine the password with the random number generated by the server and then do MD5 or SHA1
here, the random string sent by the server is different every time the client requests, so even if the intruder listens to the random string and the encrypted submitted data, it cannot submit the same data again to pass the verification. Moreover, the key cannot be calculated from the monitored data, so the login information cannot be forged
symmetric and asymmetric encryption algorithms are not only suitable for login verification, but also for initial password setting and subsequent password modification, while hash algorithm is only suitable for login verification. But hash algorithm is more efficient than symmetric and asymmetric encryption algorithm.
5.
To use OpenSSL on Ubuntu, you need to install it first. The command is as follows:
sudo apt get install OpenSSL
after the installation, you can use OpenSSL
first, you need to enter the interactive interface of OpenSSL and enter OpenSSL on the command line
1) generate RSA private key:
genrsa - out RSA_ private_ Key.pem 1024
this command will generate a 1024 bit private key. The successful interface is as follows:
3) generate RSA public key
input the command RSA - in RSA_ private_ key.pem -pubout -out rsa_ public_ Key.pem and enter
6. If you want to use OpenSSL on Ubuntu, you need to install it first. The command is as follows: sudo apt get install OpenSSL. After installation, you can use OpenSSL. First of all, you need to enter the interactive interface of OpenSSL and enter OpenSSL on the command line; 1) Generating RSA private key: genrsa - out RSA_ private_ Key.pem 1024 this command will generate a 1024 bit private key. The successful interface is as follows: at this time, we can see RSA in the current path_ private_ The key.pem file is missing. 2) Convert RSA private key to pkcs8 format and input pkcs8 - topk8 - Information PEM - in RSA_ private_ Key.pem - outform PEM – nocrypt, and enter to get the result of generating success, which is the private key in pkcs8 format, as shown in the following figure: 3) generate RSA public key input command RSA - in RSA_ private_ key.pem -pubout -out rsa_ public_ Key.pem, and enter to get the result of successful generation, as shown in the following figure: at this time, we can see a file named RSA_ public_ Key.pem file, open it, you can see the string without newline at the beginning of --- - begin public key --- - end public key ---. This is the public key.
7. This should be IC card intelligent parking lot charging management system, which is the general name of modern parking lot vehicle charging and equipment automatic management. The system organically combines machinery, electronic computer, automatic control equipment and intelligent IC card technology. Through computer management, it can realize vehicle image comparison, automatic charging, automatic data storage and other functions, And the parking lot management system can realize offline operation, in the case of computer failure can still ensure the normal access of vehicles, is an ideal facility for modern residential property management
the intelligent parking fee collection system is a set of network system which is built by computer, network equipment and lane management equipment to manage the vehicle access, traffic flow guidance and parking fee collection in the parking lot. It is a necessary tool for professional depot management company. It realizes the dynamic and static comprehensive management of vehicles in and out of the yard by collecting and recording the records of vehicles in and out of the yard and the location of the yard. The system generally takes the radio frequency inction card as the carrier, records the vehicle in and out information through the inction card, realizes the charging strategy through the management software, realizes the charging accounting management, Lane equipment control and other functions.
the intelligent parking fee collection system is a set of network system which is built by computer, network equipment and lane management equipment to manage the vehicle access, traffic flow guidance and parking fee collection in the parking lot. It is a necessary tool for professional depot management company. It realizes the dynamic and static comprehensive management of vehicles in and out of the yard by collecting and recording the records of vehicles in and out of the yard and the location of the yard. The system generally takes the radio frequency inction card as the carrier, records the vehicle in and out information through the inction card, realizes the charging strategy through the management software, realizes the charging accounting management, Lane equipment control and other functions.
8. OpenSSL gensa - DES3 - out server. Key 1024
when running, you will be prompted to enter a password. This password is used to encrypt the key file (parameter DES3 refers to the encryption algorithm, of course, you can choose other algorithms that you think are safe). In the future, you need to enter a password whenever you need to read this file (through the command or API provided by OpenSSL). If you find it inconvenient, you can also remove this password, But we must take other protective measures
command to remove the password of key file:
OpenSSL RSA - in server.key - out server.key!
when running, you will be prompted to enter a password. This password is used to encrypt the key file (parameter DES3 refers to the encryption algorithm, of course, you can choose other algorithms that you think are safe). In the future, you need to enter a password whenever you need to read this file (through the command or API provided by OpenSSL). If you find it inconvenient, you can also remove this password, But we must take other protective measures
command to remove the password of key file:
OpenSSL RSA - in server.key - out server.key!
9. /// < summary>
/ / / generate the private key and public key in XML format in the given path< br /> /// </ summary>< br /> public void GenerateKeys(string path)
/ / / generate the private key and public key in XML format in the given path< br /> /// </ summary>< br /> public void GenerateKeys(string path)
10. 以下命令来生成密钥对
$openssl genrsa -out mykey.pem 2048
$openssl pkcs8 -topk8 -inform PEM -outform PEM -in mykey.pem \
-out private_key.pem -nocrypt
这个命令得到的公共密钥
$ openssl rsa -in mykey.pem -pubout -outform DER -out public_key.der
我写了两方法读取私钥和公钥
分别public PrivateKey getPemPrivateKey(String filename, String algorithm) throws Exception {
File f = new File(filename);
FileInputStream fis = new FileInputStream(f);
DataInputStream dis = new DataInputStream(fis);
byte[] keyBytes = new byte[(int) f.length()];
dis.readFully(keyBytes);
dis.close();
String temp = new String(keyBytes);
String privKeyPEM = temp.replace("-----BEGIN PRIVATE KEY-----\n", "");
privKeyPEM = privKeyPEM.replace("-----END PRIVATE KEY-----", "");
//System.out.println("Private key\n"+privKeyPEM);
Base64 b64 = new Base64();
byte [] decoded = b64.decode(privKeyPEM);
PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(decoded);
KeyFactory kf = KeyFactory.getInstance(algorithm);
return kf.generatePrivate(spec);
}
public PublicKey getPemPublicKey(String filename, String algorithm) throws Exception {
File f = new File(filename);
FileInputStream fis = new FileInputStream(f);
DataInputStream dis = new DataInputStream(fis);
byte[] keyBytes = new byte[(int) f.length()];
dis.readFully(keyBytes);
dis.close();
String temp = new String(keyBytes);
String publicKeyPEM = temp.replace("-----BEGIN PUBLIC KEY-----\n", "");
publicKeyPEM = privKeyPEM.replace("-----END PUBLIC KEY-----", "");
Base64 b64 = new Base64();
byte [] decoded = b64.decode(publicKeyPEM);
X509EncodedKeySpec spec =
new X509EncodedKeySpec(decoded);
KeyFactory kf = KeyFactory.getInstance(algorithm);
return kf.generatePublic(spec);
}
$openssl genrsa -out mykey.pem 2048
$openssl pkcs8 -topk8 -inform PEM -outform PEM -in mykey.pem \
-out private_key.pem -nocrypt
这个命令得到的公共密钥
$ openssl rsa -in mykey.pem -pubout -outform DER -out public_key.der
我写了两方法读取私钥和公钥
分别public PrivateKey getPemPrivateKey(String filename, String algorithm) throws Exception {
File f = new File(filename);
FileInputStream fis = new FileInputStream(f);
DataInputStream dis = new DataInputStream(fis);
byte[] keyBytes = new byte[(int) f.length()];
dis.readFully(keyBytes);
dis.close();
String temp = new String(keyBytes);
String privKeyPEM = temp.replace("-----BEGIN PRIVATE KEY-----\n", "");
privKeyPEM = privKeyPEM.replace("-----END PRIVATE KEY-----", "");
//System.out.println("Private key\n"+privKeyPEM);
Base64 b64 = new Base64();
byte [] decoded = b64.decode(privKeyPEM);
PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(decoded);
KeyFactory kf = KeyFactory.getInstance(algorithm);
return kf.generatePrivate(spec);
}
public PublicKey getPemPublicKey(String filename, String algorithm) throws Exception {
File f = new File(filename);
FileInputStream fis = new FileInputStream(f);
DataInputStream dis = new DataInputStream(fis);
byte[] keyBytes = new byte[(int) f.length()];
dis.readFully(keyBytes);
dis.close();
String temp = new String(keyBytes);
String publicKeyPEM = temp.replace("-----BEGIN PUBLIC KEY-----\n", "");
publicKeyPEM = privKeyPEM.replace("-----END PUBLIC KEY-----", "");
Base64 b64 = new Base64();
byte [] decoded = b64.decode(publicKeyPEM);
X509EncodedKeySpec spec =
new X509EncodedKeySpec(decoded);
KeyFactory kf = KeyFactory.getInstance(algorithm);
return kf.generatePublic(spec);
}
Hot content
