Esempio n. 1
0
        public void EncryptTest()
        {
            var str          = "HelloWorld!";
            var rsaEncrypter = new RsaEncryptor(PublicKey, PrivateKey);
            var encrypted    = rsaEncrypter.Encrypt(str);

            Assert.Equal(str, rsaEncrypter.Decrypt(encrypted));

            var rsa2Encrypter = new RsaEncryptor(PublicKey, PrivateKey);

            rsa2Encrypter.SetHashAlg("RSA2");
            var encrypted2 = rsa2Encrypter.Encrypt(str);

            Assert.Equal(str, rsa2Encrypter.Decrypt(encrypted2));


            var rsaEncrypter2 = new RsaEncryptor();

            rsaEncrypter2.SetHashAlg("RSA");
            rsaEncrypter2.LoadPublicKey(PublicKey);
            rsaEncrypter2.LoadPrivateKey(PrivateKey);
            var encrypted2_2 = rsaEncrypter2.Encrypt(str);

            Assert.Equal(str, rsaEncrypter2.Decrypt(encrypted2_2));
        }
Esempio n. 2
0
        public void GenerateKeyPair_Test()
        {
            var keyPair = RsaUtil.GenerateKeyPair(RSAKeyFormat.PKCS1, 512);

            Assert.NotEmpty(keyPair.PublicKey);
            var rsaEncryptor = new RsaEncryptor(keyPair.PublicKey, keyPair.PrivateKey);
            var d1           = rsaEncryptor.Encrypt("hello");
            var d2           = rsaEncryptor.Decrypt(d1);

            Assert.Equal("hello", d2);

            var sign = rsaEncryptor.SignData("string1");

            Assert.True(rsaEncryptor.VerifyData("string1", sign));


            var rsaParams1 = RsaUtil.ReadPrivateKeyInfo(keyPair.PrivateKey);

            var rsaParams2     = RsaUtil.ReadPrivateKeyInfo(keyPair.PrivateKey);
            var rsaPrivateKey2 = RsaUtil.ExportPrivateKeyPkcs8(rsaParams2);
            var rsaEncryptor2  = new RsaEncryptor();

            rsaEncryptor2.LoadPrivateKey(rsaPrivateKey2);

            var d3 = rsaEncryptor2.Decrypt(d1);

            Assert.Equal("hello", d3);
        }
Esempio n. 3
0
 private bool setupEncryption(PendingOperation pendingOperation)
 {
     if (generatingKeyPair || requestingEncryptionKey)
     {
         pendingOperations.Enqueue(pendingOperation);
         return(false);
     }
     if (!haveKeyPair())
     {
         pendingOperations.Enqueue(pendingOperation);
         generatingKeyPair = true;
         clubPenguinClient.GenerateKeyPair(delegate(RSAParameters rsaParams)
         {
             generatingKeyPair = false;
             clubPenguinClient.CPKeyValueDatabase.SetRsaParameters(rsaParams);
             sendPendingOperations();
         }, delegate
         {
             generatingKeyPair = false;
             failPendingOperations();
         });
         return(false);
     }
     if (!haveEncryptionKey())
     {
         pendingOperations.Enqueue(pendingOperation);
         requestingEncryptionKey = true;
         RSAParameters rsaParameters     = clubPenguinClient.CPKeyValueDatabase.GetRsaParameters().Value;
         string        publicKeyExponent = Convert.ToBase64String(rsaParameters.Exponent);
         string        publicKeyModulus  = Convert.ToBase64String(rsaParameters.Modulus);
         APICall <GetEncryptionKeyOperation> encryptionKey = clubPenguinClient.EncryptionApi.GetEncryptionKey(publicKeyExponent, publicKeyModulus);
         encryptionKey.OnResponse += delegate(GetEncryptionKeyOperation op, HttpResponse resp)
         {
             requestingEncryptionKey = false;
             try
             {
                 string keyId                  = op.ResponseBody.keyId;
                 byte[] ciphertext             = Convert.FromBase64String(op.ResponseBody.encryptedSymmetricKey);
                 byte[] symmetricEncryptionKey = RsaEncryptor.Decrypt(ciphertext, rsaParameters);
                 updateEncryptionKey(keyId, symmetricEncryptionKey);
                 sendPendingOperations();
             }
             catch (Exception ex)
             {
                 Log.LogErrorFormatted(this, "Failed to decrypt symmetric key from server. Operations requiring encryption will fail. Exception: {0}", ex);
                 failPendingOperations();
             }
         };
         encryptionKey.OnError += delegate
         {
             requestingEncryptionKey = false;
             failPendingOperations();
         };
         encryptionKey.Execute();
         return(false);
     }
     return(true);
 }
Esempio n. 4
0
        private void encryptionKeyReceived(string encodedEncryptedEncryptionKey)
        {
            RSAParameters value = mt.ClubPenguinClient.CPKeyValueDatabase.GetRsaParameters().Value;

            byte[] ciphertext = Convert.FromBase64String(encodedEncryptedEncryptionKey);
            byte[] key        = RsaEncryptor.Decrypt(ciphertext, value);
            mt.SmartFoxEncryptor = new SmartFoxEncryptor(key);
            roomJoinCompleted();
        }
Esempio n. 5
0
        private void DecryptMessage(object obj)
        {
            var keyValues = new Dictionary <string, string>();

            keyValues.Add(nameof(Modulus), Modulus);
            keyValues.Add(nameof(Exponent), Exponent);
            keyValues.Add(nameof(D), D);
            keyValues.Add(nameof(Dp), Dp);
            keyValues.Add(nameof(Dq), Dq);
            keyValues.Add(nameof(InverseQ), InverseQ);
            keyValues.Add(nameof(P), P);
            keyValues.Add(nameof(Q), Q);

            Decrypted = Encoding.UTF8.GetString(rsaEncryptor.Decrypt(Convert.FromBase64String(Cipherbytes), keyValues));
        }
Esempio n. 6
0
        public void TrimKey_Test()
        {
            var keyPair          = RsaUtil.GenerateFormatKeyPair();
            var trimedPublicKey  = RsaUtil.TrimKey(keyPair.PublicKey);
            var trimedPrivateKey = RsaUtil.TrimKey(keyPair.PrivateKey);

            Assert.Contains("-", keyPair.PublicKey);
            Assert.Contains("-", keyPair.PrivateKey);
            Assert.DoesNotContain("-", trimedPublicKey);
            Assert.DoesNotContain("-", trimedPrivateKey);
            var rsaEncryptor = new RsaEncryptor(trimedPublicKey, trimedPrivateKey);
            var encrypted1   = rsaEncryptor.Encrypt("china");
            var decrypted1   = rsaEncryptor.Decrypt(encrypted1);

            Assert.Equal("china", decrypted1);
        }
Esempio n. 7
0
        public void Pkcs8_Pkcs1_Conver_Test()
        {
            var keyPair       = RsaUtil.GenerateKeyPair();
            var rsaEncryptor1 = new RsaEncryptor(keyPair.PublicKey, keyPair.PrivateKey);

            var pkcs8Key      = RsaUtil.Pkcs1ToPkcs8(keyPair.PrivateKey);
            var rsaEncryptor2 = new RsaEncryptor(keyPair.PublicKey, pkcs8Key);

            var d1 = rsaEncryptor1.Encrypt("123456");
            var d2 = rsaEncryptor2.Encrypt("123456");
            //Assert.Equal(d1, d2);

            var d3 = rsaEncryptor1.Decrypt(d1);

            Assert.Equal("123456", d3);
            var d4 = rsaEncryptor2.Decrypt(d2);

            var pkcs1Key = RsaUtil.Pkcs8ToPkcs1(pkcs8Key);

            Assert.Equal(keyPair.PrivateKey, pkcs1Key);
        }
        public void Decrypt_should_throw_NotImplementedException()
        {
            var sut = new RsaEncryptor(null);

            Assert.Throws <NotImplementedException>(() => sut.Decrypt(null, null));
        }
 public byte[] Decrypt(byte[] encryptedData)
 {
     return(_rsaEncryptor.Decrypt(encryptedData));
 }