Search code examples
c#javaencryptionequivalentaes

Problem with equivalence of a few things while porting encryption code from java to c#


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 ?


Solution

  • 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);
    }