Exemple #1
0
        public void SaveFile(string name, Stream data)
        {
            string documentsPath = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, "Temp");

            Directory.CreateDirectory(documentsPath);

            string filePath = Path.Combine(documentsPath, name);

            // Length is 16 byte
            Cipher cipher = Cipher.GetInstance("AES/CBC/PKCS5Padding");

            byte[]          raw      = System.Text.Encoding.Default.GetBytes(sKey);
            SecretKeySpec   skeySpec = new SecretKeySpec(raw, "AES");
            IvParameterSpec iv       = new IvParameterSpec(System.Text.Encoding.Default.GetBytes(ivParameter));//

            cipher.Init(CipherMode.EncryptMode, skeySpec, iv);

            // Wrap the output stream
            CipherInputStream cis = new CipherInputStream(data, cipher);

            byte[] bArray = new byte[1024 * 1024];
            int    b;

            using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate))
            {
                while ((b = cis.Read(bArray)) != -1)
                {
                    fs.Write(bArray, 0, b);
                }
            }
        }
        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));
        }
Exemple #3
0
        public void encrypt(string filename, string path)
        {
            // Here you read the cleartext.
            try
            {
                var extStore = new Java.IO.File("/storage/emulated/0/jukebox/Songs");
                startTime = System.DateTime.Now.Millisecond;
                Android.Util.Log.Error("Encryption Started", extStore + "/" + filename);

                // This stream write the encrypted text. This stream will be wrapped by
                // another stream.
                createFile(filename, extStore);
                var webRequest = WebRequest.Create(path);

                using (var response = webRequest.GetResponse())
                    using (var content = response.GetResponseStream())
                        using (var reader = new StreamReader(content))
                        {
                            var strContent = reader.ReadToEnd();

                            //  System.IO.FileStream fs = System.IO.File.OpenWrite(path);
                            FileOutputStream fos = new FileOutputStream(extStore + "/" + filename + ".aes", false);

                            // Length is 16 byte
                            Cipher          cipher   = Cipher.GetInstance("AES/CBC/PKCS5Padding");
                            byte[]          raw      = System.Text.Encoding.Default.GetBytes(sKey);
                            SecretKeySpec   skeySpec = new SecretKeySpec(raw, "AES");
                            IvParameterSpec iv       = new IvParameterSpec(System.Text.Encoding.Default.GetBytes(ivParameter));//
                            cipher.Init(Javax.Crypto.CipherMode.EncryptMode, skeySpec, iv);

                            // Wrap the output stream
                            CipherInputStream cis = new CipherInputStream(content, cipher);
                            // Write bytes
                            int    b;
                            byte[] d = new byte[1024 * 1024];
                            while ((b = cis.Read(d)) != -1)
                            {
                                fos.Write(d, 0, b);
                            }
                            // Flush and close streams.
                            fos.Flush();
                            fos.Close();
                            cis.Close();
                            stopTime = System.DateTime.Now.Millisecond;
                            Android.Util.Log.Error("Encryption Ended", extStore + "/5mbtest/" + filename + ".aes");
                            Android.Util.Log.Error("Time Elapsed", ((stopTime - startTime) / 1000.0) + "");
                        }
            }
            catch (Exception e)
            {
                Android.Util.Log.Error("lv", e.Message);
            }
        }
 public virtual void decrypt(Stream paramInputStream, Stream paramOutputStream)
 {
     try
     {
         paramInputStream = new CipherInputStream(paramInputStream, this.dcipher);
         int i = 0;
         while ((i = paramInputStream.Read(this.buf, 0, this.buf.Length)) >= 0)
         {
             paramOutputStream.Write(this.buf, 0, i);
         }
         paramOutputStream.Close();
     }
     catch (IOException)
     {
     }
 }
        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());
        }