如何在C#中转换java代码 [英] How to convert java code in C#

查看:85
本文介绍了如何在C#中转换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());
	}





我的尝试:

i得到不同的输出#



从以下评论中添加 - Nelek

i试图生成密钥



What I have tried:

i have getting different output when conver to c#

addition from comment below - Nelek
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 };
    }

推荐答案

您可以使用现成的工具:



帮助您转换的6种最佳工具Java到C#源代码»CODECALL [ ^ ]



...或者你可以成为程序员并手动完成。这真的不是那么艰难。大多数Java甚至使用了一些相同的类名。您唯一需要关注的是命名空间和特定于Java的关键字,例如 final (如示例所示)。



BTW,有关生成加密密钥的问题是在一三天前提出的,我发布了一个实际包含代码的C#特定答案。你会很好地搜索它,并忘记这个java废话。
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.


这篇关于如何在C#中转换java代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆