Hack Forums
[C#] Help me convert this PHP code to C# - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [C#] Help me convert this PHP code to C# (/showthread.php?tid=6058490)



Help me convert this PHP code to C# - draxyee - Mar 20, 2020

PHP Code

<?php 
       
        $key 
= openssl_random_pseudo_bytes(32);
 
       $iv = openssl_random_pseudo_bytes(12);
 
       $time = time();

 
       openssl_public_encrypt($key ,$encryptedAesKey, base64_decode($publicKey));
 
       $encrypted = openssl_encrypt($password, 'aes-256-gcm', $key, OPENSSL_RAW_DATA, $iv, $tag, strval($time)); 

This is what I currently have:

Code
           
           byte[] key = new byte[32];
           byte[] iv = new byte[12];
           RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
           rng.GetBytes(key);
           rng.GetBytes(iv);
           string rngString = Encoding.UTF8.GetString(key);

           string keyString = "LS0tLS1CRUdJTiBQVUJMSUMgS0VZLS0tLS0KTUlJQklqQU5CZ2txaGtpRzl3MEJBUUVGQUFPQ0FROEFNSUlCQ2dLQ0FRRUF4ZmFic2RVSDJLenRJaGs5MG42eQpUdk9teGZZRWU5MXA3OWRKYkxwa2g0eXJnNmtCQVppeUVoUHNlNlFpcHVxM2h0c0ppZnpGMzdlVUF0ODE2M3QxCnNLZXlBQzlxU0NKcndaUGZRb3ZyUHdIYjU1bVc2SGdZbUxsU0ovK2xKbzdsLzNzekRaYnd1dHVBYWxsZU9oZUcKQnlrYVgrRFhQbDRwbWVYLzZDUHRYS1Bkdi9xUGRYRytOZS9JaHJCNEtKdzl4NElMbGtnR3ZjaS8wblFKRjl1awpOYytVK3F2eE9TaWV4MjljT2pIUnNWOGpadnNxOGh5Q1EyK2hwNmdkSEdxUGoveW1zRG14RHd4MUF1ZUIrVlZiCnlzQ0RjYzdHdTNXdTRtK2dpZXN6NTRXTXlCQUFnVm9DSWRQMFU1Tis0S1duMWNhc1RoL3NZWVFFSFRaUis3UzcKcFFJREFRQUIKLS0tLS1FTkQgUFVCTElDIEtFWS0tLS0tCg==";
           byte[] keyBytes = Convert.FromBase64String(keyString);
           string keyStringDecoded = Encoding.UTF8.GetString(keyBytes);

           string timeString = ((int)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds).ToString();
           byte[] timeBytes = Encoding.UTF8.GetBytes(timeString);

           OpenSSLProtect osp = new OpenSSLProtect();
           string encryptedAesKey = osp.OpenSSLEncrypt(rngString, keyStringDecoded);

           EncryptionService iq = new EncryptionService(32, 16, 12);
           string encrypted = iq.EncryptWithKey(password, rngString, timeBytes);

I'm getting this error: "System.FormatException: 'The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters. '"

This is what I'm using for AES-256-GCM (EncryptionService / iq.EncryptWithKey): https://github.com/lukemerrett/Bouncy-Castle-AES-GCM-Encryption

If anyone can help me, I would be so appreciative.

Cheers.