DES 암호화 알고리즘 암호화 해독

1716 단어 암호화 해독

 
private final static String DES = "DES";
	/**
	 *   
	 * @param src   
	 * @param key  ,     8   
	 * @return         
	 * @throws Exception
	 */
	public static byte[] encrypt(byte[] src, byte[] key) throws Exception {
		// DES               
		SecureRandom sr = new SecureRandom();
		//          DESKeySpec  
		DESKeySpec dks = new DESKeySpec(key);
		//         ,     DESKeySpec   
		//   SecretKey  
		SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
		SecretKey securekey = keyFactory.generateSecret(dks);
		// Cipher          
		Cipher cipher = Cipher.getInstance(DES);
		//       Cipher  
		cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);
		//   ,       
		//         
		return cipher.doFinal(src);
	}

	/**
	 *   
	 * @param src   
	 * @param key  ,     8   
	 * @return           
	 * @throws Exception
	 */
	public static byte[] decrypt(byte[] src, byte[] key) throws Exception {
		// DES               
		SecureRandom sr = new SecureRandom();
		//            DESKeySpec  
		DESKeySpec dks = new DESKeySpec(key);
		//         ,     DESKeySpec     
		//   SecretKey  
		SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
		SecretKey securekey = keyFactory.generateSecret(dks);
		// Cipher          
		Cipher cipher = Cipher.getInstance(DES);
		//       Cipher  
		cipher.init(Cipher.DECRYPT_MODE, securekey, sr);
		//   ,       
		//         
		return cipher.doFinal(src);
	}

좋은 웹페이지 즐겨찾기