简体   繁体   中英

Encrypt text file using RSA public key

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-----

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";
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM