Java security-Public and private keys

Source: Internet
Author: User

Java security-Public and private keys
The public key and private key mechanisms belong to the category of asymmetric encryption. asymmetric encryption is relative to symmetric encryption. symmetric encryption is the same key for encryption and decryption, asymmetric encryption is used to encrypt and decrypt different keys. A public key is called a public key. A private key is called a private key. The public key and the private key must be paired, only the paired public key and private key can be used for encryption and decryption. The Public Key is issued through a non-secure channel, and the private key is retained by the publisher. The data encrypted by the Public Key can only be decrypted using its paired private key, you can only use the public key to decrypt it.

Symmetric encryption effectively improves the data security after it appears. However, since encryption and decryption use the same key, both parties must agree on an encryption algorithm, the data Sender also needs to send the key to the receiver, which makes management of the key inconvenient. To make up for this weakness, asymmetric encryption algorithms are generated. For asymmetric encryption, the Public Key itself is public. As we all know, there is no public key management problem. You only need to save the private key safely, this brings great convenience to secure communication.

Compared with object encryption algorithms, asymmetric encryption does not exist in key management and has an insurmountable security level. However, encryption/decryption efficiency is much lower than symmetric encryption, therefore, non-object encryption algorithms are often used in areas with high security requirements in Shenzhen, such as e-commerce platforms, bank gateways, and payment systems. Asymmetric encryption algorithms are inefficient. In many cases, symmetric encryption algorithms are combined with asymmetric encryption algorithms to encrypt/decrypt data, use the public key and private key to encrypt/decrypt the key of the object encryption algorithm. The efficiency of symmetric encryption algorithms and key management of non-object encryption algorithms are used to improve the security of the entire encryption system. In addition, in the design of algorithms, non-object encryption algorithms still have extremely strict requirements on the length of encrypted data. For example, the RSA algorithm requires that the data to be encrypted must not exceed 53 bytes. Based on the above reasons, asymmetric encryption algorithms are mainly used to exchange symmetric encryption algorithms and secret keys instead of data exchanges.

The following is an example of using the public key and private key in Java:

 

Package com. xforwarfjpk. security; import java. io. fileInputStream; import java. io. fileOutputStream; import java. io. objectInputStream; import java. io. objectOutputStream; import java. security. key; import java. security. keyPair; import java. security. keyPairGenerator; import java. security. privateKey; import java. security. publicKey; import javax. crypto. cipher; import org. junit. test; public class KeyPairTest {private static final String ALGOGRITHM = "RSA"; private static final String PUBLIC_KEY_PATH = "public. key "; private static final String PRIVATE_KEY_PATH =" private. key "; @ Testpublic void testGenerate () throws Exception {// The KeyPairGenerator engine class is used to generate key pairs. By default, JDK (7) supports the following algorithms, diffieHellman, DSA, RSA, ECKeyPairGenerator generator = KeyPairGenerator. getInstance (ALGOGRITHM); // generate key pair KeyPair keyPair = generator. generateKeyPair (); // obtain the Public Key PublicKey publicKey = keyPair. getPublic (); // obtain the private key PrivateKey privateKey = keyPair. getPrivate (); // write the public key and private key to the file for later use writeKey (PUBLIC_KEY_PATH, publicKey); writeKey (PRIVATE_KEY_PATH, privateKey);} @ Testpublic void testEncryptAndDecrypt () throws Exception {Cipher cipher = Cipher. getInstance (ALGOGRITHM); // read the private key and encrypt PrivateKey privateKey = (PrivateKey) readKey (PRIVATE_KEY_PATH); cipher. init (Cipher. ENCRYPT_MODE, privateKey); // encrypt String sendInfo = "My plaintext"; byte [] results = cipher. doFinal (sendInfo. getBytes (); // read the public key and decrypt PublicKey publicKey = (PublicKey) readKey (PUBLIC_KEY_PATH); cipher. init (Cipher. DECRYPT_MODE, publicKey); // decrypt byte [] deciphered = cipher. doFinal (results); // obtain the plaintext String recvInfo = new String (deciphered); System. out. println (recvInfo);} public void writeKey (String path, Key key) throws Exception {FileOutputStream fos = new FileOutputStream (path); ObjectOutputStream oos = new ObjectOutputStream (fos); oos. writeObject (key); oos. close ();} public Key readKey (String path) throws Exception {FileInputStream FCM = new FileInputStream (path); ObjectInputStream bis = new ObjectInputStream (FCM); Object object Object = bis. readObject (); bis. close (); return (Key) object ;}}

In the preceding example, the public Key and private Key are stored directly through the object serialization mechanism. Like the secret Key, the encoded binary data (Key. getEncoded () is saved. For details, see secret key of Java security.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.