概述
基于3des ecb模式加密,密码为24位。
c# JAVA通用
C#代码
/// <summary>
/// C# DES3加密解密
/// </summary>
public class Des3
{
#region ECB模式
byte[] IV = Encoding.UTF8.GetBytes("12345678");
public static string netEncrypt(string content, string key)
{
try
{
var tDes = new TripleDESCryptoServiceProvider
{
Key = Encoding.UTF8.GetBytes(key.Substring(0, 24)),//长度必须24个字节
Mode = CipherMode.ECB,
IV = IV
};
var desEncrypt = tDes.CreateEncryptor();
byte[] buffer = Encoding.UTF8.GetBytes(content);
return Convert.ToBase64String(desEncrypt.TransformFinalBlock(buffer, 0, buffer.Length));
}
catch (Exception e)
{
return string.Empty;
}
}
public static string netDecrypt(string content, string key)
{
try
{
var tDes = new TripleDESCryptoServiceProvider
{
Key = Encoding.UTF8.GetBytes(key.Substring(0, 24)),//长度必须24个字节
Mode = CipherMode.ECB,
Padding = PaddingMode.PKCS7
};
var desDecrypt = tDes.CreateDecryptor();
byte[] buffer = Convert.FromBase64String(content);
var result = Encoding.UTF8.GetString(desDecrypt.TransformFinalBlock(buffer, 0, buffer.Length));
return result;
}
catch (Exception e)
{
return string.Empty;
}
}
#endregion
}
Java代码
**
* java 3des ECB加解密
**/
public class Des3{
public static String get3DESEncryptECB(String src, String secretKey) {
try {
Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
cipher.init(1, new SecretKeySpec(build3DesKey(secretKey), "DESede"));
String base64Encode = getBase64Encode(cipher.doFinal(src.getBytes("UTF-8")));
return filter(base64Encode);
} catch (Exception var4) {
return null;
}
}
public static String get3DESDecryptECB(String src, String secretKey) {
try {
Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
cipher.init(2, new SecretKeySpec(build3DesKey(secretKey), "DESede"));
byte[] base64DValue = getBase64Decode(src);
byte[] ciphertext = cipher.doFinal(base64DValue);
return new String(ciphertext, "UTF-8");
} catch (Exception var5) {
return null;
}
}
public static String getBase64Encode(byte[] src) {
String requestValue = "";
try {
BASE64Encoder base64en = new BASE64Encoder();
requestValue = filter(base64en.encode(src));
} catch (Exception var3) {
var3.printStackTrace();
}
return requestValue;
}
public static byte[] build3DesKey(String keyStr) throws UnsupportedEncodingException {
byte[] key = new byte[24];
byte[] temp = keyStr.getBytes("UTF-8");
if (key.length > temp.length) {
System.arraycopy(temp, 0, key, 0, temp.length);
} else {
System.arraycopy(temp, 0, key, 0, key.length);
}
return key;
}
}
亲测好使
最后
以上就是合适冰淇淋为你收集整理的JAVA C# 3DES ECB加解密,24位密码的全部内容,希望文章能够帮你解决JAVA C# 3DES ECB加解密,24位密码所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复