44 lines
10 KiB
C#
44 lines
10 KiB
C#
|
using System;
|
||
|
using System.Linq;
|
||
|
using System.Security.Cryptography;
|
||
|
using System.Security.Cryptography.X509Certificates;
|
||
|
|
||
|
using NUnit.Framework;
|
||
|
|
||
|
namespace MonoTests.System.Security.Cryptography
|
||
|
{
|
||
|
[TestFixture]
|
||
|
public class X509Certificate2ExtensionsTests
|
||
|
{
|
||
|
static byte[] selfSignedCertData = { 48,130,10,73,2,1,3,48,130,10,5,6,9,42,134,72,134,247,13,1,7,1,160,130,9,246,4,130,9,242,48,130,9,238,48,130,6,7,6,9,42,134,72,134,247,13,1,7,1,160,130,5,248,4,130,5,244,48,130,5,240,48,130,5,236,6,11,42,134,72,134,247,13,1,12,10,1,2,160,130,4,246,48,130,4,242,48,28,6,10,42,134,72,134,247,13,1,12,1,3,48,14,4,8,183,24,178,64,55,187,89,192,2,2,7,208,4,130,4,208,85,96,57,23,163,136,131,116,70,109,5,14,149,108,104,0,10,33,156,192,186,217,74,35,181,5,143,106,201,127,16,78,231,237,3,15,217,6,92,63,162,172,254,45,66,23,162,53,227,223,129,181,51,204,237,16,236,138,241,198,11,33,88,119,177,201,149,114,170,37,23,194,221,85,102,157,172,211,78,4,217,88,120,213,140,142,202,233,8,48,48,46,173,29,23,194,39,143,114,66,29,17,210,0,138,20,236,35,125,110,38,190,22,249,147,196,245,117,10,141,122,98,35,93,35,28,128,39,52,45,38,116,176,160,91,247,142,54,5,178,106,154,35,5,69,84,99,13,190,58,208,194,128,176,20,14,43,113,165,110,88,189,240,164,7,77,95,8,20,165,59,159,48,31,159,237,177,194,12,69,7,222,149,191,59,16,184,227,76,16,225,147,236,129,240,144,252,163,222,226,133,243,8,97,201,79,149,119,181,22,71,233,55,224,2,116,63,126,50,58,224,83,46,216,64,34,2,64,215,20,198,253,83,66,59,224,169,231,95,117,56,134,152,147,147,62,244,39,96,245,131,122,199,167,222,59,218,225,176,78,115,235,127,44,162,188,207,57,152,226,134,218,36,34,66,30,18,217,50,118,129,13,94,85,202,123,181,66,60,124,89,115,249,133,42,82,200,61,48,248,194,140,195,113,72,15,211,1,230,31,24,106,126,157,193,114,244,28,177,100,199,58,194,15,225,206,216,121,108,95,166,106,232,216,157,238,206,211,66,200,117,180,113,118,180,63,133,172,151,84,143,97,28,105,46,226,75,194,180,193,194,78,201,211,9,79,212,226,212,200,102,76,128,215,209,146,73,47,175,250,139,20,98,64,220,164,180,117,26,141,45,169,98,200,223,78,11,38,8,105,192,110,190,183,40,65,184,194,24,12,9,233,36,115,107,71,175,110,77,126,230,19,101,136,180,145,58,126,231,154,91,201,83,155,194,215,12,188,97,35,134,169,91,220,9,80,156,157,62,230,246,214,216,242,49,227,14,24,67,68,179,80,125,52,153,236,40,196,10,217,181,188,247,118,150,196,181,111,215,128,87,91,7,202,22,37,172,103,217,255,37,117,208,9,246,174,204,105,176,1,9,42,191,204,183,101,5,90,155,230,188,227,226,250,65,186,119,5,5,23,153,242,0,99,234,51,61,31,14,58,151,175,86,44,247,40,231,226,92,43,59,69,66,207,167,37,106,32,181,150,54,107,168,55,121,164,41,195,74,43,220,173,218,180,7,38,250,237,200,135,51,249,231,201,208,179,12,151,127,23,228,218,89,19,132,154,173,71,72,34,174,19,139,231,2,207,202,32,171,152,27,3,207,218,72,200,102,135,86,178,80,6,187,56,156,57,213,160,33,205,209,81,199,89,79,168,254,139,72,202,9,91,132,216,89,29,152,113,145,230,129,3,211,145,130,164,112,229,103,36,112,71,67,241,8,96,170,30,0,198,68,130,101,20,2,227,55,206,127,138,42,36,87,66,117,175,21,16,234,121,64,169,236,199,61,63,16,184,202,190,189,124,6,127,228,215,167,127,240,111,29,58,105,42,56,244,206,169,0,73,110,143,109,182,200,248,199,86,243,158,237,92,139,126,196,14,0,162,82,46,157,12,120,135,25,28,116,233,162,250,24,173,164,86,15,172,138,13,67,209,243,35,126,117,136,229,181,37,99,78,8,224,253,57,102,169,77,174,69,46,253,70,248,190,196,248,113,222,240,82,71,60,155,86,64,127,61,28,90,87,173,247,199,67,16,200,155,23,81,40,87,157,76,72,117,159,163,35,218,117,19,76,55,153,179,164,158,204,170,103,41,148,126,190,210,229,162,240,50,101,192,201,109,157,255,65,4,173,50,181,157,178,241,245,85,41,223,84,146,133,184,201,14,155,175,40,123,136,253,10,197,153,183,161,28,74,36,91,223,8,206,95,147,53,187,84,73,151,66,66,24,11,224,23,171,12,190,165,205,183,228,197,131,170,153,43,39,123,18,164,182,108,12,11,36,49,216,86,110,202,173,188,19,26,211,172,119,151,246,76,121,52,168,214,24,70,249,239,184,93,128,135,7,243,97,216,7,168,159,103,3,82,100,120,26,91,144,154,213,234,234,199,142,134,82,108,25,161,149,56,128,65,136,9,136,35,20,15,254,228,210,207,104,152,78,245,97,75,167,187,52,243,77,104,242,89,117,145,153,206,148,167,72,254,154,86,132,164,231,178,35,149,122,186,40,223,144,134,39,231,35,205,133,15,67,90,58,31,163,189,8,132,97,55,215,62,225,205,207,211,141,221,146,247,19,118,52,85,124,80,2
|
||
|
|
||
|
[Test]
|
||
|
public void PublicKeyIsReusable()
|
||
|
{
|
||
|
var cert = new X509Certificate2 (selfSignedCertData, "MonoTemp123");
|
||
|
var src = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
|
||
|
var padding = RSAEncryptionPadding.Pkcs1;
|
||
|
byte[] encrypted1 = EncryptData (cert, src, padding);
|
||
|
byte[] encrypted2 = EncryptData (cert, src, padding);
|
||
|
|
||
|
RSA privateKey = cert.GetRSAPrivateKey ();
|
||
|
byte[] decrypted1 = DecryptData (cert, encrypted1, padding);
|
||
|
byte[] decrypted2 = DecryptData (cert, encrypted2, padding);
|
||
|
|
||
|
CollectionAssert.AreEqual (src, decrypted1);
|
||
|
CollectionAssert.AreEqual (src, decrypted2);
|
||
|
}
|
||
|
|
||
|
static byte[] EncryptData(X509Certificate2 cert, byte[] data, RSAEncryptionPadding padding)
|
||
|
{
|
||
|
using (RSA rsa = cert.GetRSAPublicKey ())
|
||
|
return rsa.Encrypt (data, padding);
|
||
|
}
|
||
|
|
||
|
static byte[] DecryptData(X509Certificate2 cert, byte[] data, RSAEncryptionPadding padding)
|
||
|
{
|
||
|
using (RSA rsa = cert.GetRSAPrivateKey ())
|
||
|
return rsa.Decrypt (data, padding);
|
||
|
}
|
||
|
}
|
||
|
}
|