2

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 ?

Guapo
  • 3,446
  • 9
  • 36
  • 63
  • Whats the point of continuing if the KeyGenerator factory method failed ? This just seems plain wrong not to mention that kgen will be null. – mP. Jun 04 '11 at 05:28
  • @mP as I mentioned on your answer the java was not made by me and I am just trying to refactor while porting it to c# – Guapo Jun 04 '11 at 07:26

4 Answers4

1

I would take a look at a former SO post, c# implementations of AES encryption as the SecretKey (and alike) objects appear to be part of an Advanced Encryption Standard (AES) library.

Community
  • 1
  • 1
Brad Christie
  • 100,477
  • 16
  • 156
  • 200
  • sorry to disturb you again but would using Rijndael like the update I did on my question get the same results ? I have been looking around and this seemed to one option. – Guapo Jun 04 '11 at 07:20
1

My guess is that you probably want to take a look at the System.Security.Cryptography namespace.

On another note, while there doesn't seem to be any direct translations to those classes, it does look like System.Security.Cryptography.AESManaged might provide the functions to get the job done anyway.

June Rhodes
  • 3,047
  • 4
  • 23
  • 29
0

Your code fragment is completely wrong/broken. Firstly you are handling the case of getting an instanceof KeyGenerator completely wrong. If the algorithm is unavailable, your kgen instance will be null which will prevent the remainder from continuing and actually doing anything with the null reference.

mP.
  • 18,002
  • 10
  • 71
  • 105
  • @mP that is not my code ... I am just trying to port it to c# but not as it is and for that i need to know what is the equivalent of those functions – Guapo Jun 04 '11 at 06:23
0

The closest conversion I came down to was this one, where I get the parameters using the CastleBouncy method, the random and some other things from the java code was never needed to reproduce the conversion and encryption on c#

public class Encrypt
{
    private byte[] skey;
    private byte[] iv;
    private ParametersWithIV skey_spec;

    public Encrypt()
    {
    }

    public void initAES()
    {
        RijndaelManaged myAES = new RijndaelManaged();
        myAES.Padding = PaddingMode.PKCS7;
        myAES.Mode = CipherMode.CBC;
        myAES.KeySize = 256;
        myAES.BlockSize = 128;
        myAES.GenerateIV();
        myAES.GenerateKey();

        skey = myAES.Key;
        iv = myAES.IV;

        skey_spec = new ParametersWithIV(new KeyParameter(skey), iv);
    }
}

which can further be used with:

public byte[] aesDecrypt(byte[] data)
{
    IBufferedCipher cipher = CipherUtilities.GetCipher("AES/CBC/PKCS5Padding");
    cipher.Init(false, skey_spec);
    return cipher.DoFinal(data);
}

public byte[] aesEncrypt(byte[] data)
{
    IBufferedCipher cipher = CipherUtilities.GetCipher("AES/CBC/PKCS5Padding");
    cipher.Init(true, skey_spec);
    return cipher.DoFinal(data);
}
Guapo
  • 3,446
  • 9
  • 36
  • 63