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[]
*/