Encryption and decryption in Java

Source: Internet
Author: User
Tags decrypt asymmetric encryption

Encryption and decryption in Java is mainly to protect the security of the data, divided into symmetric and asymmetric, symmetric means that the encryption key and decryption key is the same key, it is often referred to as the secret key or the private key, asymmetric key represents the encryption key and decryption to two different keys, one for the public key, The other is a private secret key. Symmetric encryption and decryption algorithm has DES, AES and so on, asymmetric encryption decryption algorithm has RSA, DH and so on. For encrypting and decrypting data we usually encrypt and decrypt the data by generating the key and then using the encryption and decryption operation class, then we look at how to implement symmetric, asymmetric encryption and decryption in Java.

Symmetric encryption and decryption: Generates a key (key) through Keygenerator (key generator), and then encrypts and decrypts the data through the cipher (cryptographic decryption operation Class).

Example:

Keygenerator kg=keygenerator.getinstance ("DES");//Key generator for the specified algorithm
Kg.init (56);//Key generator initialization, specifying the length of the generated key
Key Key=kg.generatekey ();//Get Key

Cipher cp=cipher.getinstance ("DES");//Cryptographic decryption operation class for the specified algorithm
Cp.init (Cipher.encrypt_mode, key);//Initialize it with a key and initialize it as a cryptographic operation class
Byte[] b=cp.dofinal ("Hello". GetBytes ());//Encrypt data
System.out.println (New String (b));

Cp.init (Cipher.decrypt_mode, key);//Initialize it with a key and initialize it to the decryption operation class
System.out.println (New String (cp.dofinal (b)));//Decrypt data


Asymmetric encryption and decryption: Generate KeyPair (key) through Keypairgenerator (key generator), then obtain public key and secret key through KeyPair, and finally utilize public key and secret key through cipher (cryptographic decryption operation Class) the encryption and decryption of data.

Keypairgenerator kpg=keypairgenerator.getinstance ("RSA");
Kpg.initialize (512);
KeyPair Kp=kpg.generatekeypair ();

Cipher c=cipher.getinstance ("RSA");
C.init (Cipher.encrypt_mode, Kp.getprivate ());
Byte[] B=c.dofinal ("Zhangsan". GetBytes ());

System.out.println (New String (b));

C.init (Cipher.decrypt_mode, Kp.getpublic ());
System.out.println (New String (c.dofinal (b)));

Data security is guaranteed but how is the integrity of the data guaranteed? There are two ways to guarantee message digest and digital signature. A message digest is a hash value generated through information, such as a sending the data to B,a, which sends the message and message digest in the past, B receives the information, generates a message digest with the information, and is consistent with the received message digest. The digital signature not only guarantees the integrity of the information, but also guarantees the non-forgery of the data, and also uses the message to generate the signature, sends the past with the message, receives the message and the signature, and uses the message to verify the signature.

Message Summary:

               MessageDigest md=messagedigest.getinstance ("MD5");
     md.update ("Zhangsan". GetBytes ());
    system.out.println (New String (Md.digest ()));//Generate Message Digest
   md.reset ();//For resetting MessageDigest, In fact, when we call the Digest () method, we reset the messagedigest
   SYSTEM.OUT.PRINTLN ( New String (Md.digest ("Zhangsan". GetBytes ())));

/**
* MessageDigest is used to generate a message digest, also known as a hash value and a hash value, the algorithm used to generate the digest is called a hash function, a hash function, and is mainly used to guarantee
* Data integrity, in the case of the same hash function, only the same message can produce the same digest, send the message and digest to send a receiver, the recipient accepts
* The message digest is generated after the message using the same hash function that was sent, and the comparison is the same as the received message digest.
*/

Digital signatures: Unlike message digests, digital signatures are generated and validated through public keys

byte[] message= "Hello". GetBytes ();
Keypairgenerator kpg=keypairgenerator.getinstance ("DSA");
Kpg.initialize (1024);
KeyPair Kp=kpg.generatekeypair ();
Signature sign=signature.getinstance (Kpg.getalgorithm ());

Generate a signature from a private key
Sign.initsign (Kp.getprivate ());
Sign.update (message);
Byte[] S=sign.sign (); Generate signature

Verifying signatures with public keys
Sign.initverify (Kp.getpublic ());
Sign.update (message);
System.out.println (Sign.verify (s));//Verify Signature


We may also have access to the provider (security provider) in the process of use, simply to understand is to provide cryptographic decryption algorithm side, in the generation of the secret key generator and public key generator can be used, provider inherit properties, It can therefore be used in the same way as properties. Security (The action class for the provider), which is used to manage the provider (find, delete, add). There are many providers built into the JDK and can be found in the following ways

Provider[] Ps=security.getproviders ();
System.out.println (ps.length);
for (Provider P:ps)
{
Set<entry<object, object>> entry=p.entryset ();
for (entry<object,object> e:entry)
System.out.println (E.getkey () + "" +e.getvalue ());
}



Encryption and decryption in Java

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.