加密解密DES

package test; import java.security.SecureRandom; import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.DESKeySpec; public class Encrypt { /** * 方法名:Encrypt.java * 日 期:Aug 18, 2009 * * @param args */ public Encrypt() { } public byte[] getKey() throws Exception { return "1234567890".getBytes(); } public byte[] encryptData(String encryptdata) throws Exception { SecureRandom sr = new SecureRandom(); byte[] rawKeyData = getKey(); DESKeySpec dks = new DESKeySpec(rawKeyData); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey key = keyFactory.generateSecret(dks); Cipher cipher = Cipher.getInstance("DES"); cipher.init(Cipher.ENCRYPT_MODE, key, sr); byte[] data = encryptdata.getBytes(); byte[] encryptedData = cipher.doFinal(data); return encryptedData; } public String decryptData(byte[] decryptdata) throws Exception { SecureRandom sr = new SecureRandom(); byte[] rawKeyData = getKey(); DESKeySpec dks = new DESKeySpec(rawKeyData); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey key = keyFactory.generateSecret(dks); Cipher cipher = Cipher.getInstance("DES"); cipher.init(Cipher.DECRYPT_MODE, key, sr); byte[] encryptedData = decryptdata; byte[] decryptedData = cipher.doFinal(encryptedData); return new String(decryptedData); } public static void main(String[] args) { try { Encrypt encrypt = new Encrypt(); byte[] enpasswd = encrypt.encryptData("333333"); String depasswd = encrypt.decryptData( enpasswd ); System.out.println("enpasswd==="+enpasswd); System.out.println("depasswd==="+depasswd); } catch (Exception e) { e.printStackTrace(); } } }

你可能感兴趣的:(加密,exception,String,解密,Class,byte)