Original: zhouxianli1978@msn.com, welcome friends from Java to add me
DES encryption package
package org.jtool.desutils; import java.security.SecureRandom; import javax.crypto *;. import javax.crypto.spec.DESKeySpec; public class DESEncrypt {private byte [] desKey; public DESEncrypt (byte [] desKey) {this .deskey = deskey;} public byte [] doencrypt (Byte [] plation) throws exception {// DES algorithm requires a trusted random number SECURANDOM SR = New Securerandom (); byte rawkeydata [] = deskey; / * Get a key data in some way * / / From the original key data DESKEYSPEC object Deskeyspec DKS = New Deskeyspec (RawkeyData); // Create a key factory, then use it to convert DeskeySpec into // a secretkey object SecretKeyFactory KeyFactory = SecretKeyFactory.GetInstance ("des"); SecretKey key = keyfactory.generateSecret (DKS); // Cipher object actually completed encryption Cipher Cipher = Cipher.GetInstance ("DES"); // Initialize Cipher Object with Key .init (Cipher.Encrypt_mode, KEY, SR); // Today, get data and encrypted byte data [] = plaintext; / * Get data * / / in some way to officially perform encryption operation Byte EncryptedData [] = Cipher. DOFINAL (DAT a); return encrypteddata;}}
DES decryption package
package org.jtool.desutils; import java.security.SecureRandom; import javax.crypto *;. import javax.crypto.spec.DESKeySpec; public class DESDecrypt {private byte [] desKey; public DESDecrypt (byte [] desKey) {this .deskey = deskey;} public byte [] dodecrypt (byte [] encrypttext) throws exception {// des Algorithm requires a trusted random number SECURANDOM SR = new securerandom (); byte rawkeydata [] = deskey; / * Get the original key data in some way * / / Create a deskeySpec object from the original key data Deskeyspec DKS = New Deskeyspec (RawkeyData); // Create a key factory, then use it to convert the Deskeyspec object into // SecretKey objects SecretKeyFactory keyFactory = SecretKeyFactory.getInstance ( "DES"); SecretKey key = keyFactory.generateSecret (dks); // cipher objects actually completed decryption cipher cipher = Cipher.getInstance ( "DES"); // initialize with key Cipher object cipher.init (cipher.decrypt_mode, key, sr); // Now get data and decrypt Byte EncryptedData [] = EncryptText; / * Get encrypted data * / / officially perform decryption operation Byte DecryptedData [] = Cipher.dofinal (EncryptedData); Return DecryptedData;}}} DES adds example