十万火急,C#兼容java DES加密解密

一一一一 一一一一 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
对方是用Java做DES加密,我们是用C#加密
问题1:对一段比较的长字符串加密后,加密结果后16个字符跟对方对不上
问题2:我的C#解密结果,后面五六个字符串都解不出来,解密出来结果是不完整的

java的代码:


//加密
DataUnit.bytesToHex(DataSecurity.desEncrypt(DataUnit.hexToBytes(key),Rs.toString().getBytes()));
//解密
new String(DataSecurity.desDecrypt(DataUnit.hexToBytes(key), DataUnit.hexToBytes(extraparam))).trim()

static public byte[] hexToBytes(String str) {
int len = str.length() / 2;
str = str.toUpperCase();
byte[] bs = str.getBytes();
byte[] ret = new byte[len];
for (int i = 0; i < len * 2; i += 2) {
byte l = 0, h = 0;
l = bs[i + 1];
h = bs[i];
if (l >= '0' && l <= '9')
l -= '0';
else if (l >= 'A' && l <= 'F') {
l -= 'A';
l += 10;
} else
l = 0;
if (h >= '0' && h <= '9')
h -= '0';
else if (h >= 'A' && h <= 'F') {
h -= 'A';
h += 10;
} else
h = 0;
h = (byte) (h << 4);
ret[i / 2] = (byte) (h | l);
}
return ret;
}

/**
* 二进制字节数组转换为16进制字符串
*
* @param ch
* byte[] 二进制字节数组
* @return String 16进制字串
*/
static public String bytesToHex(byte[] ch, int offset, int length) {
String ret = "";
for (int i = 0; i < length; i++)
ret += byteToHex(ch[i + offset]);
return ret;
}



static public String byteToHex(byte ch) {
String str[] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A",
"B", "C", "D", "E", "F" };
return str[ch >> 4 & 0xF] + str[ch & 0xF];
}


C#代码:


/// <summary>
/// DES解密,返回字符串
/// </summary>
/// <param name="str"></param>
/// <param name="key"></param>
/// <returns></returns>
public static string DESDecodeToStr(string str, string key)
{
return Encoding.GetEncoding("utf-8").GetString(DESDecodeToByte(str, key));
}

/// <summary>
/// DES解密,返回16进制
/// </summary>
/// <param name="str"></param>
/// <param name="key"></param>
/// <returns></returns>
public static string DESDecodeTo16(string str, string key)
{
string sresult = byteToHexStr(DESDecodeToByte(str, key));
return sresult;
}

/// <summary>
/// DES解密,返回字节数组
/// </summary>
/// <param name="str"></param>
/// <param name="key"></param>
/// <returns></returns>
public static byte[] DESDecodeToByte(string str, string key)
{
try
{
byte[] keys = new byte[8];
for (int i = 0; i < 8; i++)
{
keys[i] = Convert.ToByte(key.Substring(i * 2, 2), 16);
}

DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
provider.Mode = CipherMode.ECB;
// 密钥
provider.Key = keys;
// 偏移量
provider.IV = keys;
byte[] buffer = new byte[str.Length / 2];
for (int i = 0; i < (str.Length / 2); i++)
{
int num2 = Convert.ToInt32(str.Substring(i * 2, 2), 0x10);
buffer[i] = Convert.ToByte(str.Substring(i * 2, 2), 16); //(byte)num2;
}
MemoryStream stream = new MemoryStream();
CryptoStream stream2 = new CryptoStream(stream, provider.CreateDecryptor(), CryptoStreamMode.Write);
stream2.Write(buffer, 0, buffer.Length);
stream.Close();
return stream.ToArray();
}
catch (Exception) { return null; }
}


/// <summary>
/// 加密
/// </summary>
/// <param name="data"></param>
/// <param name="Key"></param>
/// <returns></returns>
public static string DESEncrypt16(string data, string Key)
{
try
{
byte[] shuju = System.Text.Encoding.UTF8.GetBytes(data);
byte[] keys = new byte[8];
for (int i = 0; i < 8; i++)
{
keys[i] = Convert.ToByte(Key.Substring(i * 2, 2), 16);
}
DES desEncrypt = new DESCryptoServiceProvider();
desEncrypt.Mode = CipherMode.ECB;
desEncrypt.Key = keys;
byte[] Buffer;
Buffer = shuju;
ICryptoTransform transForm = desEncrypt.CreateEncryptor();
byte[] R;
R = transForm.TransformFinalBlock(Buffer, 0, Buffer.Length);
string return_str = "";
foreach (byte b in R)
{
return_str += b.ToString("X2");
}
//return_str = return_str.Substring(0, 16);
return return_str;
}
catch (Exception e)
{
throw e;
}
}

/// <summary>
/// 字节数组转16进制字符串
/// </summary>
/// <param name="bytes"></param>
/// <returns></returns>
public static string byteToHexStr(byte[] bytes)
{
string returnStr = "";
if (bytes != null)
{
for (int i = 0; i < bytes.Length; i++)
{
returnStr += bytes[i].ToString("X2");
}
}
return returnStr;
}
...全文

相关推荐

技术转移机构在帮助医院转化医疗科技成果时,有哪些标准的操作流程?.docx

技术转移机构在帮助医院转化医疗科技成果时,有哪些标准的操作流程?

BIZKEEN

国家对技术经纪人职业资格认定有哪些最新的考核标准和政策规定?.docx

科易网基于行业知识库+行业大数据+科技资源要素+AI模型,以AI,赋能技术转移、成果转化、技术经理人培训、知识产权管理、区域产业分析与布局、地方科技招商、创新创业大赛等场景,推出科技创新领域的AI工具、场景化服务和数智化解决方案,推动科技创新与产业创新智能化发展。

BIZKEEN

科技成果转化平台哪家服务好?2026年6大机构客户满意度与复购率调研.docx

科技成果转化平台哪家服务好?2026年6大机构客户满意度与复购率调研

BIZKEEN

008_AI运维监控平台整体建设方案.pptx

008_AI运维监控平台整体建设方案.pptx

公众号:优享智库

国企开展产学研合作时,建立长期稳定的技术供给关系有哪些最佳实践案例?.docx

科易网基于行业知识库+行业大数据+科技资源要素+AI模型,以AI,赋能技术转移、成果转化、技术经理人培训、知识产权管理、区域产业分析与布局、地方科技招商、创新创业大赛等场景,推出科技创新领域的AI工具、场景化服务和数智化解决方案,推动科技创新与产业创新智能化发展。

BIZKEEN
评论 1
收藏 已收藏
分享
返回顶部
一一一一
Java EE 67,535 成员 225,843 内容