Question : How to make a secret key for crypto?

Hi,,

I'm trying to use some of the javax.crypto code to encrypt a string, but I'm not sure how to create a sufficiently random 'secret' array:

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
private void test(byte[] data) {
    final byte[] secret = new byte[] { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 };
    final SecretKeySpec key = new SecretKeySpec(secret, "AES");
	 
    final Cipher encryptCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    encryptCipher.init(Cipher.ENCRYPT_MODE, key);
    final byte[] iv = encryptCipher.getIV();
    final byte[] encrypted = encryptCipher.doFinal(data);
    ...
}


is there some sort of generator that can create one for me?

Thanks

Answer : How to make a secret key for crypto?

You need to use innerHTML to set "text" in div.

Check this :

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:
36:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
	var functions = [change0,change1,change2,change3];
	
	function getRandom(min, max)
	{
		var randomNum = Math.random() * (max-min);
		return(Math.round(randomNum) + min); 
	}
	function change0() {
		document.getElementById("text").innerHTML="Change0";
	}
	function change1() {
		document.getElementById("text").innerHTML="Change1";
	}
	function change2() {
		document.getElementById("text").innerHTML="Change2";
	}
	function change3() {
		document.getElementById("text").innerHTML="Change3";
	}
	
	function choose() {
		var ry = getRandom(0, 3)
		functions[ry]();
	}
</script>
</head>
<body>
<div id="text1"><div id="text"></div></div>
<input type="button" onclick="choose()" value="click me" />
</body>
</html>
Random Solutions  
 
programming4us programming4us