DES.JAVAPAGER Lion.security.cryptography;
import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.NoSuchPaddingException; import javax.crypto.SecretKey; import java.security.NoSuchAlgorithmException; import java.security.Security; / ** * DES encryption, There are two ways, encrypted, decrypting * @Author lion * @Author www.lionsky.net * / public class des {private string algorithm = "des"; private keygenerator keygen; private secretkey design; private cipher C; private [] cipherbyte;
/ ** * Initialization DES instance * / public des () {init ();
public void init () {Security.addProvider (new com.sun.crypto.provider.SunJCE ()); try {keygen = KeyGenerator.getInstance (Algorithm); deskey = keygen.generateKey (); c = Cipher.getInstance (Algorithm );} Catch (nosuchalgorithmexception ex) {ex.printStackTrace ();} catch (nosuchpaddingexception ex) {ex.printstacktrace ();}}
/ ** * Encrypts String * @Param Str To encrypt data * @return Returns the encrypted BYTE array * / public byte [] CreateEncryptor (String str) {try {c.init (cipher.encrypt_mode, deskey); cipherByte = c.doFinal (str.getBytes ());} catch (java.security.InvalidKeyException ex) {ex.printStackTrace ();} catch (javax.crypto.BadPaddingException ex) {ex.printStackTrace ();} catch ( Javax.crypto.illegalblocksizeException ex) {ex.printStackTrace ();} returnified cipherbyte
/ ** * Decrypts the BYTE array * @Param BUFF to decrypt data * @return returns the encrypted string * / public string createDecryptor (byte [] buff) {Try {c.init (cipher.decrypt_mode, deskey); cipherByte = c.doFinal (buff);} catch (java.security.InvalidKeyException ex) {ex.printStackTrace ();} catch (javax.crypto.BadPaddingException ex) {ex.printStackTrace ();} catch (javax.crypto. IllegalblocksizeException ex) {ex.printStackTrace ();}}}} DES.JSP <% @ Page ContentTyPE = "text / html; charSet = GB2312"%>