RSA public key cryptosystem. The so-called public key cryptosystem uses different encryption keys and decryption keys, and is a cryptosystem that is " computationally infeasible by deriving a decryption key from a known encryption key."
In the public key cryptosystem, the encryption key (i.e., public key) PK is public information, and the decryption key (i.e., secret key) SK is required to be kept secret. Both the encryption algorithm E and the decryption algorithm D are also public. Although the decryption key SK is determined by the public key PK, the SK cannot be calculated from the PK.
It is based on this theory that the famous RSA algorithm appeared in 1978, which is usually a pair of RSA keys, one of which is a secret key, which is kept by the user; the other is a public key, which can be made public, even Can be registered in the web server. To increase the security strength, the RSA key is at least 500 bits long, and 1024 bits are generally recommended. This makes the amount of encryption computationally large. In order to reduce the amount of calculation, the traditional encryption method is combined with the public key encryption method when transmitting information, that is, the information is encrypted with an improved DES or IDEA session key, and then the session key and information are encrypted using the RSA key. Summary. After the other party receives the information, it decrypts with a different key and can check the message digest.
The RSA algorithm is the first algorithm that can be used for both encryption and digital signatures, and is easy to understand and operate. RSA is the most widely studied public key algorithm. It has been tested by various attacks for more than 30 years from now. It is widely accepted as the best public key solution in 2017. one.
Encryption process:A extracts the message digest h(m) of the message m, and encrypts the digest h(m) with its own private key to generate a signature s
A encrypts the signature s together with the message m using B's public key, generates ciphertext c, and sends it to B.
Decryption process :B receives the ciphertext c, decrypts c with its own private key to get the plaintext m and the digital signature s
B decrypts the digital signature s using A's public key to obtain H(m).
B uses the same method to extract the message digest of message m (m)
B compares two message digests. If the same, the verification is successful; if it is different, the verification fails.
RSA encryption process brief
When A and B perform encrypted communication, B first generates a pair of keys. One is the public key, and A and B hold the private key themselves. A uses B's public key to encrypt the content to be encrypted, and then B decrypts the content through its own private key.
The role of digital signatures is to ensure data integrity, confidentiality and non-repudiation of the sender's role.
Suppose A wants B to send a message, A will first calculate the message digest of the message, then encrypt the digest encryption with its own private key, and finally send the encrypted message digest together with the message to B. The encrypted message digest is "signature".
After receiving the message, B will also extract the message digest in the same way as A, and then use A's public key to decrypt the signature sent by A and compare it with the message digest calculated by himself. If they are the same, the message is sent by A to B. At the same time, A cannot deny the fact that he sent the message to B.
Among them, A encrypts the message digest with its own private key to become a "signature"; B uses A's public key to decrypt the signature file, which is called "checking".
RSA encryption and decryption code/// "summary"
/// RSA public key encryption
/// "/summary"
/// "param name="content"" content to be encrypted "/param"
/// "param name="publickey" public key "/param"
/// "returns" /returns
Public staTIc string EncryptByPublicKey(string content, string publickey)
{
Byte[] s = Convert.FromBase64String(publickey);
AsymmetricKeyParameter publicKey = PublicKeyFactory.CreateKey(s);
IBufferedCipher c = CipherUTIliTIes.GetCipher("RSA/ECB/PKCS1Padding");
//public key encryption
c.Init(true, publicKey);
Byte[] byteData = Encoding.UTF8.GetBytes(content);
byteData = c.DoFinal(byteData, 0, byteData.Length);
Return Convert.ToBase64String(byteData);
}
/// "summary"
/// RSA encryption
/// "/summary"
/// "param name="content"" encrypted plaintext "/param"
/// "param name="privatekey" private key "/param"
/// "returns" returns the ciphertext "/returns"
Public staTIc string RSAEncry(string content, string privatekey)
{
AsymmetricKeyParameter privateKey = PrivateKeyFactory.CreateKey(Convert.FromBase64String(privatekey));
IBufferedCipher c = CipherUtilities.GetCipher("RSA/ECB/PKCS1Padding");
//Private key encryption
c.Init(true, privateKey);
Byte[] byteData = Encoding.UTF8.GetBytes(content);
byteData = c.DoFinal(byteData, 0, byteData.Length);
Return Convert.ToBase64String(byteData);
}
/// "summary"
/// RSA decryption
/// "/summary"
/// "param name="content"" ciphertext "/param"
/// "param name="privatekey" private key "/param"
/// "returns" plain text "/returns"
Public static string RSADeEncry(string content, string privatekey)
{
Try
{
MemoryStream bufferStream = new MemoryStream();
Byte[] bytData = Convert.FromBase64String(content);
Int inputLength = bytData.Length;
AsymmetricKeyParameter privateKey = PrivateKeyFactory.CreateKey(Convert.FromBase64String(privatekey));
IBufferedCipher cipher = CipherUtilities.GetCipher("RSA/ECB/PKCS1Padding");
cipher.Init(false, privateKey);
Int offSet = 0;
Byte[] cache;
Int i = 0;
While (inputLength - offSet) 0)
{
If (inputLength - offSet) 128)
{
Cache = cipher.DoFinal(bytData, offSet, 128);
}
Else
{
Cache = cipher.DoFinal(bytData, offSet, inputLength - offSet);
}
bufferStream.Write(cache, 0, cache.Length);
i++;
offSet = i * 128;
}
Byte[] decryptedData = bufferStream.ToArray();
bufferStream.Close();
Return Encoding.UTF8.GetString(bufferStream.ToArray());
}
Catch (Exception e)
{
Return e.Message;
}
}
Smart Board For Conference,Whiteboard Smart Board,Interactive Whiteboard Smart Board,Smart Board Interactive Whiteboard
APIO ELECTRONIC CO.,LTD , https://www.displayapio.com