Click here to Skip to main content
15,890,845 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Java
public static final String AES_TRANSFORMATION = "AES/ECB/PKCS5Padding";
	public static final String AES_ALGORITHM = "AES";
	public static final int ENC_BITS = 256;
	public static final String CHARACTER_ENCODING = "UTF-8";
	
	private static Cipher ENCRYPT_CIPHER;
	private static Cipher DECRYPT_CIPHER;
	private static KeyGenerator KEYGEN;	
	
	static{
		try{
			ENCRYPT_CIPHER = Cipher.getInstance(AES_TRANSFORMATION);
			DECRYPT_CIPHER = Cipher.getInstance(AES_TRANSFORMATION);
			KEYGEN = KeyGenerator.getInstance(AES_ALGORITHM);
			KEYGEN.init(ENC_BITS);
		}catch(NoSuchAlgorithmException | NoSuchPaddingException e) {
			e.printStackTrace();
		}
	}
	private static String generateSecureKey() throws Exception{
		SecretKey secretKey = KEYGEN.generateKey();
		return encodeBase64String(secretKey.getEncoded());
	}


What I have tried:

i have getting different output when conver to c#

addition from comment below - Nelek
i have tried to genrate key
C#
public byte[] GenerateKey()
    {
                    
        using (var aes = CreateAes256Algorithm())
        {

            aes.GenerateKey();
            return aes.Key;
        }
    }

    private RijndaelManaged CreateAes256Algorithm()
    {
        return new RijndaelManaged { KeySize = 256, BlockSize = 128, Mode = CipherMode.ECB, Padding = PaddingMode.PKCS7 };
    }
Posted
Updated 16-Nov-16 1:26am
v2
Comments
Suvendu Shekhar Giri 16-Nov-16 2:00am    
Where is the C# code you have tried?
What are the outputs you are getting?
What are the expected ouputs?
What are the inputs?
Anil Sharma1983 16-Nov-16 2:04am    
i have tried to genrate key
public byte[] GenerateKey()
{

using (var aes = CreateAes256Algorithm())
{

aes.GenerateKey();
return aes.Key;
}
}

private RijndaelManaged CreateAes256Algorithm()
{
return new RijndaelManaged { KeySize = 256, BlockSize = 128, Mode = CipherMode.ECB, Padding = PaddingMode.PKCS7 };
}

1 solution

You could use a ready-made tool:

6 Best Tools to Help You Convert Java to C# Source Code » CODECALL[^]

...or you could be a programmer and do it manually. It really isn't that tough. Most of Java even uses some of the same class names. The only thing you'd have to be concerned with are namespaces and java-specific keywords, like final (as seen in your sample).

BTW, a question regarding generating encryption keys was asked a day or three ago, and I posted a C#-specific answer that actually contained code. You would do well to search for it, and forget this java nonsense.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900