Ejemplo n.º 1
0
		protected override void OnCreate (Bundle savedInstanceState)
		{
			base.OnCreate (savedInstanceState);
			fingerprintModule = new FingerprintModule(this);
			mKeyguardManager = fingerprintModule.ProvidesKeyguardManager (this);
			mKeyStore = fingerprintModule.ProvidesKeystore ();
			mKeyGenerator = fingerprintModule.ProvidesKeyGenerator ();
			mCipher = fingerprintModule.ProvidesCipher (mKeyStore);

			RequestPermissions (new [] { Manifest.Permission.UseFingerprint }, 0);
		}
 /// <summary>
 /// Initializes a new instance of the <see cref="AesObfuscator"/> class. 
 /// The aes obfuscator.
 /// </summary>
 /// <param name="salt">
 /// an array of random bytes to use for each (un)obfuscation
 /// </param>
 /// <param name="applicationId">
 /// application identifier, e.g. the package name
 /// </param>
 /// <param name="deviceId">
 /// device identifier. Use as many sources as possible to 
 /// create this unique identifier.
 /// </param>
 public AesObfuscator(byte[] salt, string applicationId, string deviceId)
 {
     try
     {
         SecretKeyFactory factory = SecretKeyFactory.GetInstance(KeygenAlgorithm);
         IKeySpec keySpec = new PBEKeySpec((applicationId + deviceId).ToCharArray(), salt, 1024, 256);
         ISecretKey tmp = factory.GenerateSecret(keySpec);
         ISecretKey secret = new SecretKeySpec(tmp.GetEncoded(), "AES");
         this.encryptor = Cipher.GetInstance(CipherAlgorithm);
         this.encryptor.Init(CipherMode.EncryptMode, secret, new IvParameterSpec(Iv));
         this.decryptor = Cipher.GetInstance(CipherAlgorithm);
         this.decryptor.Init(CipherMode.DecryptMode, secret, new IvParameterSpec(Iv));
     }
     catch (GeneralSecurityException e)
     {
         // This can't happen on a compatible Android device.
         throw new RuntimeException("Invalid environment", e);
     }
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="CryptoTransformAdaptor"/> class.
 /// </summary>
 /// <param name="algorithm">The algorithm.</param>
 /// <param name="transform">The transform.</param>
 internal CryptoTransformAdaptor(SymmetricAlgorithm algorithm, Cipher transform)
 {
     Requires.NotNull(transform, "transform");
     this.algorithm = algorithm;
     this.transform = transform;
 }
        /// <summary>
        /// Initializes the cipher if it has not yet been initialized.
        /// </summary>
        /// <param name="mode">The mode.</param>
        /// <param name="iv">The iv.</param>
        /// <param name="cipher">The cipher.</param>
        /// <exception cref="System.ArgumentException">
        /// Invalid algorithm parameter.
        /// </exception>
        /// <exception cref="System.NotSupportedException">Algorithm not supported.</exception>
        private void InitializeCipher(CipherMode mode, byte[] iv, ref Cipher cipher)
        {
            try
            {
                bool newCipher = false;
                if (cipher == null)
                {
                    cipher = Cipher.GetInstance(this.GetCipherAcquisitionName().ToString());
                    newCipher = true;
                }

                if (this.algorithm.IsBlockCipher() || newCipher)
                {
                    iv = this.ThisOrDefaultIV(iv);
                    using (var ivspec = iv != null ? new IvParameterSpec(iv) : null)
                    {
                        cipher.Init(mode, this.key, ivspec);
                    }
                }
            }
            catch (InvalidKeyException ex)
            {
                throw new ArgumentException(ex.Message, ex);
            }
            catch (NoSuchAlgorithmException ex)
            {
                throw new NotSupportedException("Algorithm not supported.", ex);
            }
            catch (InvalidAlgorithmParameterException ex)
            {
                throw new ArgumentException("Invalid algorithm parameter.", ex);
            }
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="CryptoTransformAdaptor"/> class.
 /// </summary>
 /// <param name="name">The name of the base algorithm to use.</param>
 /// <param name="mode">The algorithm's mode (i.e. streaming or some block mode).</param>
 /// <param name="padding">The padding to use.</param>
 /// <param name="transform">The transform.</param>
 internal CryptoTransformAdaptor(SymmetricAlgorithmName name, SymmetricAlgorithmMode mode, SymmetricAlgorithmPadding padding, Cipher transform)
 {
     Requires.NotNull(transform, "transform");
     this.name = name;
     this.mode = mode;
     this.padding = padding;
     this.transform = transform;
 }
        private byte[] DoCipherOperation(Cipher cipher, byte[] data)
        {
            Requires.NotNull(cipher, nameof(cipher));
            Requires.NotNull(data, nameof(data));

            // Android returns null when given an empty input.
            if (this.Padding != SymmetricAlgorithmPadding.PKCS7 && data.Length == 0)
            {
                return data;
            }

            try
            {
                return this.CanStreamAcrossTopLevelCipherOperations
                    ? cipher.Update(data)
                    : cipher.DoFinal(data);
            }
            catch (IllegalBlockSizeException ex)
            {
                throw new ArgumentException("Illegal block size.", ex);
            }
        }
        /// <summary>
        /// Gets the block size (in bytes) for the specified algorithm.
        /// </summary>
        /// <param name="pclAlgorithm">The PCL algorithm.</param>
        /// <param name="algorithm">The platform-specific algorithm.</param>
        /// <returns>The block size (in bytes).</returns>
        internal static int GetBlockSize(SymmetricAlgorithm pclAlgorithm, Cipher algorithm)
        {
            Requires.NotNull(algorithm, "algorithm");

            if (algorithm.BlockSize == 0 && pclAlgorithm.GetName() == SymmetricAlgorithmName.Rc4)
            {
                // This is a streaming cipher without a block size. Return 1 to emulate behavior of other platforms.
                return 1;
            }

            return algorithm.BlockSize;
        }