aes.js 1.43 KB
Newer Older
Spencer Chang's avatar
Spencer Chang committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
importClass(Packages.java.security.SecureRandom);
importClass(Packages.javax.crypto.Cipher);
importClass(Packages.javax.crypto.KeyGenerator);
importClass(Packages.javax.crypto.spec.SecretKeySpec);
//importClass(Packages.leaf.plugin.util.Base64);

var AES = {
	encrypt : function(key, data, alg) {
		var base64 = new Packages.leaf.plugin.util.Base64(0, null, true);
		var bytes = new java.lang.String(data).getBytes('UTF-8');
		var gennerator = KeyGenerator.getInstance('AES');
		seed = SecureRandom.getInstance(alg || "SHA1PRNG");
		seed.setSeed(new java.lang.String(key).getBytes());
		gennerator.init(128, seed);
		var cipher = Cipher.getInstance('AES');
		cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(gennerator
				.generateKey().getEncoded(), 'AES'));
		bytes = cipher.doFinal(bytes);
		return String(new java.lang.String(base64.encode(bytes), 'UTF-8'));
	},

	decrypt : function(key, data, alg) {
		var base64 = new Packages.leaf.plugin.util.Base64(0, null, true);
		var bytes = bytes = base64.decodeBase64(data);
		var gennerator = KeyGenerator.getInstance('AES');
		var seed = SecureRandom.getInstance(alg || "SHA1PRNG");
		seed.setSeed(new java.lang.String(key).getBytes());
		gennerator.init(128, seed);
		var cipher = Cipher.getInstance('AES');
		cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(gennerator
				.generateKey().getEncoded(), 'AES'));
		bytes = cipher.doFinal(bytes);
		return String(new java.lang.String(bytes, 'UTF-8'));
	}
};