weixin_45777754 2020-10-11 21:41 采纳率: 0%
浏览 71

php解密C# tripledes加密

各位大神如果用PHP实现下面C#类中的Decrypt方法该怎样写?试了好久没能实现

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;

namespace Common.Utilities
{
    /// <summary>
    /// 类名称   :CryptTools
    /// 类说明   :加解密算法
    /// 作者     :
    /// 完成日期 :
    /// </summary>
    public static class CryptTools
{
/// <summary>
    /// Speaker密钥字符串
    /// </summary>
public static string keykey = "123456";

/// <summary>
    /// 方法说明 :加密方法
    /// 作者     :
    /// 完成日期 :
    /// </summary>
/// <param name="content">需要加密的明文内容</param>
/// <returns>返回加密后密文字符串</returns>
public static string Encrypt(this string content)
{
return Encrypt(content, keykey);
}

/// <summary>
    /// 方法说明 :加密方法
    /// 作者     :
    /// 完成日期 :
    /// </summary>
/// <param name="content">需要加密的明文内容</param>
/// <param name="secret">加密密钥</param>
/// <returns>返回加密后密文字符串</returns>
public static string Encrypt(this string content, string secret)
{
if ((content == null) || string.IsNullOrEmpty(secret) || (content.Length == 0) || (secret.Length == 0))
{
//throw new ArgumentNullException("Invalid Argument");
return string.Empty;
}

byte[] Key = GetKey(secret);
byte[] ContentByte = Encoding.Unicode.GetBytes(content);
MemoryStream MSTicket = new MemoryStream();

MSTicket.Write(ContentByte, 0, ContentByte.Length);

byte[] ContentCryptByte = Crypt(MSTicket.ToArray(), Key);

string ContentCryptStr = Encoding.ASCII.GetString(Base64Encode(ContentCryptByte));

return ContentCryptStr;
}

/// <summary>
    /// 方法说明 :加密方法
    /// 作者     :
    /// 完成日期 :
    /// </summary>
/// <param name="content">需要加密的明文内容</param>
/// <returns>返回加密后密文字符串</returns>
public static string EncryptUTF8(this string content)
{
return EncryptUTF8(content, keykey);
}
/// <summary>
    /// 方法说明 :加密方法
    /// 作者     :
    /// 完成日期 :
    /// </summary>
/// <param name="content">需要加密的明文内容</param>
/// <param name="secret">加密密钥</param>
/// <returns>返回加密后密文字符串</returns>
public static string EncryptUTF8(this string content, string secret)
{
if ((content == null) || (secret == null) || (content.Length == 0) || (secret.Length == 0))
{
//throw new ArgumentNullException("Invalid Argument");
return string.Empty;
}

byte[] Key = GetKey(secret);
byte[] ContentByte = Encoding.UTF8.GetBytes(content);
MemoryStream MSTicket = new MemoryStream();

MSTicket.Write(ContentByte, 0, ContentByte.Length);

byte[] ContentCryptByte = Crypt(MSTicket.ToArray(), Key);

string ContentCryptStr = Encoding.ASCII.GetString(Base64Encode(ContentCryptByte));

return ContentCryptStr;
}

/// <summary>
    /// 方法说明 :解密方法
    /// 作者     :
    /// 完成日期 :
    /// </summary>
/// <param name="content">需要解密的密文内容</param>
/// <returns>返回解密后明文字符串</returns>
public static string DecryptUTF8(this string content)
{
return DecryptUTF8(content, keykey);
}

/// <summary>
    /// 方法说明 :解密方法
    /// 作者     :
    /// 完成日期 :
    /// </summary>
/// <param name="content">需要解密的密文内容</param>
/// <param name="secret">解密密钥</param>
/// <returns>返回解密后明文字符串</returns>
public static string DecryptUTF8(this string content, string secret)
{
if ((content == null) || (secret == null) || (content.Length == 0) || (secret.Length == 0))
{
//throw new ArgumentNullException("Invalid Argument");
return string.Empty;
}

byte[] Key = GetKey(secret);

byte[] CryByte = Base64Decode(Encoding.ASCII.GetBytes(content));
byte[] DecByte = Decrypt(CryByte, Key);

byte[] RealDecByte;
string RealDecStr;

RealDecByte = DecByte;
byte[] Prefix = new byte[Constants.Operation.UnicodeReversePrefix.Length];
Array.Copy(RealDecByte, Prefix, 2);

if (CompareByteArrays(Constants.Operation.UnicodeReversePrefix, Prefix))
{
byte SwitchTemp = 0;
for (int i = 0; i < RealDecByte.Length - 1; i = i + 2)
{
SwitchTemp = RealDecByte[i];
RealDecByte[i] = RealDecByte[i + 1];
RealDecByte[i + 1] = SwitchTemp;
}
}

RealDecStr = Encoding.UTF8.GetString(RealDecByte);
return RealDecStr;
}

/// <summary>
    /// 方法说明 :解密方法
    /// 作者     :
    /// 完成日期 :
    /// </summary>
/// <param name="content">需要解密的密文内容</param>
/// <returns>返回解密后明文字符串</returns>
public static string Decrypt(this string content)
{
return Decrypt(content, keykey);
}

/// <summary>
    /// 方法说明 :解密方法
    /// 作者     :
    /// 完成日期 :
    /// </summary>
/// <param name="content">需要解密的密文内容</param>
/// <param name="secret">解密密钥</param>
/// <returns>返回解密后明文字符串</returns>
public static string Decrypt(this string content, string secret)
{
try
{
byte[] Key = GetKey(secret);

byte[] CryByte = Base64Decode(Encoding.ASCII.GetBytes(content));
byte[] DecByte = Decrypt(CryByte, Key);

byte[] RealDecByte;

RealDecByte = DecByte;
byte[] Prefix = new byte[Constants.Operation.UnicodeReversePrefix.Length];
Array.Copy(RealDecByte, Prefix, 2);

if (CompareByteArrays(Constants.Operation.UnicodeReversePrefix, Prefix))
{
byte SwitchTemp = 0;
for (int i = 0; i < RealDecByte.Length - 1; i = i + 2)
{
SwitchTemp = RealDecByte[i];
RealDecByte[i] = RealDecByte[i + 1];
RealDecByte[i + 1] = SwitchTemp;
}
}

return Encoding.Unicode.GetString(RealDecByte);
}
catch { }
return content;
}

#region Url参数加密方法
/// <summary>
    /// Url参数加密密钥
    /// </summary>
public const string QueryStringEncryptKey = "Bayer_Pass@word_QSEK_KEY";
/// <summary>
    /// 加密Url参数
    /// </summary>
/// <param name="Params"></param>
/// <returns></returns>
public static string EncryptParams(string Params)
{
try
{
return System.Web.HttpUtility.UrlEncode(CryptTools.Encrypt(Params, QueryStringEncryptKey));
}
catch { }

return string.Empty;
}
/// <summary>
    /// 解密Url参数
    /// </summary>
/// <param name="Params"></param>
/// <returns></returns>
public static string DecryptParams(string Params)
{
try
{
return CryptTools.Decrypt(Params, QueryStringEncryptKey);
}
catch { }

return string.Empty;
}
#endregion

//使用TripleDES加密
public static byte[] Crypt(byte[] source, byte[] key)
{
if ((source.Length == 0) || (source == null) || (key == null) || (key.Length == 0))
{
throw new ArgumentException("Invalid Argument");
}

TripleDESCryptoServiceProvider dsp = new TripleDESCryptoServiceProvider();
dsp.Mode = CipherMode.ECB;

ICryptoTransform des = dsp.CreateEncryptor(key, null);

return des.TransformFinalBlock(source, 0, source.Length);
}

public static byte[] Decrypt(byte[] source, byte[] key)
{
if ((source.Length == 0) || (source == null) || (key == null) || (key.Length == 0))
{
throw new ArgumentNullException("Invalid Argument");
}

TripleDESCryptoServiceProvider dsp = new TripleDESCryptoServiceProvider();
dsp.Mode = CipherMode.ECB;

ICryptoTransform des = dsp.CreateDecryptor(key, null);

byte[] ret = new byte[source.Length + 8];

int num;
num = des.TransformBlock(source, 0, source.Length, ret, 0);

ret = des.TransformFinalBlock(source, 0, source.Length);
ret = des.TransformFinalBlock(source, 0, source.Length);
num = ret.Length;

byte[] RealByte = new byte[num];
Array.Copy(ret, RealByte, num);
ret = RealByte;
return ret;
}

//原始base64编码
public static byte[] Base64Encode(byte[] source)
{
if ((source == null) || (source.Length == 0))
throw new ArgumentException("source is not valid");

ToBase64Transform tb64 = new ToBase64Transform();
MemoryStream stm = new MemoryStream();
int pos = 0;
byte[] buff;

while (pos + 3 < source.Length)
{
buff = tb64.TransformFinalBlock(source, pos, 3);
stm.Write(buff, 0, buff.Length);
pos += 3;
}

buff = tb64.TransformFinalBlock(source, pos, source.Length - pos);
stm.Write(buff, 0, buff.Length);

return stm.ToArray();

}

//原始base64解码
public static byte[] Base64Decode(byte[] source)
{
if ((source == null) || (source.Length == 0))
throw new ArgumentException("source is not valid");

FromBase64Transform fb64 = new FromBase64Transform();
MemoryStream stm = new MemoryStream();
int pos = 0;
byte[] buff;

while (pos + 4 < source.Length)
{
buff = fb64.TransformFinalBlock(source, pos, 4);
stm.Write(buff, 0, buff.Length);
pos += 4;
}

buff = fb64.TransformFinalBlock(source, pos, source.Length - pos);
stm.Write(buff, 0, buff.Length);
return stm.ToArray();

}

public static byte[] GetKey(string secret)
{
if ((secret == null) || (secret.Length == 0))
throw new ArgumentException("Secret is not valid");

byte[] temp;

ASCIIEncoding ae = new ASCIIEncoding();
temp = Hash(ae.GetBytes(secret));

byte[] ret = new byte[Constants.Operation.KeySize];

int i;

if (temp.Length < Constants.Operation.KeySize)
{
System.Array.Copy(temp, 0, ret, 0, temp.Length);
for (i = temp.Length; i < Constants.Operation.KeySize; i++)
{
ret[i] = 0;
}
}
else
System.Array.Copy(temp, 0, ret, 0, Constants.Operation.KeySize);

return ret;
}

//比较两个byte数组是否相同
public static bool CompareByteArrays(byte[] source, byte[] dest)
{
if ((source == null) || (dest == null))
throw new ArgumentException("source or dest is not valid");

bool ret = true;

if (source.Length != dest.Length)
return false;
else
if (source.Length == 0)
return true;

for (int i = 0; i < source.Length; i++)
if (source[i] != dest[i])
{
ret = false;
break;
}
return ret;
}

//使用md5计算散列
public static byte[] Hash(byte[] source)
{
if ((source == null) || (source.Length == 0))
throw new ArgumentException("source is not valid");

MD5 m = MD5.Create();
return m.ComputeHash(source);
}


/// <summary>
    /// 计算MD5
    /// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static string GetMD5(String s)
{
if (string.IsNullOrEmpty(s)) return "";
System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(s);
bytes = md5.ComputeHash(bytes);
md5.Clear();

StringBuilder strReturn = new StringBuilder();

for (int i = 0; i < bytes.Length; i++)
{
strReturn.Append(Convert.ToString(bytes[i], 16).PadLeft(2, '0'));
}

return strReturn.ToString().PadLeft(32, '0');
}

/// <summary>
    /// 对传入的明文密码进行Hash加密,密码不能为中文
    /// </summary>
/// <param name="oriPassword">需要加密的明文密码</param>
/// <returns>经过Hash加密的密码</returns>
public static string HashPassword(string oriPassword)
{
if (string.IsNullOrEmpty(oriPassword))
throw new ArgumentException("oriPassword is valid");

ASCIIEncoding acii = new ASCIIEncoding();
byte[] hashedBytes = Hash(acii.GetBytes(oriPassword));

StringBuilder sb = new StringBuilder(30);
foreach (byte b in hashedBytes)
{
sb.AppendFormat("{0:X2}", b);
}
return sb.ToString();
}

}

/// <summary>
    /// 类名称   :Constants
    /// 类说明   :加解密算法常量.
    /// 作者     :
    /// 完成日期 :
    /// </summary>
public class Constants
{
public struct Operation
{
public static readonly int KeySize = 24;
public static readonly byte[] UnicodeOrderPrefix = new byte[2] { 0xFF, 0xFE };
public static readonly byte[] UnicodeReversePrefix = new byte[2] { 0xFE, 0xFF };
}
}
}
  • 写回答

2条回答 默认 最新

  • dabocaiqq 2020-10-11 23:47
    关注
    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?