package net.oschina.crypto;

import java.security.Key;
import java.security.NoSuchAlgorithmException;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

 
 
 
 
public abstract class   RC2Coder {



	/*
	 * 
	 * @return
	 * @throws NoSuchAlgorithmException
	 */
	public static byte[] initKey() throws NoSuchAlgorithmException {
		KeyGenerator kg = KeyGenerator.getInstance(KEY_ALGORITHM);
		//kg.init(256);
		SecretKey secretKey = kg.generateKey();
		return secretKey.getEncoded();

	}

	/**
	 * 
	 * 
	 * @param data
	 * @param key
	 * @return
	 * @throws Exception
	 */
	public static byte[] encypt(byte[] data, byte[] key) throws Exception {
		Key key2 = toKey(key);
		Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
		cipher.init(Cipher.ENCRYPT_MODE, key2);
		return cipher.doFinal(data);

	}

	/**
	 * 
	 * @param key
	 * @return
	 * @throws Exception
	 */
	private static Key toKey(byte[] key) throws Exception {
		SecretKey secretKey = new SecretKeySpec(key, KEY_ALGORITHM);
		return secretKey;
	}

	/**
	 * 
	 * 
	 * @param data
	 * @param key
	 * @return
	 * @throws Exception
	 */
	public static byte[] decypt(byte[] data, byte[] key) throws Exception {
		Key key2 = toKey(key);
		Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
		cipher.init(Cipher.DECRYPT_MODE, key2);
		return cipher.doFinal(data);
	}

	/**
	 * 
	 */
	public static final String KEY_ALGORITHM = "RC2";

	public static final String CIPHER_ALGORITHM = "RC2/ECB/PKCS5Padding";
}

@@ -565,32 +565,34 @@
 ;%0A%0A%09%7D%0A%0A%09/**%0A%09 * 
+%E5%8A%A0%E5%AF%86
 %0A%09 * %0A%09 * @param
@@ -1101,24 +1101,26 @@
 **%0A%09 * %0A%09 * 
+%E8%A7%A3%E5%AF%86
 %0A%09 * @param 
@@ -1115,32 +1115,35 @@
 %0A%09 * @param data
+ %E6%95%B0%E6%8D%AE
 %0A%09 * @param key%0A
@@ -1133,32 +1133,35 @@
 %E6%8D%AE%0A%09 * @param key
+ %E5%AF%86%E9%92%A5
 %0A%09 * @return%0A%09 *

Update RC2Coder.java