将加密代码从 java 移植到 c# 时存在一些等效问题

发布于 2024-11-14 01:01:04 字数 1645 浏览 2 评论 0原文

我正在尝试将下面的 Java 代码移植到 C#,但我在弄清楚等效项时遇到了一些困难:

SecretKey skey
SecretKeySpec skey_spec
IvParameterSpec iv_spec
KeyPair rsaKey
KeyGenerator kgen

如果有人可以帮助我解决这些问题,我将不胜感激...

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

更新尝试在 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;
}

更新#2:

KeyGenerator kgen
SecretKeySpec skey_spec
IvParameterSpec iv_spec

仍然不知道如何从生成的 AES 密钥中获取规格,并且非常感谢一些帮助了解如何做到这一点,我仍然不确定这是否是KeyGenerator 的转换正确吗?

KeyGenerator 是 BouncyCastleProvider 的一部分?

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

唠甜嗑 2024-11-21 01:01:04

我会看一下以前的 SO 帖子,c# 实现 AES 加密 作为SecretKey (和类似的)对象似乎是 高级加密标准 (AES) 库。

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.

忆离笙 2024-11-21 01:01:04

我的猜测是您可能想看看 System.Security.Cryptography 命名空间。

另一方面,虽然这些类似乎没有任何直接翻译,但它看起来确实像System.Security.Cryptography.AESManaged 可能会提供完成工作的功能。

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.

月野兔 2024-11-21 01:01:04

您的代码片段完全错误/损坏。首先,您正在处理完全错误的 KeyGenerator 实例的情况。如果该算法不可用,您的 kgen 实例将为空,这将阻止其余部分继续并实际使用空引用执行任何操作。

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.

恋竹姑娘 2024-11-21 01:01:04

我最接近的转换是这个,我使用 CastleBouncy 方法获取参数,而 Java 代码中的随机和其他一些东西不需要在 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);
    }
}

,可以进一步用于:

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

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文