RsaUtilityPoc.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132
  1. using System.Diagnostics;
  2. using System.Security.Cryptography;
  3. using System.Text;
  4. using PCC.App.Security;
  5. namespace PCC.Pocs.Pocs
  6. {
  7. internal static class RsaUtilityPoc
  8. {
  9. public static void RunPoc()
  10. {
  11. var (pub1, pri1) = RsaUtility.GeneratePKCS1(4096); // sender
  12. var (pub2, pri2) = RsaUtility.GeneratePKCS1(4096); // rec
  13. var rsaPub1 = RsaUtility.FromPKCS1PublicKey(pub1);
  14. var rsaPri1 = RsaUtility.FromPKCS1PrivateKey(pri1);
  15. var rsaPub2 = RsaUtility.FromPKCS1PublicKey(pub2);
  16. var rsaPri2 = RsaUtility.FromPKCS1PrivateKey(pri2);
  17. var rawText = RandomNumberGenerator.GetString(new[] { 'C', 'D' }, RandomNumberGenerator.GetInt32(10)) + "Brrr汉字Brrr:" + RandomNumberGenerator.GetString(new[] { 'A', 'B' }, 1024);
  18. var rawBytes = Encoding.UTF8.GetBytes(rawText);
  19. var encBytes = RsaUtility.EncryptAndSignature(rsaPub2, rawBytes, rsaPri1);
  20. var decBytes = RsaUtility.DecryptAndVerifySignature(rsaPri2, encBytes, rsaPub1);
  21. var decText = Encoding.UTF8.GetString(decBytes);
  22. var success = rawText == decText;
  23. }
  24. }
  25. }