aes c# java-尊龙游戏旗舰厅官网
///
///用种子获取密钥字节///
/// 密钥种子
/// 编码格式
/// 密钥长度(一般为16,不清楚时不要随意动)
///
public static byte[] getkeybyseed(string strkey, encoding encoding, int nlen = 16)
{byte[] byseed =encoding.getbytes(strkey);byte[] bykeyarray = null;using (var st = newsha1cryptoserviceprovider())
{using (var nd = newsha1cryptoserviceprovider())
{var rd =nd.computehash(st.computehash(byseed));
bykeyarray=rd.take(nlen).toarray();
}
}returnbykeyarray;
}///
///加密 参数:string///
/// 加密内容
/// 密钥字节数组
/// 向量(注意目前只研究支持16位长度)
/// 编码方式
/// string:密文
public static string encrypt(string strcon, byte[] bytekey, encoding encoding)
{try{if (string.isnullorwhitespace(strcon))
{return null;
}byte[] bycon =encoding.getbytes(strcon);var rm = newrijndaelmanaged
{
key=bytekey,
mode=ciphermode.ecb,
padding=paddingmode.pkcs7
};
icryptotransform ctransform=rm.createencryptor();byte[] resultarray = ctransform.transformfinalblock(bycon, 0, bycon.length);returnprogram.bytearraytohexstring(resultarray);
}catch{return "";
}
}public static string decrypt(string strcon, byte[] bytekey,encoding encoding)
{try{if (string.isnullorwhitespace(strcon))
{return null;
}byte[] bycon =hexstrtobyte(strcon);var rm = newrijndaelmanaged
{
key=bytekey,
mode=ciphermode.ecb,
padding=paddingmode.pkcs7
};
icryptotransform ctransform=rm.createdecryptor();byte[] resultarray = ctransform.transformfinalblock(bycon, 0, bycon.length);returnencoding.getstring(resultarray);
}catch(exception ex)
{return "";
}
}///
///
///
///
///
public static byte[] hexstrtobyte(stringhexstring)
{
hexstring= hexstring.replace(" ", "");if ((hexstring.length % 2) != 0)
hexstring = " ";byte[] returnbytes = new byte[hexstring.length / 2];for (int i = 0; i < returnbytes.length; i )
returnbytes[i]= convert.tobyte(hexstring.substring(i * 2, 2), 16);returnreturnbytes;
}///
///转化为16进制///
///
///
public static string bytearraytohexstring(byte[] data)
{
stringbuilder sb= new stringbuilder(data.length * 3);foreach (byte b indata)
{
sb.append(convert.tostring(b,16).padleft(2, '0'));
}returnsb.tostring().toupper();
}
总结
以上是尊龙游戏旗舰厅官网为你收集整理的aes c# java_aes加密,c#和java相同的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: java后台接收数据格式_java后台基
- 下一篇: