I am trying to port the below Java code to C#, but I am having some difficulty figuring out what would be the equivalent for:
SecretKey skey
SecretKeySpec skey_spec
IvParameterSpec iv_spec
KeyPair rsaKey
KeyGenerator kgen
Would appreciate if some one could help me out with those...
package entry;
public class Encrypt {
SecretKey skey;
SecretKeySpec skey_spec;
byte[] iv;
IvParameterSpec iv_spec;
KeyPair rsaKey;
Random random;
public Encrypt() {
random = new Random();
}
public void initAES() {
System.out.println("Initializing AES Keys...");
KeyGenerator kgen = null;
try {
kgen = KeyGenerator.getInstance("AES");
} catch(NoSuchAlgorithmException nsae) {
nsae.printStackTrace();
}
kgen.init(256);
// Generate the secret key specs.
skey = kgen.generateKey();
byte[] raw = skey.getEncoded();
skey_spec = new SecretKeySpec(raw, "AES");
iv = new byte[16];
random.nextBytes(iv);
iv_spec = new IvParameterSpec(iv);
}
}
UPDATE attempt in c#:
private byte[] _secretKey_iv;
private byte[] _secretKey;
private void GenerateKey()
{
RijndaelManaged myAES = new RijndaelManaged();
myAES.KeySize = 256;
myAES.GenerateIV();
myAES.GenerateKey();
_secretKey_iv = myAES.IV;
_secretKey = myAES.Key;
}
UPDATE #2:
KeyGenerator kgen
SecretKeySpec skey_spec
IvParameterSpec iv_spec
Still don't know how to get the specs from the generated AES key and would really appreciate some help understanding how to do that also I am still not sure if that is the correct conversion of the KeyGenerator ?
KeyGenerator is part of BouncyCastleProvider ?