Java Common encryption algorithm applied MD5,SHA,RSA__ encoding

Source: Internet
Author: User
Tags array to string decrypt md5 md5 encryption

1. MD5 encryption, often used to encrypt user name passwords, when the user is authenticated. There is no way to decrypt it.

protected byte [] Encrypt (byte [] obj) ... {
Try ... {
MessageDigest MD5 = messagedigest.getinstance ("MD5");
Md5.update (obj);
return Md5.digest ();
catch (NoSuchAlgorithmException e) ... {
E.printstacktrace ();
}
}

2. Sha encryption, similar to the use of MD5, but the two algorithms are different. protected byte [] Encrypt (byte [] obj) ... {
Try ... {
MessageDigest sha = messagedigest.getinstance ("Sha");
Sha.update (obj);
return Sha.digest ();
catch (NoSuchAlgorithmException e) ... {
E.printstacktrace ();
}
}

3. RSA encryption, RAS encryption allows decryption. Often used for encryption of text content.

Import Java.security.KeyPair;
Import Java.security.KeyPairGenerator;
Import Java.security.interfaces.RSAPrivateKey;
Import Java.security.interfaces.RSAPublicKey;

Import Javax.crypto.Cipher;

/** */ /**
* <b>RSAEncrypt</b>
* <p>
* @author Maqujun
* @see
*/
public class Rsaencrypt ... {

/** *//**
* Main method for Rsaencrypt.
* @param args
*/
public static void Main (string[] args) ... {
Try ... {
Rsaencrypt encrypt = new Rsaencrypt ();

String encrypttext = "Encrypttext";
Keypairgenerator Keypairgen = keypairgenerator.getinstance ("RSA");
Keypairgen.initialize (1024);
KeyPair KeyPair = Keypairgen.generatekeypair ();
Generate keys
Rsaprivatekey Privatekey = (rsaprivatekey) keypair.getprivate ();
Rsapublickey PublicKey = (rsapublickey) keypair.getpublic ();

Byte[] e = Encrypt.encrypt (PublicKey, Encrypttext.getbytes ());
byte[] de = Encrypt.decrypt (privatekey,e);
System.out.println (Encrypt.bytestostring (e));
System.out.println (Encrypt.bytestostring (DE));
catch (Exception e) ... {
E.printstacktrace ();
}
}

/** *//**
* Change byte array to String.
* @return byte[]
*/

Related Article

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.