更新

我已经作出的改变C#代码,使它使用一块大小的256个。但现在的世界你好看起来像这样 http://pastebin.com/5sXhMV11 我不能找出什么我应该使用与rtrim()得到最混乱的结束。

还当你说IV应该是随机的,通过这个你的意思是不使用相同的四超过一次或者是我编码的错了?

再次感谢!

嗨,

我试图解string PHP这是加密的。我似乎无法得到PHP解密使用mcrypt和可以做一些帮助请。我到下面的错误与php所以我猜我不是设置的四正确。

错误:IV参数必须是作为长作为blocksize

两种功能使用同一密码钥匙、第四和设CBC模式:

加密的文本从c#=UmzUCnAzThH0nMkIuMisqg==
关键32长=qwertyuiopasdfghjklzxcvbnmqwerty
iv16长=1234567890123456

C#

    public static string EncryptString(string message, string KeyString, string IVString)
    {
        byte[] Key = ASCIIEncoding.UTF8.GetBytes(KeyString);
        byte[] IV = ASCIIEncoding.UTF8.GetBytes(IVString);

        string encrypted = null;
        RijndaelManaged rj = new RijndaelManaged();
        rj.Key = Key;
        rj.IV = IV;
        rj.Mode = CipherMode.CBC;

        try
        {
            MemoryStream ms = new MemoryStream();

            using (CryptoStream cs = new CryptoStream(ms, rj.CreateEncryptor(Key, IV), CryptoStreamMode.Write))
            {
                using (StreamWriter sw = new StreamWriter(cs))
                {
                    sw.Write(message);
                    sw.Close();
                }
                cs.Close();
            }
            byte[] encoded = ms.ToArray();
            encrypted = Convert.ToBase64String(encoded);

            ms.Close();
        }
        catch (CryptographicException e)
        {
            Console.WriteLine("A Cryptographic error occurred: {0}", e.Message);
            return null;
        }
        catch (UnauthorizedAccessException e)
        {
            Console.WriteLine("A file error occurred: {0}", e.Message);
            return null;
        }
        catch (Exception e)
        {
            Console.WriteLine("An error occurred: {0}", e.Message);
        }
        finally
        {
            rj.Clear();
        }

        return encrypted;
    }

PHP

var $mcrypt_cipher = MCRYPT_RIJNDAEL_256;
var $mcrypt_mode = MCRYPT_MODE_CBC;

function decrypt($key, $iv, $encrypted)
{
    $encrypted = base64_decode($encrypted);

    $decrypted = rtrim(mcrypt_decrypt($this->mcrypt_cipher, $key, $encrypted, $this->mcrypt_mode, $iv), "\0");;
    return $decrypted;
}

感谢

有帮助吗?

解决方案

如果你想使用Rijndael256在C#用你必须设置BlockSize256个。

RijndaelManaged rj = new RijndaelManaged();
rj.BlockSize = 256;

然后你四必须256位长。
看看 SymmetricAlgorithm.BlockSize酒店


或其他的方式圆:目前C#应用程序使用Rijndael128,因此必须php script.

<?php
class Foo {
  protected $mcrypt_cipher = MCRYPT_RIJNDAEL_128;
  protected $mcrypt_mode = MCRYPT_MODE_CBC;

  public function decrypt($key, $iv, $encrypted)
  {
    $iv_utf = mb_convert_encoding($iv, 'UTF-8');
    return mcrypt_decrypt($this->mcrypt_cipher, $key, base64_decode($encrypted), $this->mcrypt_mode, $iv_utf);
  }
}



$encrypted = "UmzUCnAzThH0nMkIuMisqg==";
$key = "qwertyuiopasdfghjklzxcvbnmqwerty";
$iv = "1234567890123456";

$foo = new Foo;
echo $foo->decrypt($key, $iv, $encrypted);

印刷品 hello world

其他提示

使用PHP加密;

/Generate public key for encrytion
$path = "keys/";

    $crt = openssl_x509_read(file_get_contents($path."cert.crt"));
    $publickey = openssl_get_publickey($crt);

    //Encrypt using public key
    openssl_public_encrypt($source, $crypted, $publickey);

    //openssl_private_encrypt($source, $crypted, $privkey);
    echo base64_encode($crypted);

解密使用C#

    X509Certificate2 x509cert = new X509Certificate2(pKeyFilename);
    RSACryptoServiceProvider.UseMachineKeyStore = false;
    RSACryptoServiceProvider crypt = (RSACryptoServiceProvider)x509cert.PrivateKey;                

    byte[] decrypted = crypt.Decrypt(Convert.FromBase64String(data), false);
    return ASCIIEncoding.UTF8.GetString(decrypted);

其中pKeyFilename与证书文件cert.crt创建一个个人信息交换文件。下面的例子使用一个AES-256加密。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top