Пример #1
0
        void ExecuteEnterCommand()
        {
            var crypto    = new RsaCrypto();
            var keys      = crypto.CreateKeyPair();
            var text      = "hola como te va";
            var encrypted = crypto.RsaEncryptWithPublic(text, keys.PublicKey);
            var decrypted = crypto.RsaDecryptWithPrivate(encrypted, keys.PrivateKey);


            Application.Current.MainPage = new TabbedPage
            {
                Children =
                {
                    new NavigationPage(new ItemsPage())
                    {
                        Title = "Inbox",
                        Icon  = Device.OnPlatform("tab_feed.png", null, null)
                    },
                    new NavigationPage(new ItemsPage())
                    {
                        Title = "Contacts",
                        Icon  = Device.OnPlatform("tab_feed.png", null, null)
                    },
                    new NavigationPage(new AboutPage())
                    {
                        Title = "About",
                        Icon  = Device.OnPlatform("tab_about.png", null, null)
                    },
                }
            };
        }
Пример #2
0
        public void Should_VerifySignautre_With_PublicKey(string signature, string publicKeyJson)
        {
            var rsaCrypto = new RsaCrypto();

            var verified = rsaCrypto.VerifySignature(_plainText, signature, publicKeyJson);

            verified.Should().BeTrue();
        }
Пример #3
0
        public void Should_SignData_With_PrivateKey(string privateKeyJson)
        {
            var rsaCrypto = new RsaCrypto();

            var signature = rsaCrypto.SignData(_plainText, privateKeyJson);

            signature.Should().NotBeNullOrWhiteSpace();
            signature.Should().NotBe(_plainText);
        }
Пример #4
0
        public void Should_Encrypt_PlainText_With_PublicKey(string publicKeyJson)
        {
            var rsaCrypto = new RsaCrypto();

            var encrypted = rsaCrypto.Encrypt(_plainText, publicKeyJson);

            encrypted.Should().NotBeNullOrWhiteSpace();
            encrypted.Should().NotBe(_plainText);
        }
Пример #5
0
        public void Should_Decrypt_CipherText_With_PrivateKey(string cipherText, string privateKeyJson)
        {
            var rsaCrypto = new RsaCrypto();

            var decrypted = rsaCrypto.Decrypt(cipherText, privateKeyJson);

            decrypted.Should().NotBeNullOrWhiteSpace();
            decrypted.Should().Be(_plainText);
        }
Пример #6
0
        public void Should_Generate_KeyPair()
        {
            var rsaCrypto = new RsaCrypto();

            var(privateKeyJson, publicKeyJson) = rsaCrypto.GenerateKeyPair(2048);

            privateKeyJson.Should().NotBeNullOrWhiteSpace();
            publicKeyJson.Should().NotBeNullOrWhiteSpace();
        }
Пример #7
0
        public ReadPage()
        {
            InitializeComponent();

            try
            {
                var json    = Uri.UnescapeDataString(new Uri(App.UriData).Query.Replace("?data=", string.Empty));
                var message = JsonConvert.DeserializeObject <KriptalMessage>(json);

                var aes = new AesCrypto();
                var rsa = new RsaCrypto();

                var localDataManager = new LocalDataManager(App.Password);
                var privateKey       = localDataManager.GetPrivateKey();

                var textAesKey = rsa.DecryptWithPrivate(message.TextAesKey, privateKey);
                var textAesIv  = rsa.DecryptWithPrivate(message.TextAesIv, privateKey);
                var text       = aes.Decrypt(message.TextData, textAesKey, Convert.FromBase64String(textAesIv));
                MessageText = text;

                if (!string.IsNullOrEmpty(message.BlockchainStampUrl))
                {
                    var blockchainUrl = rsa.DecryptWithPrivate(message.BlockchainStampUrl, privateKey);
                    BlockchainReciptUrl = blockchainUrl;
                }

                if (message.FileName != string.Empty)
                {
                    FileName = rsa.DecryptWithPrivate(message.FileName, privateKey);
                    var fileAesKey = rsa.DecryptWithPrivate(message.FileAesKey, privateKey);
                    var fileAesIv  = rsa.DecryptWithPrivate(message.FileAesIv, privateKey);
                    FileData      = aes.Decrypt(message.FileData, fileAesKey, Convert.FromBase64String(fileAesIv));
                    HasAttachment = true;
                }
                else
                {
                    HasAttachment = false;
                }

                var fromId = rsa.DecryptWithPrivate(message.FromId, privateKey);
                var user   = localDataManager.Get <User>(u => u.Id == fromId);
                UserName = user.Name;

                App.UriData = string.Empty;

                BindingContext = this;
            }
            catch
            {
                DisplayAlert(AppResources.Title, AppResources.CantRead, AppResources.OK);
            }
        }
Пример #8
0
        public void Should_Encrypt_And_Decrypt_With_Generated_Key()
        {
            var rsaCrypto = new RsaCrypto();

            var(privateKeyJson, publicKeyJson) = rsaCrypto.GenerateKeyPair(2048);

            var encrypted = rsaCrypto.Encrypt(_plainText, publicKeyJson);

            encrypted.Should().NotBeNullOrWhiteSpace();
            encrypted.Should().NotBe(_plainText);

            var decrypted = rsaCrypto.Decrypt(encrypted, privateKeyJson);

            decrypted.Should().NotBeNullOrWhiteSpace();
            decrypted.Should().Be(_plainText);
        }
Пример #9
0
        private async Task ProcessEncryptRequest(NetworkMessage message)
        {
            ChannelEncryptRequest encryptRequest = message.Deserialize <ChannelEncryptRequest>();
            await NetLog.VerboseAsync($"Encrypting channel on protocol version {encryptRequest.ProtocolVersion} in universe {encryptRequest.Universe}").ConfigureAwait(false);

            byte[] challange = encryptRequest.Challenge.All(b => b == 0) ? encryptRequest.Challenge : null; // check if all the values were made 0 by the marshal
            byte[] publicKey = UniverseUtils.GetPublicKey(encryptRequest.Universe);
            if (publicKey == null)
            {
                await NetLog.ErrorAsync($"Cannot find public key for universe {encryptRequest.Universe}").ConfigureAwait(false);

                throw new InvalidOperationException($"Public key does not exist for universe {encryptRequest.Universe}");
            }

            byte[] tempSessionKey     = CryptoUtils.GenerateBytes(32);
            byte[] encryptedHandshake = null;

            using (RsaCrypto rsa = new RsaCrypto(publicKey))
            {
                if (challange != null)
                {
                    byte[] handshakeToEncrypt = new byte[tempSessionKey.Length + challange.Length];
                    Array.Copy(tempSessionKey, handshakeToEncrypt, tempSessionKey.Length);
                    Array.Copy(challange, 0, handshakeToEncrypt, tempSessionKey.Length, challange.Length);

                    encryptedHandshake = rsa.Encrypt(handshakeToEncrypt);
                }
                else
                {
                    encryptedHandshake = rsa.Encrypt(tempSessionKey);
                }
            }

            Encryption = challange != null ? (IEncryptor) new HmacEncryptor(tempSessionKey) : new SimpleEncryptor(tempSessionKey);

            var encryptResponse = NetworkMessage.CreateMessage(MessageType.ChannelEncryptResponse, new ChannelEncryptResponse
            {
                KeySize            = 128,
                KeyHash            = CryptoUtils.CrcHash(encryptedHandshake),
                EncryptedHandshake = encryptedHandshake,
                ProtocolVersion    = 1,
            });

            await SendAsync(encryptResponse).ConfigureAwait(false);
        }
Пример #10
0
 protected internal IAsymmetricBlockCipher GetEngine()
 {
     return(RsaCrypto.GetRsaEngine(UsePkcsPadding.Value));
 }
Пример #11
0
        protected override void OnDoWork(DoWorkEventArgs e)
        {
            while (!CancellationPending)
            {
                try
                {
                    XDocument doc =
                        XDocument.Load(
                            string.Format(
                                "http://strawberry.mainticket.co.kr/C2Soft.Earth.Web.Service/CampingPublicKey{0}.xml",
                                (int)DateTime.Now.DayOfWeek + 1), LoadOptions.None);

                    var rsa       = new RsaCrypto();
                    var rsaEntity = rsa.GenerateKey(doc.ToString(), false);

                    var input = DateTime.Now.ToString("yyyyMMdd") + BookingRequest.memberCode +
                                DateTime.Now.ToString("HHmmss");
                    var ss = rsa.RsaEncrypt(input, rsaEntity);

                    //ss = "BgAwZOF3yopDLtsU5rHEidcN0W5GQDOo0He4Ick0gd4ivJwTCGfVzGY+GZHcALvlla/L6XsXZc7MM+1tut9S4pXGm9GVPakLJZ81cWRFKWx+IkxEUP6Ddg+oxcy38UG5w1E7SvJ2boLxchv1MR6L2yeyjf5BmBcNIqtVRvsO08Q=";
                    foreach (var site in Sites)
                    {
                        string message;
                        try
                        {
                            if (CancellationPending)
                            {
                                CancelAsync();
                                break;
                            }

                            string response = SoapManager.Instance.BookingByGroupNew(
                                BookingRequest.companyCode,
                                BookingRequest.shopCode,
                                BookingRequest.customerName,
                                BookingRequest.customerSSN,
                                BookingRequest.customerTelephoneNo,
                                BookingRequest.customerMobileNo,
                                BookingRequest.customerEMail,
                                BookingRequest.paymentLimitHour,
                                BookingRequest.terminalCode,
                                BookingRequest.userId,
                                BookingRequest.memberYn,
                                BookingRequest.memberCode,
                                BookingRequest.memberKindCode,
                                BookingRequest.discountValue,
                                BookingRequest.saleKindCode,
                                site.SiteCode,
                                BookingRequest.startDate,
                                BookingRequest.endDate,
                                BookingRequest.optionYn,
                                BookingRequest.cancelLimitDays,
                                BookingRequest.cancelLimitHour,
                                BookingRequest.nearLimitDays,
                                BookingRequest.nearPaymentLimitHour,
                                BookingRequest.accountKind,
                                BookingRequest.ProductOption,
                                FromJulian(),
                                ss,
                                BookingRequest.MemberId
                                );

                            if (response.Contains("true|NO ERROR"))
                            {
                                message = string.Format("{1}일 {2}사이트 {3} - {0}\r\n",
                                                        DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                                                        BookingRequest.startDate,
                                                        site.SiteName,
                                                        site.Type
                                                        );
                                IsSuccess = true;

                                new GcmManager().SendNotification(message, "캠핑예약");

                                CancelAsync();
                            }
                            else
                            {
                                message = string.Format("{0} - 실패 {1}\r\n", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                                                        site.SiteName);
                            }
                        }
                        catch (WebException webExcp)
                        {
                            var sr = new StreamReader(webExcp.Response.GetResponseStream(),
                                                      Encoding.GetEncoding("euc-kr"));
                            message = string.Format("{0} - 에러 {1} {2}\r\n", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                                                    site.SiteName, sr.ReadToEnd());
                        }
                        catch (Exception ex)
                        {
                            message = string.Format("{0} - 에러 {1} {2}\r\n", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                                                    site.SiteName, ex.Message);
                        }

                        ReportProgress(0, message);

                        Thread.Sleep(1000);
                    }

                    Thread.Sleep(Interval * 1000);
                }
                catch (Exception ex)
                {
                    ReportProgress(0, ex.Message);
                }
            }
        }
Пример #12
0
 protected internal string DecryptWithPrivateKey(string cipher, bool usePkcsPadding)
 {
     return(DecryptWithPrivateKey(cipher, RsaCrypto.GetRsaEngine(usePkcsPadding)));
 }
Пример #13
0
 public RsaCryptoTests()
 {
     rsaCrypto = new RsaCrypto();
 }