解密加密.NET的RijndaelManaged的使用Java字节 [英] Decrypting bytes encrypted by .NET's RijndaelManaged using Java

查看:1124
本文介绍了解密加密.NET的RijndaelManaged的使用Java字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图解密的东西,这是使用.NET / C#RijndaelManaged的加密,使用Java来解密。

I am trying to decrypt something, which was encrypted using RijndaelManaged of .NET/C#, using Java to decrypt.

在C#程序是不是我的;我不能改变它是更好的互操作性。但我知道它是如何加密:

The C# program is not mine; I cannot change it to be more interoperable. But I know how it is encrypting:

byte[] bytes = new UnicodeEncoding().GetBytes(password); // edit: built-in is 8chars
FileStream fileStream = new FileStream(outputFile, FileMode.Create);
RijndaelManaged rijndaelManaged = new RijndaelManaged();
CryptoStream cryptoStream = new CryptoStream((Stream) fileStream,
    rijndaelManaged.CreateEncryptor(bytes, bytes), CryptoStreamMode.Write);

我不知道如何解密这个在Java结束。最接近有用的,我发现是这篇博客文章< /一>,但它是轻实际的细节,我不能实施解密。

I do not know how to decrypt this on the Java end. The closest thing to useful I have found is this blog post, but it is light on actual details and I could not implement a decrypter.

编辑:我是个白痴,现在有工作

I'm an idiot and now have it working.

统一codeEncoding 是UTF-16LE,而我是用UTF-8。堵漏密码时有固定的程序切换到正确的编码。

UnicodeEncoding is UTF-16LE, while I was using UTF-8. Switching to the proper encoding when plugging the password in has fixed the program.

我还需要得到BouncyCastle的,做 Cipher.getInstance(AES / CBC / PKCS7Padding,BC);

I also needed to get BouncyCastle and do Cipher.getInstance("AES/CBC/PKCS7Padding", "BC");

finaledit:这里的code到解密默认 RijndaelManaged的从.NET的Java流,假设它是使用原始密码作为密钥创建的:

finaledit: Here's the code to decrypt a default RijndaelManaged stream from .NET in Java, assuming it was created using a raw password as the key:

Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
String password = "kallisti"; // only 8, 12, or 16 chars will work as a key
byte[] key = password.getBytes(Charset.forName("UTF-16LE"));
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding", "BC");
cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key, "AES"),
    new IvParameterSpec(key));
return cipher; // then use CipherInputStream(InputStream, Cipher)

请记住:如果你控制了C#结束,请不要使用underived密码作为密钥

And remember: if you control the C# end, don't use an underived password as your key!

推荐答案

这是可能使用标准的AES加密。 Rijndel AES是只是一个超集这是比较宽松的,特别的选择。详细信息请参见 Rijndael算法支持Java的

It's possible using the standard AES decryption. Rijndel is just a superset of AES which is more lax with particular options. See Rijndael support in Java for more details.

从链接的问题给出了答案:

From the answer given in the linked question:

byte[] sessionKey = null; //Where you get this from is beyond the scope of this post
byte[] iv = null ; //Ditto
byte[] plaintext = null; //Whatever you want to encrypt/decrypt
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
//You can use ENCRYPT_MODE or DECRYPT_MODE
cipher.calling init(Cipher.DECRYPT_MODE, new SecretKeySpec(sessionKey, "AES"), new IvParameterSpec(iv));
byte[] ciphertext = cipher.doFinal(plaintext);

这篇关于解密加密.NET的RijndaelManaged的使用Java字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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