Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用PHP解密C#(密码)中创建的字符串?_C#_Php_Encryption - Fatal编程技术网

用PHP解密C#(密码)中创建的字符串?

用PHP解密C#(密码)中创建的字符串?,c#,php,encryption,C#,Php,Encryption,我正在使用下面的类来编写字符串。 现在我需要用PHP解码它,但我很难实现它。但首先,你知道这是否可能吗 我还可以在php中使用相同的密钥吗? 任何想法或任何帮助都将不胜感激 using System; using System.IO; using System.Security.Cryptography; using System.Text; namespace HySoftLocalApiNetCore { class Cipher { /// <su

我正在使用下面的类来编写字符串。 现在我需要用PHP解码它,但我很难实现它。但首先,你知道这是否可能吗

我还可以在php中使用相同的密钥吗? 任何想法或任何帮助都将不胜感激

using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace HySoftLocalApiNetCore
{
    class Cipher
    {

        /// <summary>
        /// Encrypt a string.
        /// </summary>
        /// <param name="plainText">String to be encrypted</param>
        /// <param name="password">Password</param>
        public static string Encrypt(string plainText, string password)
        {
            if (plainText == null)
            {
                return null;
            }

            if (password == null)
            {
                password = String.Empty;
            }

            // Get the bytes of the string
            var bytesToBeEncrypted = Encoding.UTF8.GetBytes(plainText);
            var passwordBytes = Encoding.UTF8.GetBytes(password);

            // Hash the password with SHA256
            passwordBytes = SHA256.Create().ComputeHash(passwordBytes);

            var bytesEncrypted = Cipher.Encrypt(bytesToBeEncrypted, passwordBytes);

            return Convert.ToBase64String(bytesEncrypted);
        }

        /// <summary>
        /// Decrypt a string.
        /// </summary>
        /// <param name="encryptedText">String to be decrypted</param>
        /// <param name="password">Password used during encryption</param>
        /// <exception cref="FormatException"></exception>
        public static string Decrypt(string encryptedText, string password)
        {
            if (encryptedText == null)
            {
                return null;
            }

            if (password == null)
            {
                password = String.Empty;
            }

            // Get the bytes of the string
            var bytesToBeDecrypted = Convert.FromBase64String(encryptedText);
            var passwordBytes = Encoding.UTF8.GetBytes(password);

            passwordBytes = SHA256.Create().ComputeHash(passwordBytes);

            var bytesDecrypted = Cipher.Decrypt(bytesToBeDecrypted, passwordBytes);

            return Encoding.UTF8.GetString(bytesDecrypted);
        }

        private static byte[] Encrypt(byte[] bytesToBeEncrypted, byte[] passwordBytes)
        {
            byte[] encryptedBytes = null;

            // Set your salt here, change it to meet your flavor:
            // The salt bytes must be at least 8 bytes.
            var saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };

            using (MemoryStream ms = new MemoryStream())
            {
                using (RijndaelManaged AES = new RijndaelManaged())
                {
                    var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000);

                    AES.KeySize = 256;
                    AES.BlockSize = 128;
                    AES.Key = key.GetBytes(AES.KeySize / 8);
                    AES.IV = key.GetBytes(AES.BlockSize / 8);

                    AES.Mode = CipherMode.CBC;

                    using (var cs = new CryptoStream(ms, AES.CreateEncryptor(), CryptoStreamMode.Write))
                    {
                        cs.Write(bytesToBeEncrypted, 0, bytesToBeEncrypted.Length);
                        cs.Close();
                    }

                    encryptedBytes = ms.ToArray();
                }
            }

            return encryptedBytes;
        }

        private static byte[] Decrypt(byte[] bytesToBeDecrypted, byte[] passwordBytes)
        {
            byte[] decryptedBytes = null;

            // Set your salt here, change it to meet your flavor:
            // The salt bytes must be at least 8 bytes.
            var saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };

            using (MemoryStream ms = new MemoryStream())
            {
                using (RijndaelManaged AES = new RijndaelManaged())
                {
                    var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000);

                    AES.KeySize = 256;
                    AES.BlockSize = 128;
                    AES.Key = key.GetBytes(AES.KeySize / 8);
                    AES.IV = key.GetBytes(AES.BlockSize / 8);
                    AES.Mode = CipherMode.CBC;

                    using (var cs = new CryptoStream(ms, AES.CreateDecryptor(), CryptoStreamMode.Write))
                    {
                        cs.Write(bytesToBeDecrypted, 0, bytesToBeDecrypted.Length);
                        cs.Close();
                    }

                    decryptedBytes = ms.ToArray();
                }
            }

            return decryptedBytes;
        }

    }
}
使用系统;
使用System.IO;
使用System.Security.Cryptography;
使用系统文本;
LocalApinetCore名称空间
{
类密码
{
/// 
///加密字符串。
/// 
///要加密的字符串
///密码
公共静态字符串加密(字符串明文、字符串密码)
{
if(纯文本==null)
{
返回null;
}
如果(密码==null)
{
password=String.Empty;
}
//获取字符串的字节数
var bytesToBeEncrypted=Encoding.UTF8.GetBytes(纯文本);
var passwordBytes=Encoding.UTF8.GetBytes(密码);
//用SHA256散列密码
passwordBytes=SHA256.Create().ComputeHash(passwordBytes);
var bytesEncrypted=Cipher.Encrypt(bytesToBeEncrypted,passwordBytes);
返回Convert.tobase64字符串(字节加密);
}
/// 
///解密字符串。
/// 
///要解密的字符串
///加密期间使用的密码
/// 
公共静态字符串解密(字符串加密文本、字符串密码)
{
如果(encryptedText==null)
{
返回null;
}
如果(密码==null)
{
password=String.Empty;
}
//获取字符串的字节数
var bytesToBeDecrypted=Convert.FromBase64String(encryptedText);
var passwordBytes=Encoding.UTF8.GetBytes(密码);
passwordBytes=SHA256.Create().ComputeHash(passwordBytes);
var bytesDecrypted=Cipher.Decrypt(bytesToBeDecrypted,passwordBytes);
返回Encoding.UTF8.GetString(字节解密);
}
私有静态字节[]加密(字节[]bytesToBeEncrypted,字节[]passwordBytes)
{
字节[]encryptedBytes=null;
//将盐放在这里,根据您的口味进行更换:
//salt字节必须至少为8字节。
var saltBytes=新字节[]{1,2,3,4,5,6,7,8};
使用(MemoryStream ms=new MemoryStream())
{
使用(RijndaelManaged AES=new RijndaelManaged())
{
var key=新的Rfc2898DeriveBytes(passwordBytes,saltBytes,1000);
AES.KeySize=256;
AES.BlockSize=128;
AES.Key=Key.GetBytes(AES.KeySize/8);
AES.IV=key.GetBytes(AES.BlockSize/8);
AES.Mode=CipherMode.CBC;
使用(var cs=new CryptoStream(ms,AES.CreateEncryptor(),CryptoStreamMode.Write))
{
写(bytesToBeEncrypted,0,bytesToBeEncrypted.Length);
cs.Close();
}
encryptedBytes=ms.ToArray();
}
}
返回encryptedBytes;
}
私有静态字节[]解密(字节[]bytesToBeDecrypted,字节[]passwordBytes)
{
byte[]decryptedBytes=null;
//将盐放在这里,根据您的口味进行更换:
//salt字节必须至少为8字节。
var saltBytes=新字节[]{1,2,3,4,5,6,7,8};
使用(MemoryStream ms=new MemoryStream())
{
使用(RijndaelManaged AES=new RijndaelManaged())
{
var key=新的Rfc2898DeriveBytes(passwordBytes,saltBytes,1000);
AES.KeySize=256;
AES.BlockSize=128;
AES.Key=Key.GetBytes(AES.KeySize/8);
AES.IV=key.GetBytes(AES.BlockSize/8);
AES.Mode=CipherMode.CBC;
使用(var cs=new CryptoStream(ms,AES.CreateDecryptor(),CryptoStreamMode.Write))
{
Write(bytesToBeDecrypted,0,bytesToBeDecrypted.Length);
cs.Close();
}
decryptedBytes=ms.ToArray();
}
}
返回解密字节;
}
}
}

要使用,我只需传递字符串和密码,也要解密,使用起来非常简单。

如果你能解密密码,你就不能安全地存储你的密码。@user3783243我认为你需要更仔细地检查这个示例。你现有的代码是不安全的。它把钥匙当作静脉注射(恶心)。首先解决这个问题。此外,如果您试图将代码转换为PHP,我们不会为您编写代码。为什么要这样做,为什么不能使用HTTPS?您可能的副本应该能够使用
AES-256-CBC
SHA256
对数据进行解密。如果您向我们展示您的PHP代码,我可能会提供更多帮助。