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. rsw.vatekey;
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
Rsw.vatekey privateKey = (rsw.vatekey) 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 (rsw.vatekey 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;
}
}