Public/Private Key Encryption/Decryption with super simple integration with PHP and C#
PPK-Sharp is a quick and easy way to quickly get your foot in the door with PHP and C# based RSA public/private key encryption.
You may be able to use PHP 4.2 or PHP 5, but do not submit an issue if you aren't using PHP 7.0 or higher
Check Nuget for BouncyCastle, you'll need to add BouncyCastle to your project for this to work in C#
- [PHP] OpenSSL 1.1.1 or higher
- [C#] BouncyCastle 1.8.10 or higher for
.Net Core 3.1
or equivilent BouncyCastle Package
- Drag and drop the files from
php/
into your php project directory, or to a location on your web server. - Ensure OpenSSL has been installed or enabled
- Write a PHP document that includes
PPK.class.php
- Drag and drop the files from
csharp/
into your Visual Studio project directory - Ensure BouncyCastle has been installed from Nuget in Visual Studio
- Write a class that uses PPK
Check the examples/
subfolder of both languages (eg. php/examples/
) for more examples, but the basic usage is this:
<?php
include("PPK.class.php");
$pem = new PPK();
$encrypted_data = $pem->private->encrypt("Hello World");
$decrypted_data = $pem->public->decrypt($encrypted_data);
echo $decrypted_data; // Output: Hello World
using System;
using PPKSharp;
namespace ConsoleTest
{
class Program
{
static void Main(string[] args)
{
// you can provide your key by fetching it from a web server, or by reading it from a file.
string PrivateKey = "... your public or private key here...";
string PublicKey = "... your public or private key here...";
PPK ppkPublic = new PPK(PublicKey); // key should be in PEM format
PPK ppkPrivate = new PPK(PrivateKey); // key should be in PEM format
string encrypted = ppkPublic.Encrypt("Hello World");
string decrypted = ppkPrivate.Decrypt(encrypted);
Console.WriteLine(decrypted);
}
}
}
- PPK-PHP supports generation of private keys, PPK-Sharp does not
- PPK-Sharp supports initialization using private OR public keys, PPK-PHP only supports initialization using private keys.
- PPK-Sharp cannot tell you what the public key of a private key is, but PPK-PHP can.