04-04-22, 09:43 PM
جرب هذا الكود :
PHP كود :
#region EncryptDecryptWord
public string Encrypt(string text, string key)
{
if (Operators.CompareString(text, "", TextCompare: false) == 0)
{
return text;
}
try
{
System.Security.Cryptography.TripleDESCryptoServiceProvider crp = new System.Security.Cryptography.TripleDESCryptoServiceProvider();
UnicodeEncoding uEncode = new UnicodeEncoding();
byte[] bytPlainText = uEncode.GetBytes(text);
System.IO.MemoryStream stmCipherText = new System.IO.MemoryStream();
byte[] slt = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
System.Security.Cryptography.Rfc2898DeriveBytes pdb = new System.Security.Cryptography.Rfc2898DeriveBytes(key, slt);
byte[] bytDerivedKey = pdb.GetBytes(24);
crp.Key = bytDerivedKey;
crp.IV = pdb.GetBytes(8);
System.Security.Cryptography.CryptoStream csEncrypted = new System.Security.Cryptography.CryptoStream(stmCipherText, crp.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write);
csEncrypted.Write(bytPlainText, 0, bytPlainText.Length);
csEncrypted.FlushFinalBlock();
return Convert.ToBase64String(stmCipherText.ToArray());
}
catch (Exception ex)
{
Microsoft.VisualBasic.Interaction.MsgBox(ex.Message, Microsoft.VisualBasic.MsgBoxStyle.Critical, "Error");
return null;
}
}
public string Decrypt(string text, string key)
{
if (Operators.CompareString(text, "", TextCompare: false) == 0)
{
return text;
}
try
{
System.Security.Cryptography.TripleDESCryptoServiceProvider crp = new System.Security.Cryptography.TripleDESCryptoServiceProvider();
UnicodeEncoding uEncode = new UnicodeEncoding();
byte[] bytCipherText = Convert.FromBase64String(text);
System.IO.MemoryStream stmPlainText = new System.IO.MemoryStream();
System.IO.MemoryStream stmCipherText = new System.IO.MemoryStream(bytCipherText);
byte[] slt = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
System.Security.Cryptography.Rfc2898DeriveBytes pdb = new System.Security.Cryptography.Rfc2898DeriveBytes(key, slt);
byte[] bytDerivedKey = pdb.GetBytes(24);
crp.Key = bytDerivedKey;
crp.IV = pdb.GetBytes(8);
System.Security.Cryptography.CryptoStream csDecrypted = new System.Security.Cryptography.CryptoStream(stmCipherText, crp.CreateDecryptor(), System.Security.Cryptography.CryptoStreamMode.Read);
System.IO.StreamWriter sw = new System.IO.StreamWriter(stmPlainText);
System.IO.StreamReader sr = new System.IO.StreamReader(csDecrypted);
sw.Write(sr.ReadToEnd());
sw.Flush();
csDecrypted.Clear();
crp.Clear();
return uEncode.GetString(stmPlainText.ToArray());
}
catch (Exception ex)
{
Microsoft.VisualBasic.Interaction.MsgBox(ex.Message, Microsoft.VisualBasic.MsgBoxStyle.Critical, "Error");
return null;
}
}
#endregion
سبحان الله وبحمده سبحان الله العظيم و الحمد لله ولا اله الا الله والله اكبر
