java 加密解密工具类

	/**
	 * @param input
	 * @return
	 * @throws Exception
	 */
	public static String encryptData(String input) throws Exception {

		SecureRandom sr = new SecureRandom();
		byte rawKeyData[] = "ABCDEFGH".getBytes();
		DESKeySpec dks = new DESKeySpec(rawKeyData);

		SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
		SecretKey key = keyFactory.generateSecret(dks);

		Cipher c = Cipher.getInstance("DES");
		c.init(Cipher.ENCRYPT_MODE, key, sr);
		byte[] cipherByte = c.doFinal(input.getBytes());
		String dec = new BASE64Encoder().encode(cipherByte);
		return dec;

	}

	/**
	 * @param input
	 * @return
	 * @throws Exception
	 */
	public static String decryptData(String input) throws Exception {
		byte[] dec = new BASE64Decoder().decodeBuffer(input);

		SecureRandom sr = new SecureRandom();
		byte rawKeyData[] = "ABCDEFGH".getBytes();

		DESKeySpec dks = new DESKeySpec(rawKeyData);

		SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");

		SecretKey key = keyFactory.generateSecret(dks);

		Cipher c = Cipher.getInstance("DES");
		c.init(Cipher.DECRYPT_MODE, key, sr);
		byte[] clearByte = c.doFinal(dec);

		return new String(clearByte);

	}

你可能感兴趣的:(java,C++,c,C#)