1 ImportJava.math.BigInteger;2 Importjava.security.InvalidKeyException;3 Importjava.security.MessageDigest;4 Importjava.security.NoSuchAlgorithmException;5 ImportJavax.crypto.KeyGenerator;6 ImportJavax.crypto.Mac;7 ImportJavax.crypto.SecretKey;8 ImportJavax.crypto.spec.SecretKeySpec;9 Importorg.apache.commons.codec.binary.Base64;Ten One Public classMyencrypt { A Public Static FinalString Key_sha = " sha"; - Public Static FinalString KEY_MD5 = " md5"; - Public Static FinalString Key_mac = " hmacmd5"; the - - //Sun does not recommend the use of their own base64, with Apache very good - /** + * BASE64 Decryption - */ + Public Static byte[] decryptBASE64 (byte[] dest) { A if(Dest = =NULL) { at return NULL; - } - returnbase64.decodebase64 (dest); - } - - /** in * BASE64 Encryption - */ to Public Static byte[] encryptBASE64 (byte[] origin) { + if(Origin = =NULL) { - return NULL; the } * returnBase64.encodebase64 (origin); $ }Panax Notoginseng - /** the * MD5 Encryption + * A * @throwsnosuchalgorithmexception the */ + Public Static byte[] EncryptMD5 (byte[] data) - throwsnosuchalgorithmexception { $ if(Data = =NULL) { $ return NULL; - } -MessageDigest MD5 =messagedigest.getinstance (KEY_MD5); the md5.update (data); - returnmd5.digest ();Wuyi } the - /** Wu * SHA Encryption - * About * @throwsnosuchalgorithmexception $ */ - Public Static byte[] Encryptsha (byte[] data) - throwsnosuchalgorithmexception { - if(Data = =NULL) { A return NULL; + } theMessageDigest sha =messagedigest.getinstance (Key_sha); - sha.update (data); $ returnsha.digest (); the } the the /** the * Initialize HMAC key - * in * @throwsnosuchalgorithmexception the */ the Public StaticString Initmackey ()throwsnosuchalgorithmexception { AboutKeygenerator Keygenerator =keygenerator.getinstance (KEY_MAC); theSecretkey Secretkey =Keygenerator.generatekey (); the return NewString (encryptBASE64 (secretkey.getencoded ())); the } + - /** the * HMAC EncryptionBayi * the * @throwsnosuchalgorithmexception the * @throwsinvalidkeyexception - */ - Public Static byte[] Encrypthmac (byte[] data, String key) the throwsnosuchalgorithmexception, invalidkeyexception { theSecretkey Secretkey =NewSecretkeyspec (decryptBASE64 (Key.getbytes ()), the Key_mac); theMac Mac =mac.getinstance (Secretkey.getalgorithm ()); - Mac.init (secretkey); the returnmac.dofinal (data); the the }94 the Public Static voidMain (string[] args)throwsException { the //TODO auto-generated Method Stub theString data = " Simple Encryption ";98System.out.println (NewBigInteger (encryptBASE64 (Data.getbytes ())). ToString (16)); AboutSystem.out.println (NewBigInteger (encryptBASE64 (Data.getbytes ())). ToString (32)); -System.out.println (NewString (decryptBASE64 (encryptBASE64 (Data.getbytes () )));101 102System.out.println (NewBigInteger (EncryptMD5 (Data.getbytes ())). ToString ());103System.out.println (NewBigInteger (Encryptsha (Data.getbytes ())). ToString ());104 theSystem.out.println (NewBigInteger (Encrypthmac (Data.getbytes (), Initmackey ()). ToString ());106 }107 108}
Java various encryption algorithms