将加密代码从 java 移植到 c# 时存在一些等效问题
我正在尝试将下面的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我会看一下以前的 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.我的猜测是您可能想看看
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.您的代码片段完全错误/损坏。首先,您正在处理完全错误的 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.
我最接近的转换是这个,我使用 CastleBouncy 方法获取参数,而 Java 代码中的随机和其他一些东西不需要在 C# 上重现转换和加密
,可以进一步用于:
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#
which can further be used with: