Přepis z C# do PHP Vyřešeno

Místo pro dotazy a rady ohledně programovacích jazyků (C++, C#, PHP, ASP, Javascript, VBS..) a tvorby webových stránek

Moderátor: Mods_senior

Uživatelský avatar
lamin_cz
Level 2.5
Level 2.5
Příspěvky: 314
Registrován: říjen 12
Bydliště: Kostelec u Holešova
Pohlaví: Muž
Stav:
Offline
Kontakt:

Přepis z C# do PHP

Příspěvekod lamin_cz » 27 zář 2017 14:21

Zdravím moudré hlavy. Už od rána si lámu hlavu nad přepsáním třídy z C# do PHP. Jedná se o "podpis" XML pro komunikaci. Bohužel máme příklad jen v C# ...

Kód: Vybrat vše

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

public class RijndaelSimple
{
    private static string saltValue = "sul";     // can be any string
    private static string hashAlgorithm = "SHA1";            // can be "MD5"
    private static int passwordIterations = 2;               // can be any number
    private static string initVector = "vektor";   // must be 16 bytes
    private static int keySize = 256;                        // can be 192 or 128


    public static string Encrypt(string plainText, string passPhrase)
    {
        byte[] initVectorBytes = Encoding.UTF8.GetBytes(initVector);
        byte[] saltValueBytes = Encoding.UTF8.GetBytes(saltValue);

        byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);
        

        PasswordDeriveBytes password 
= new PasswordDeriveBytes(passPhrase, saltValueBytes, hashAlgorithm, passwordIterations);
        

        
// Use the password to generate pseudo-random bytes for the encryption
        // key. Specify the size of the key in bytes (instead of bits).
        byte[] keyBytes = password.GetBytes(keySize / 8);

        RijndaelManaged symmetricKey = new RijndaelManaged();

        // It is reasonable to set encryption mode to Cipher Block Chaining
        // (CBC). Use default options for other symmetric key parameters.
        symmetricKey.Mode = CipherMode.CBC;

        ICryptoTransform encryptor = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes);
        MemoryStream memoryStream = new MemoryStream();
        CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write);
        
        cryptoStream
.Write(plainTextBytes, 0, plainTextBytes.Length);
        cryptoStream.FlushFinalBlock();

        byte[] cipherTextBytes = memoryStream.ToArray();

        foreach(var item in plainTextBytes)
        {
            Console.WriteLine(item.ToString());
        }
        memoryStream.Close();
        cryptoStream.Close();

        string cipherText = Convert.ToBase64String(cipherTextBytes);
        return cipherText;
    }

    static byte[] GetBytes(string str)
    {
        byte[] bytes = new byte[str.Length * sizeof(char)];
        System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
        return bytes;
    }

}
 


Jde mi zatím o Encrypt ... ztroskotal jsem u přepisu fce:

PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, saltValueBytes, hashAlgorithm, passwordIterations);

(já vím, není toho mnoho)

Nesetkal se někdo s řešením?

Děkuji
  • CPU: Cx6x86L-PR150+
  • MB: XT PRO2
  • GPU: integrovaná
  • RAM: 28Mb SDRAM
PHP, MySQL, HTML, CSS, jQuery ...

Reklama
Uživatelský avatar
CZechBoY
Master Level 9.5
Master Level 9.5
Příspěvky: 8813
Registrován: srpen 08
Bydliště: Brno
Pohlaví: Muž
Stav:
Offline
Kontakt:

Re: Přepis z C# do PHP  Vyřešeno

Příspěvekod CZechBoY » 15 říj 2017 21:33

PHP, Nette, MySQL, C#, TypeScript, Python
IntelliJ Idea, Docker, Opera browser, Linux Mint
iPhone XS
Raspberry PI 3 (KODI, Raspbian)
XBox One S, PS 4, nVidia GeForce NOW

Uživatelský avatar
lamin_cz
Level 2.5
Level 2.5
Příspěvky: 314
Registrován: říjen 12
Bydliště: Kostelec u Holešova
Pohlaví: Muž
Stav:
Offline
Kontakt:

Re: Přepis z C# do PHP

Příspěvekod lamin_cz » 21 říj 2017 10:58

Díky :-)
  • CPU: Cx6x86L-PR150+
  • MB: XT PRO2
  • GPU: integrovaná
  • RAM: 28Mb SDRAM
PHP, MySQL, HTML, CSS, jQuery ...


Zpět na “Programování a tvorba webu”

Kdo je online

Uživatelé prohlížející si toto fórum: sherry a 1 host