Ejemplo n.º 1
0
        private void GenerateStoreKey(bool withDate)
        {
            if (_keyStore.ContainsAlias(KeyAlias))
            {
                return;
            }

            ClearSettings();

            var end = Calendar.Instance;

            end.Add(CalendarField.Year, 99);

            if (_oldAndroid)
            {
                var subject = new X500Principal($"CN={KeyAlias}");

                var builder = new KeyPairGeneratorSpec.Builder(Application.Context)
                              .SetAlias(KeyAlias)
                              .SetSubject(subject)
                              .SetSerialNumber(BigInteger.Ten);

                if (withDate)
                {
                    builder.SetStartDate(new Date(0)).SetEndDate(end.Time);
                }

                var spec = builder.Build();
                var gen  = KeyPairGenerator.GetInstance(KeyProperties.KeyAlgorithmRsa, AndroidKeyStore);
                gen.Initialize(spec);
                gen.GenerateKeyPair();
            }
            else
            {
                var builder = new KeyGenParameterSpec.Builder(KeyAlias, KeyStorePurpose.Decrypt | KeyStorePurpose.Encrypt)
                              .SetBlockModes(KeyProperties.BlockModeGcm)
                              .SetEncryptionPaddings(KeyProperties.EncryptionPaddingNone);

                if (withDate)
                {
                    builder.SetKeyValidityStart(new Date(0)).SetKeyValidityEnd(end.Time);
                }

                var spec = builder.Build();
                var gen  = KeyGenerator.GetInstance(KeyProperties.KeyAlgorithmAes, AndroidKeyStore);
                gen.Init(spec);
                gen.GenerateKey();
            }
        }
Ejemplo n.º 2
0
            private void CreateKey_Credentials()
            {
                var generator = KeyPairGenerator.GetInstance("RSA", AndroidKeyStore);

                if (Build.VERSION.SdkInt < BuildVersionCodes.M)
                {
                    Java.Util.Calendar calendar = Java.Util.Calendar.Instance;
                    calendar.Add(Java.Util.CalendarField.Year, 20);

                    Date startDate = Java.Util.Calendar.Instance.Time;
                    Date endDate   = calendar.Time;

#pragma warning disable 0618

                    var builder = new KeyPairGeneratorSpec.Builder(_context);

#pragma warning restore 0618

                    builder.SetAlias(KEYALIAS_CREDENTIALS);
                    builder.SetSerialNumber(Java.Math.BigInteger.One);
                    builder.SetSubject(new Javax.Security.Auth.X500.X500Principal("CN=${alias} CA Certificate"));
                    builder.SetStartDate(startDate);
                    builder.SetEndDate(endDate);

                    generator.Initialize(builder.Build());
                }
                else
                {
                    var builder = new KeyGenParameterSpec.Builder(KEYALIAS_CREDENTIALS, KeyStorePurpose.Encrypt | KeyStorePurpose.Decrypt);
                    builder.SetBlockModes(KeyProperties.BlockModeEcb);
                    builder.SetEncryptionPaddings(KeyProperties.EncryptionPaddingRsaPkcs1);
                    generator.Initialize(builder.Build());
                }

                generator.GenerateKeyPair();
            }