private PrivateKey extractPrivateKey(System.IO.Stream PKCSFile, string password)
        {
            if (PKCSFile == null)
            {
                return(null);
            }

            try
            {
                KeyStore keyStore = KeyStore.getInstance("PKCS12");
                keyStore.load(PKCSFile, password.ToCharArray());

                string keyAlias = null;
                IEnumerator <string> aliases = keyStore.aliases();
                while (aliases.MoveNext())
                {
                    keyAlias = aliases.Current;
                    if (keyStore.isKeyEntry(keyAlias))
                    {
                        break;
                    }
                }

                if (keyAlias != null)
                {
                    KeyStore.PrivateKeyEntry keyEntry = (KeyStore.PrivateKeyEntry)keyStore.getEntry(keyAlias, new KeyStore.PasswordProtection(password.ToCharArray()));
                    return(keyEntry.PrivateKey);
                }
            }
            catch (Exception e)
            {
                Log.e(TAG, e.Message);
            }
            return(null);
        }
        public string DecryptKey(string encryptedKey)
        {
            KeyStore.PrivateKeyEntry privateKeyEntry = (KeyStore.PrivateKeyEntry)_keyStore.GetEntry(_alias, null);

            if (privateKeyEntry == null)
            {
                return(null);
            }

            var    privateKey = privateKeyEntry.PrivateKey;
            Cipher cipher     = Cipher.GetInstance("RSA/ECB/PKCS1Padding");

            cipher.Init(CipherMode.DecryptMode, privateKey);

            byte[] encryptedBytes = Nethereum.Hex.HexConvertors.Extensions.HexByteConvertorExtensions.HexToByteArray(encryptedKey);

            CipherInputStream cipherInputStream = new CipherInputStream(
                new MemoryStream(encryptedBytes), cipher);

            List <byte> values = new List <byte>();

            int nextByte;

            while ((nextByte = cipherInputStream.Read()) != -1)
            {
                values.Add((byte)nextByte);
            }

            return(Encoding.UTF8.GetString(values.ToArray(), 0, values.Count));
        }
Ejemplo n.º 3
0
        private KeyPair GetKeyPair(string keyName)
        {
            var keyStore = KeyStore.GetInstance(KEY_STORE_NAME);

            keyStore.Load(null);
            if (keyStore.ContainsAlias(keyName))
            {
                // Get public key
                var publicKey = keyStore.GetCertificate(keyName).PublicKey;
                // Get private key
                KeyStore.PrivateKeyEntry privateKey = (KeyStore.PrivateKeyEntry)keyStore.GetEntry(keyName, null);
                // Return a key pair
                return(new KeyPair(publicKey, (IPrivateKey)privateKey.PrivateKey));
            }
            return(null);
        }
        private byte[] RSAEncrypt(byte[] secret)
        {
            KeyStore.PrivateKeyEntry privateKeyEntry = (KeyStore.PrivateKeyEntry)_keyStore.GetEntry(_secureStoredKeyAlias, null);
            // Encrypt the text
            Cipher inputCipher = Cipher.GetInstance(_RSAMode, "AndroidOpenSSL");

            inputCipher.Init(CipherMode.EncryptMode, privateKeyEntry.Certificate.PublicKey);

            MemoryStream       outputStream       = new MemoryStream();
            CipherOutputStream cipherOutputStream = new CipherOutputStream(outputStream, inputCipher);

            cipherOutputStream.Write(secret);
            cipherOutputStream.Close();

            return(outputStream.ToArray());
        }
        public string EncryptKey(string privKey)
        {
            KeyStore.PrivateKeyEntry privateKeyEntry = (KeyStore.PrivateKeyEntry)_keyStore.GetEntry(_alias, null);
            var publicKey = privateKeyEntry.Certificate.PublicKey;

            Cipher cipher = Cipher.GetInstance("RSA/ECB/PKCS1Padding", "AndroidOpenSSL");

            cipher.Init(CipherMode.EncryptMode, publicKey);//

            MemoryStream       outputStream       = new MemoryStream();
            CipherOutputStream cipherOutputStream = new CipherOutputStream(
                outputStream, cipher);

            cipherOutputStream.Write(Encoding.UTF8.GetBytes(privKey));
            cipherOutputStream.Close();

            byte[] encryptedBytes = outputStream.ToArray();
            return(Nethereum.Hex.HexConvertors.Extensions.HexByteConvertorExtensions.ToHexCompact(encryptedBytes));
        }
        private byte[] RSADecrypt(byte[] encrypted)
        {
            KeyStore.PrivateKeyEntry privateKeyEntry = (KeyStore.PrivateKeyEntry)_keyStore.GetEntry(_secureStoredKeyAlias, null);

            Cipher output = Cipher.GetInstance(_RSAMode, "AndroidOpenSSL");
            IKey   pk     = privateKeyEntry.PrivateKey;

            output.Init(CipherMode.DecryptMode, pk);

            CipherInputStream cipherInputStream = new CipherInputStream(new MemoryStream(encrypted), output);
            List <byte>       values            = new List <byte>();

            int nextByte;

            while ((nextByte = cipherInputStream.Read()) != -1)
            {
                values.Add((byte)nextByte);
            }

            return(values.ToArray());
        }