Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/256.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_Blowfish - Fatal编程技术网

将PHP中的解密代码转换为C#

将PHP中的解密代码转换为C#,c#,php,encryption,blowfish,C#,Php,Encryption,Blowfish,我需要将PHP中的河豚描述代码转换为C# PHP代码(X-Cart) 函数mdecrypt($data,$key){ $td=mcrypt_模块_打开(mcrypt_河豚,”,mcrypt_模式_ECB,”); $decrypted_data=mdecrypt_generic($td,func_hex2str($data)); 返回$decrypted_数据; } # #将十六进制转换为字符串 # 函数func_hex2str($str){ $ret=“”; $l=strlen($str);

我需要将PHP中的河豚描述代码转换为C#

PHP代码(X-Cart)

函数mdecrypt($data,$key){
$td=mcrypt_模块_打开(mcrypt_河豚,”,mcrypt_模式_ECB,”);
$decrypted_data=mdecrypt_generic($td,func_hex2str($data));
返回$decrypted_数据;
}
#
#将十六进制转换为字符串
# 
函数func_hex2str($str){
$ret=“”;
$l=strlen($str);
对于($i=0;$i<$l;$i+=2){
$ret.=chr(hexdec(substr$str,$i,2));
}     
echo$ret;
返回$ret;
}
我试着

BlowFish algo = new BlowFish("0cb12a77dbb5ee7128ad3aea6154614f");
string details = "138b5a7e2c0e453a"; 

int dLen = details.Length;
string ret = "";
for (int i = 0; i < dLen; i += 2)
{
    ret += (char) Convert.ToInt64(details.Substring(i, 2), 16);
}
details = algo.Decrypt_ECB(ret);
Console.WriteLine(details); 
BlowFish algo=新河豚(“0cb12a77dbb5ee7128ad3aea6154614f”);
字符串详细信息=“138b5a7e2c0e453a”;
int dLen=详细信息。长度;
字符串ret=“”;
对于(int i=0;i
但是它在PHP和C#之间打印不同的结果。(PHP:HELLO,C:?q??)

有人给我一个提示,他说可能是关于“填充”

但我还是不明白

有人知道我做错了什么吗?请告诉我

谢谢。

是河豚加密算法的开源实现。你可以用这些源代码来看看它是如何完成的

如果你只是为了工作和完成事情而需要它,那么非常简单:

string salt = BCryptHelper.GenerateSalt(6);
var hashedPassword = BCryptHelper.HashPassword("password", salt);
Console.WriteLine(BCryptHelper.CheckPassword("password", hashedPassword));

您可以找到源代码:

///使用OpenBSD bcrypt方案对密码进行哈希运算。
///当一个或多个参数不受支持或
///非法值。
///要哈希的密码。
///要散列的盐(可能使用BCrypt.gensalt生成)。
///散列密码
公共静态字符串HashPassword(字符串输入,字符串salt)
{
如果(输入==null)
抛出新的ArgumentNullException(“输入”);
if(string.IsNullOrEmpty(salt))
抛出新的ArgumentException(“无效的salt”、“salt”);
//确定起始偏移量并验证salt
int启动偏移量;
char minor=(char)0;
if(salt[0]!='$'| | salt[1]!='2')
抛出新的SaltParseException(“无效的salt版本”);
if(salt[2]=='$')
起始偏移量=3;
其他的
{
小调=盐[2];
如果(小调!=“a”| salt[3]!=“$”)
抛出新的SaltParseException(“无效的salt修订”);
起始偏移=4;
}
//提取轮数
if(salt[startingOffset+2]>'$')
抛出新的SaltParseException(“缺少盐回合”);
//从盐中提取细节
int logRounds=Convert.ToInt32(salt.Substring(startingOffset,2));
string extractedSalt=salt.Substring(起始偏移量+3,22);
byte[]inputBytes=Encoding.UTF8.GetBytes((输入+(小调>='a'?“\0:”);
字节[]saltBytes=DecodeBase64(extractedSalt,BCRYPT_SALT_LEN);
BCrypt BCrypt=新的BCrypt();
byte[]hashed=bCrypt.CryptRaw(inputBytes、saltBytes、logRounds);
//生成结果字符串
StringBuilder结果=新建StringBuilder();
结果。追加(“$2”);
如果(次要>='a')
结果:追加(次要);
AppendFormat(“${0:00}$”,logRounds);
Append(EncodeBase64(saltBytes,saltBytes.Length));
结果.Append(EncodeBase64(散列,(BfCryptCiphertext.Length*4)-1));
返回result.ToString();
}

请问您使用的是哪一个河豚库?@Joachim Isaksson在C#中,我使用了这个,这是出于教育目的(意思是:您需要解密它并显示您的工作),还是出于实际工作目的?如果是后者,只需使用BCrypt对字符串进行加密。它使用了河豚算法@Serg谢谢你的建议,但是它已经被X-Cart加密了。我如何使用这个代码?你是说,我需要用BCrypt.Net加密吗?或者我可以用BCrypt.Net来解密已经加密的文件?
string salt = BCryptHelper.GenerateSalt(6);
var hashedPassword = BCryptHelper.HashPassword("password", salt);
Console.WriteLine(BCryptHelper.CheckPassword("password", hashedPassword));
    /// <summary>Hash a password using the OpenBSD bcrypt scheme.</summary>
    /// <exception cref="ArgumentException">Thrown when one or more arguments have unsupported or
    ///                                     illegal values.</exception>
    /// <param name="input">The password to hash.</param>
    /// <param name="salt">    the salt to hash with (perhaps generated using BCrypt.gensalt).</param>
    /// <returns>The hashed password</returns>
    public static string HashPassword(string input, string salt)
    {
        if (input == null)
            throw new ArgumentNullException("input");

        if (string.IsNullOrEmpty(salt))
            throw new ArgumentException("Invalid salt", "salt");

        // Determinthe starting offset and validate the salt
        int startingOffset;
        char minor = (char)0;
        if (salt[0] != '$' || salt[1] != '2')
            throw new SaltParseException("Invalid salt version");
        if (salt[2] == '$')
            startingOffset = 3;
        else
        {
            minor = salt[2];
            if (minor != 'a' || salt[3] != '$')
                throw new SaltParseException("Invalid salt revision");
            startingOffset = 4;
        }

        // Extract number of rounds
        if (salt[startingOffset + 2] > '$')
            throw new SaltParseException("Missing salt rounds");

        // Extract details from salt
        int logRounds = Convert.ToInt32(salt.Substring(startingOffset, 2));
        string extractedSalt = salt.Substring(startingOffset + 3, 22);

        byte[] inputBytes = Encoding.UTF8.GetBytes((input + (minor >= 'a' ? "\0" : "")));
        byte[] saltBytes = DecodeBase64(extractedSalt, BCRYPT_SALT_LEN);

        BCrypt bCrypt = new BCrypt();
        byte[] hashed = bCrypt.CryptRaw(inputBytes, saltBytes, logRounds);

        // Generate result string
        StringBuilder result = new StringBuilder();
        result.Append("$2");
        if (minor >= 'a')
            result.Append(minor);
        result.AppendFormat("${0:00}$", logRounds);
        result.Append(EncodeBase64(saltBytes, saltBytes.Length));
        result.Append(EncodeBase64(hashed, (_BfCryptCiphertext.Length * 4) - 1));
        return result.ToString();
    }