Since you use SshPrivateKey
for client authentication, you can use it to encrypt the USERID.
Encrypt like this:
// load SSH private key
var pk = new SshPrivateKey("privatekey.pri", "my-password");
string userId = "my-user-id";
using (var alg = new Rebex.Security.Cryptography.AsymmetricKeyAlgorithm())
{
// import public key
alg.ImportKey(pk.GetPublicKeyInfo());
// encrypt data
byte[] encrypted = alg.Encrypt(Encoding.UTF8.GetBytes(userId));
// save encrypted data
File.WriteAllBytes("c:/data/user.id", encrypted);
}
Decrypt like this:
// load SSH private key
var pk = new SshPrivateKey("privatekey.pri", "my-password");
// get private key info
var pki = new PrivateKeyInfo();
pki.Load(new MemoryStream(pk.GetPrivateKey()), null);
string userId;
using (var alg = new Rebex.Security.Cryptography.AsymmetricKeyAlgorithm())
{
// import private key
alg.ImportKey(pki);
// decypt data
byte[] decrypted = alg.Decrypt(File.ReadAllBytes("c:/data/user.id"));
// get string representation
userId = Encoding.UTF8.GetString(decrypted);
}