package com.ai.ipu.mobile.license;

import java.security.SecureRandom;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.SecretKeyFactory;
import javax.crypto.SecretKey;
import javax.crypto.Cipher;

public class LicenseVerifyUtil {
	
	// add by wyj
	// 使用AES/DES加密算法时应显示设定工作模式为CBC或CFB 模式。
	// 定义加密算法
	// DES,DESede,Blowfish，DESede/CBC/PKCS5Padding 
	private static final String DES_CBC = "DESede/CBC/PKCS5Padding"; 
		
	static {
		System.loadLibrary("VerifySafe");
	}
	
	public static byte[] encrypt(byte[] datasource, String password) {
		try {
			SecureRandom random = new SecureRandom();
			DESKeySpec desKey = new DESKeySpec(password.getBytes());
			// SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");// del by wyj
			// modify by wyj
			SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("desede");
			SecretKey securekey = keyFactory.generateSecret(desKey);
			// Cipher cipher = Cipher.getInstance("DES");// del by wyj
			// modify by wyj
			Cipher cipher = Cipher.getInstance(DES_CBC);
			cipher.init(Cipher.ENCRYPT_MODE, securekey, random);
			return cipher.doFinal(datasource);
		} catch (Throwable e) {
			e.printStackTrace();
		}
		return null;
	}

	public static byte[] decrypt(byte[] src, String password) throws Exception {
		SecureRandom random = new SecureRandom();
		DESKeySpec desKey = new DESKeySpec(password.getBytes());
		// SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");// del by wyj
		// modify by wyj
		SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("desede");
		SecretKey securekey = keyFactory.generateSecret(desKey);
		// Cipher cipher = Cipher.getInstance("DES");// del by wyj
		// modify by wyj
		Cipher cipher = Cipher.getInstance(DES_CBC);
		cipher.init(Cipher.DECRYPT_MODE, securekey, random);
		return cipher.doFinal(src);
	}
	
	public native String  decodeLicense(String license);
}
