0

I have been given a Public Key (key.pub file) and with the use of the same, I want to encrypt XML file using my C# application.

The file is having structure as below:

-----BEGIN PUBLIC KEY-----

xxxxxxxxxxxxxxxxxxxxyyyyyyyyyyyyyyyyyyyyyyxxxxxxxxxxxxxxxxxxxxxx aaaaaaaaaaaaaaabbbbbbbbbbbbbbzzzzzzzzzzzzzzzzbbbbbbbbbbbbbbbbbbb yyyyyyyyyyyyyyyyyyyyyyyyzzzzzzzzzzzzzzuuuuuuuuuuuuuuuuaaaaaaauuu pppppppppppppppaao==

-----END PUBLIC KEY-----

Community
  • 1
  • 1
Bhaumik Vyas
  • 55
  • 10
  • https://stackoverflow.com/questions/22385229/best-practices-for-symmetric-encryption-in-net – VhsPiceros Oct 22 '19 at 10:52
  • In my opinion this question is too broad, but you might find many examples here on SO by searching with the [C# and RSA tags](https://stackoverflow.com/questions/tagged/c%23%2brsa?tab=Newest). Good luck. – President James K. Polk Oct 22 '19 at 14:46

1 Answers1

2
public  string Encryption(string strText)
{ 
  var publicKey = "XXXXXXXXXXXXX The Key Value XXXXXXXXXXXXX";
  var testData = Encoding.UTF8.GetBytes(strText);
  RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(1024);

  // client encrypting data with a public key issued by server                    
    rsa.FromXmlString(publicKey.ToString());

   var encryptedData = rsa.Encrypt(testData, true);

   var base64Encrypted = Convert.ToBase64String(encryptedData);

    string retval = base64Encrypted.ToString();

    if (HaxVal1.Equals(HaxVal))
    {
        return retval;
    }
    else
    {
        return "InvalidSignature";
    }
}
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Bhaumik Vyas
  • 55
  • 10