Find two encryption algorithms, one MD5 encryption, which is applicable to password encryption, because this algorithm is irreversible; the other is RSA encryption, Which is reversible:
1. MD5 Encryption
Import Java. security. messagedigest; public class MD5 {private final static string [] hexdigits = {"0", "1", "2", "3", "4", "5 ", "6", "7", "8", "9", "A", "B", "C", "D", "E ", "F "}; /*** convert the byte array to a hexadecimal string ** @ Param B * byte array * @ return hexadecimal string */public static string bytearraytohexstring (byte [] B) {stringbuffer resultsb = new stringbuffer (); For (INT I = 0; I <B. length; I ++) {resultsb. append (bytetohexstring (B [I]);} return resultsb. tostring ();} Private Static string bytetohexstring (byte B) {int n = B; If (n <0) n = 256 + N; int d1 = N/16; int D2 = n % 16; return hexdigits [D1] + hexdigits [D2];} public static string md5encode (string origin) {string resultstring = NULL; try {resultstring = new string (origin); messagedigest MD = messagedigest. getinstance ("MD5"); resultstring = bytearraytohexstring (MD. digest (resultstring. getbytes ();} catch (exception ex) {} return resultstring ;}}
Ii. RSA Encryption
import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.interfaces.RSAPrivateKey; import java.security.interfaces.RSAPublicKey; import javax.crypto.Cipher; /** * RSAEncrypt * * @author chen * @see */ public class Rsaencrype { /** * Main method for RSAEncrypt. * @param args */ public static void main(String[] args) { try { Rsaencrype encrypt = new Rsaencrype(); 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[] */ protected String bytesToString(byte[] encrytpByte) { String result = ""; for (Byte bytes : encrytpByte) { result += (char) bytes.intValue(); } return result; } /** * Encrypt String. * @return byte[] */ protected byte[] encrypt(RSAPublicKey publicKey, byte[] obj) { if (publicKey != null) { try { Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.ENCRYPT_MODE, publicKey); return cipher.doFinal(obj); } catch (Exception e) { e.printStackTrace(); } } return null; } /** * Basic decrypt method * @return byte[] */ protected byte[] decrypt(RSAPrivateKey privateKey, byte[] obj) { if (privateKey != null) { try { Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.DECRYPT_MODE, privateKey); return cipher.doFinal(obj); } catch (Exception e) { e.printStackTrace(); } } return null; } }