c# AES加密与java的不同;

weixin_42362748 2018-06-29 03:49:58


string param = "{\"idcard\":\"332529196209241717\",\"mobile\":\"13520507685\",\"name\":\"吴神木\"}";
string key="1833ad3ad741456db435163fc5de7e76"
public static string AesEncrypt(string encryptStr, string key)
{
Byte[] keyArray = UTF8Encoding.UTF8.GetBytes(key);
byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(encryptStr);

RijndaelManaged rDel = new RijndaelManaged();
rDel.Key = keyArray;
rDel.Mode = CipherMode.ECB;
rDel.Padding = PaddingMode.Zeros;

ICryptoTransform cTransform = rDel.CreateEncryptor();
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

//return Convert.ToBase64String(resultArray, 0, resultArray.Length);
return ByteArrayToHexString(resultArray);
}

java进行aes加密后的结果为:
AD15C6505362F5E88F92AE89854328CC097790DA1B7DD0361197A9039326239AC73E05605130A696BF99F8544B8BF8680681A29CA3E992D4122BF3ACAE51FC88BAA010AC2B5F07748637792A50D97CEE
但是用C#加密后为:
3A53B8D4FC6667602A9F4C38B047668F5CDEE2A4F65B2E34E05ACF3D99FCA5965562FB12FA9F559C2428C840E7B094A7248AA533222F19A264586ABEA531CCE5D04D0CD8BA30D17545F056300A5A3B4B
java加密代码如下:

public static String encode(String key, String data) {
try {
SecretKey secretKey = new SecretKeySpec(hex2bin(key), "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] bytes = cipher.doFinal(data.getBytes());
return bin2hex(bytes);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 将2进制转换成16进制
*
* @param bin
* 2进制数组
* @return 16进制字符串
*/
private static String bin2hex(byte[] bin) throws Exception {
StringBuffer buf = new StringBuffer();
for (int i = 0; i < bin.length; i++) {
String hex = Integer.toHexString(bin[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
buf.append(hex.toUpperCase());
}
return buf.toString();
}

/**
* 将16进制转换为2进制
*
* @param hex
* 16进制字符串
* @return 2进制数组
*/
private static byte[] hex2bin(String hex) throws Exception {
if (hex.length() < 1)
return null;
byte[] result = new byte[hex.length() / 2];
for (int i = 0; i < hex.length() / 2; i++) {
int high = Integer.parseInt(hex.substring(i * 2, i * 2 + 1), 16);
int low = Integer.parseInt(hex.substring(i * 2 + 1, i * 2 + 2), 16);
result[i] = (byte) (high * 16 + low);
}
return result;
}

...全文
1202 26 打赏 收藏 转发到动态 举报
写回复
用AI写文章
26 条回复
切换为时间正序
请发表友善的回复…
发表回复
维秀斯丢丢 2018-07-04
  • 打赏
  • 举报
回复
public static String Encrypt(String str ,String key) {
try {
if (str == null || key == null) return null;
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key.getBytes("utf-8"), "AES"));
byte[] bytes = cipher.doFinal(str.getBytes("utf-8"));
return new BASE64Encoder().encode(bytes);
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
维秀斯丢丢 2018-07-04
  • 打赏
  • 举报
回复
嗯 我做过, 两个操作对应关系不一样。
  • 打赏
  • 举报
回复
UTF8Encoding.UTF8.GetBytes(encryptStr);
改为
System.Text.Encoding.GetEncoding("gb2312").GetBytes(encryptStr);

一点动手能力都没?
of123 2018-07-03
  • 打赏
  • 举报
回复
AES 是对称密码算法,不存在公钥和私钥,只有共享的秘密密钥。
of123 2018-07-03
  • 打赏
  • 举报
回复
AES 是分组加密算法之一,它要求的加解密的数据长度是分组长度(对于 AES 是 16 字节)的整数倍。

建议你采用流加密的工作方式,例如用 CBC 模式加密连续的 0x00 分组,得到足够长度的“流密钥”。

截取与数据等长的流密钥:流密钥与明文异或,就得到密文;与密文异或就得到明文。

无论加密还是加密,你都采用 AES 加密模式即可。

上述实现,实际上是 AES 的 OFB 模式。
threenewbee 2018-07-02
  • 打赏
  • 举报
回复
主要是你的private key要对应上
xuzuning 2018-07-02
  • 打赏
  • 举报
回复
嗯,是充填方式的问题
将 AesEncrypt 方法中的
rDel.Padding = PaddingMode.Zeros;
改为
rDel.Padding = PaddingMode.PKCS7;

解码可以不改,因为 C# 要宽松的多(你的 data_aes_net 和 data_aes_java 都可正常解码)
weixin_42362748 2018-07-02
  • 打赏
  • 举报
回复

protected void Button8_Click(object sender, EventArgs e)
{
string data_aes_net = "AD15C6505362F5E88F92AE89854328CC097790DA1B7DD0361197A9039326239AC73E05605130A696BF99F8544B8BF8680681A29CA3E992D4122BF3ACAE51FC88E8DAADA268ED8C6FE1E207FC6F9578D9";
string data_aes_java= "AD15C6505362F5E88F92AE89854328CC097790DA1B7DD0361197A9039326239AC73E05605130A696BF99F8544B8BF8680681A29CA3E992D4122BF3ACAE51FC88BAA010AC2B5F07748637792A50D97CEE";
string result_aes_net = AesDecrypt(data_aes_net, "1833ad3ad741456db435163fc5de7e76");
string result_aes_java = AesDecrypt(data_aes_java, "1833ad3ad741456db435163fc5de7e76");
Response.Write(result_aes_net+"<br>"+result_aes_java);
}


我用C#解密这2个字符串,得到同样的结果,但是用java,第一个就解密不了,第二个可以正常解密;
weixin_42362748 2018-07-02
  • 打赏
  • 举报
回复
@xuzuning 用java解不开,但是我用C#的就可以解开;
tianfang 2018-07-02
  • 打赏
  • 举报
回复
这个两个参数不一致的问题 都搞成显式设置

rDel.Mode = CipherMode.ECB;
rDel.Padding = PaddingMode.Zeros;
weixin_42584046 2018-07-01
  • 打赏
  • 举报
回复
java的byte和.net的表示范围不一样,你查一下
xuzuning 2018-07-01
  • 打赏
  • 举报
回复
你把 C# 加密的结果,拿到 Java 里解密看看
weixin_42362748 2018-07-01
  • 打赏
  • 举报
回复
up 顶一下。
weixin_42362748 2018-06-29
  • 打赏
  • 举报
回复
那怎么改那?
  • 打赏
  • 举报
回复
一般来说就是
System.Text.Encoding.GetEncoding("gb2312")
  • 打赏
  • 举报
回复
引用 6 楼 weixin_42362748 的回复:
是不是汉字的问题导致的;
如果涉及到非英文字符建议先转码(Unicode或其他你熟悉的编码)后再加密解密。
  • 打赏
  • 举报
回复
data.getBytes()
对应C#下的
System.Text.Encoding.Default
weixin_42362748 2018-06-29
  • 打赏
  • 举报
回复
是不是汉字的问题导致的;
weixin_42362748 2018-06-29
  • 打赏
  • 举报
回复
结果还是不一样,后面的十几个字符不一样;
weixin_42362748 2018-06-29
  • 打赏
  • 举报
回复
我没明白原理。
加载更多回复(2)

110,537

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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