c# aes加密怎么用java解密?

weixin_38606033 2018-02-24 10:00:40
以下是我公司13年c#开发人员写的ACCESS数据库字段加密的程序,现在有个需求,就是将c#加密的数据用java解密出来。该代码片段中的解密方法Decrypt()用java代替掉。代码如下,有大牛帮帮忙吗?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;

namespace EDI.cryptor
{
public class Encryptor
{
// Fields
private static string key = "121212121212";
private static SymmetricAlgorithm mobjCryptoService = new RijndaelManaged();

// Methods
public static string Decrypt(string Source)
{
try
{
byte[] buffer = Convert.FromBase64String(Source);
MemoryStream stream = new MemoryStream(buffer, 0, buffer.Length);
mobjCryptoService.Key = GetLegalKey();
mobjCryptoService.IV = GetLegalIV();
ICryptoTransform transform = mobjCryptoService.CreateDecryptor();
CryptoStream stream2 = new CryptoStream(stream, transform, CryptoStreamMode.Read);
StreamReader reader = new StreamReader(stream2);
return reader.ReadToEnd();
}
catch (Exception)
{
}
return null;
}

public static string Encrypt(string Source)
{
byte[] bytes = Encoding.UTF8.GetBytes(Source);
MemoryStream stream = new MemoryStream();
mobjCryptoService.Key = GetLegalKey();
mobjCryptoService.IV = GetLegalIV();
ICryptoTransform transform = mobjCryptoService.CreateEncryptor();
CryptoStream stream2 = new CryptoStream(stream, transform, CryptoStreamMode.Write);
stream2.Write(bytes, 0, bytes.Length);
stream2.FlushFinalBlock();
stream.Close();
return Convert.ToBase64String(stream.ToArray());
}

private static byte[] GetLegalIV()
{
string s = "34343434343434343434343434343434";
mobjCryptoService.GenerateIV();
int length = mobjCryptoService.IV.Length;
if (s.Length > length)
{
s = s.Substring(0, length);
}
else if (s.Length < length)
{
s = s.PadRight(length, ' ');
}
return Encoding.ASCII.GetBytes(s);
}

private static byte[] GetLegalKey()
{
mobjCryptoService.GenerateKey();
int length = mobjCryptoService.Key.Length;
if (key.Length > length)
{
key = key.Substring(0, length);
}
else if (key.Length < length)
{
key = key.PadRight(length, ' ');
}
return Encoding.ASCII.GetBytes(key);
}


}
}
...全文
263 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
weixin_38606033 2019-05-18
  • 打赏
  • 举报
回复
谢谢哦。该问题已经解决了!
attilax 2018-02-24
  • 打赏
  • 举报
回复
c# java解密aes方法默认参数不同,不能直接使用java解密法。。建议使用c#写个解密,加个cli接口,通过java调用。。后者powershell可以直接调用。net类库,用powersershll写个cli接口即可。。

50,526

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧