在java中加密,在C#中解密以及逆向
我已经阅读了我的标题的一些主题,所以我无法使用它们来解决我的问题。所以,我打开一个新话题... 我想使用对称加密
客户端(java)在java(针对Android应用程序)和C#(我的服务器)中创建2个函数加密和解密:Encrypt(ClearText1, Key)=secret -->服务器:解密(秘密,密钥)= ClearText1 和 服务器(java):加密(ClearText2,Key)=秘密-->客户端:Decrypt(secret,Key)=ClearText2
我已经使用了BouncyCastle Library For java和C#,但java中的字节是-128..127,而C#中的字节是0..255,没有负数数据。所以客户端和服务器端加密解密都是失败的。
请帮助我(或分享我的问题的库)。谢谢你!
这里是 C# 代码: 如何在java中使用它们?注意C#中的数据类型“byte”与java中不匹配 (当然,用以下2个函数加密和解密数据是正确的)
> public string _secretPhrase = "123abc456";
> public string EncryptData(string plainText)
> {
> DES des = new DESCryptoServiceProvider();
> des.Mode = CipherMode.ECB;
> des.Padding = PaddingMode.PKCS7;
>
> des.Key = Encoding.UTF8.GetBytes(_secretPhrase.Substring(0, 8));
> des.IV = Encoding.UTF8.GetBytes(_secretPhrase.Substring(0, 8));
>
> byte[] bytes = Encoding.UTF8.GetBytes(plainText);
> byte[] resultBytes = des.CreateEncryptor().TransformFinalBlock(bytes, 0, bytes.Length);
>
> return Convert.ToBase64String(resultBytes);
> }
>
> public string DecryptData(string encryptedText)
> {
> DES des = new DESCryptoServiceProvider();
> des.Mode = CipherMode.ECB;
> des.Padding = PaddingMode.PKCS7;
> des.Key = Encoding.UTF8.GetBytes(_secretPhrase.Substring(0, 8));
> des.IV = System.Text.Encoding.UTF8.GetBytes(_secretPhrase.Substring(0, 8));
>
> byte[] bytes = Convert.FromBase64String(encryptedText);
> byte[] resultBytes = des.CreateDecryptor().TransformFinalBlock(bytes, 0, bytes.Length);
>
> return Encoding.UTF8.GetString(resultBytes);
> }
I have read some topic for my title, so I can't use them to fix my problem. So, i open a new topic...
I want to create 2 functions Encrypt and Decrypt in java (for Android app) and C# (my server) using symmetric encryption
Client (java) : Encrypt(ClearText1, Key)=secret --> Server : Decrypt(secret,Key)=ClearText1
and
Server (java) : Encrypt(ClearText2, Key)=secret --> Client: Decrypt(secret,Key)=ClearText2
I have used BouncyCastle Library For java and C#, but the byte in java is -128..127, while the byte in C# is 0..255, have not negative data. So client and Server encryption and decryption is fail.
Please help me (or share me the library for my problem). Thank you!
Here the code in C#:
How to use them in java? Note the data type "byte" in C# and java in not match
(of course, encrypt and decrypt data with 2 following funcions is true)
> public string _secretPhrase = "123abc456";
> public string EncryptData(string plainText)
> {
> DES des = new DESCryptoServiceProvider();
> des.Mode = CipherMode.ECB;
> des.Padding = PaddingMode.PKCS7;
>
> des.Key = Encoding.UTF8.GetBytes(_secretPhrase.Substring(0, 8));
> des.IV = Encoding.UTF8.GetBytes(_secretPhrase.Substring(0, 8));
>
> byte[] bytes = Encoding.UTF8.GetBytes(plainText);
> byte[] resultBytes = des.CreateEncryptor().TransformFinalBlock(bytes, 0, bytes.Length);
>
> return Convert.ToBase64String(resultBytes);
> }
>
> public string DecryptData(string encryptedText)
> {
> DES des = new DESCryptoServiceProvider();
> des.Mode = CipherMode.ECB;
> des.Padding = PaddingMode.PKCS7;
> des.Key = Encoding.UTF8.GetBytes(_secretPhrase.Substring(0, 8));
> des.IV = System.Text.Encoding.UTF8.GetBytes(_secretPhrase.Substring(0, 8));
>
> byte[] bytes = Convert.FromBase64String(encryptedText);
> byte[] resultBytes = des.CreateDecryptor().TransformFinalBlock(bytes, 0, bytes.Length);
>
> return Encoding.UTF8.GetString(resultBytes);
> }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您似乎没有使用 BouncyCastle。这看起来像是普通的 .NET Framework 加密代码。 BouncyCastle 使用 DesEngine 等命名约定。
我建议您尝试将 BouncyCastle 用于 C# 和 Java。我不相信字节差异会成为一个实际问题。图书馆应该正确解释这一点。
此外,单一 DES 现在被认为非常弱。在合理的硬件上存在针对它的单日攻击,并且它不再是经过批准的标准。看看 AES 吧。
最后,我不确定使用相同的密钥和 IV 是否明智。我不是密码学专家,但使用两个不同的随机数应该更安全。
It doesn't look like you are using BouncyCastle. That looks like stock .NET Framework cryptography code. BouncyCastle uses naming conventions like DesEngine.
I recommend that you try using BouncyCastle for both C# and Java. I'm not convinced the byte difference will be a practical problem. The library should account for it correctly.
Also, single DES is now considered very weak. There are single day attacks against it on reasonable hardware, and it's no longer an approved standard. Look at AES instead.
Finally, I'm not sure using the same key and IV are wise. I'm not a cryptography expert, but using two distinct random numbers should be safer.