Esempio n. 1
0
        public string DecriptFile(string filename, string name)
        {
            Java.IO.File extStore = Android.OS.Environment.GetExternalStoragePublicDirectory("Temp");
            Android.Util.Log.Error("Decryption Started", extStore + "");
            FileInputStream fis = new FileInputStream(extStore + "/" + filename);

            // createFile(filename, extStore);
            FileOutputStream fos = new FileOutputStream(extStore + "/" + "decrypted" + filename, false);

            System.IO.FileStream fs = System.IO.File.OpenWrite(extStore + "/" + name);
            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.DecryptMode, skeySpec, iv);
            CipherOutputStream cos = new CipherOutputStream(fs, cipher);
            int b;

            byte[] d = new byte[1024 * 1024];
            while ((b = fis.Read(d)) != -1)
            {
                cos.Write(d, 0, b);
            }
            System.IO.File.Delete(extStore + "/" + "decrypted" + filename);
            Android.Util.Log.Error("Decryption Ended", extStore + "/" + "decrypted" + name);
            return(extStore.ToString());
            //return d;
        }
        public void decrypt()
        {
            try
            {
                string path = "/storage/emulated/0/jukebox/Songs";
                //   Log.d("Files", "Path: " + path);
                Java.IO.File   directory = new Java.IO.File(path);
                Java.IO.File[] files     = directory.ListFiles();
                //  Log.d("Files", "Size: " + files.length);
                for (int i = 0; i < files.Length; i++)
                {
                    //  Log.d("Files", "FileName:" + files[i].getName());
                    var fileName = files[i].Name;
                    int index    = fileName.LastIndexOf(".");
                    if (index > 0)
                    {
                        fileName = fileName.Substring(0, index);
                    }

                    //Java.IO.File extStore = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryMovies);
                    Android.Util.Log.Error("Decryption Started", directory + "");
                    FileInputStream fis = new FileInputStream(directory + "/" + fileName + ".aes");

                    createFile(files[i].Name, directory);
                    FileOutputStream     fos = new FileOutputStream(directory + "/" + "decrypted" + fileName, false);
                    System.IO.FileStream fs  = System.IO.File.OpenWrite(directory + "/" + "decrypted" + fileName);
                    // Create cipher

                    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.DecryptMode, skeySpec, iv);

                    startTime = System.DateTime.Now.Millisecond;
                    CipherOutputStream cos = new CipherOutputStream(fs, cipher);
                    int    b;
                    byte[] d = new byte[1024 * 1024];
                    while ((b = fis.Read(d)) != -1)
                    {
                        cos.Write(d, 0, b);
                    }

                    stopTime = System.DateTime.Now.Millisecond;

                    Android.Util.Log.Error("Decryption Ended", directory + "/" + "decrypted" + fileName);
                    Android.Util.Log.Error("Time Elapsed", ((stopTime - startTime) / 1000.0) + "");

                    cos.Flush();
                    cos.Close();
                    fis.Close();
                }
            }
            catch (Exception e)
            {
                Android.Util.Log.Error("lv", e.Message);
            }
        }
        public void decrypt(string filename)
        {
            try
            {
                Java.IO.File extStore = new Java.IO.File("/storage/emulated/0/jukebox/Songs");
                Android.Util.Log.Error("Decryption Started", extStore + "");
                FileInputStream fis = new FileInputStream(extStore + "/" + filename + ".aes");

                createFile(filename, extStore);
                FileOutputStream     fos = new FileOutputStream(extStore + "/" + "decrypted" + filename, false);
                System.IO.FileStream fs  = System.IO.File.OpenWrite(extStore + "/" + "decrypted" + filename);
                // Create cipher

                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.DecryptMode, skeySpec, iv);

                startTime = System.DateTime.Now.Millisecond;
                CipherOutputStream cos  = new CipherOutputStream(fs, cipher);
                Java.IO.File       file = new Java.IO.File("/storage/emulated/0/jukebox/Songs" + "/" + filename);
                if (file.Delete())
                {
                    Android.Util.Log.Error("File Deteted", extStore + filename);
                }
                else
                {
                    Android.Util.Log.Error("File Doesn't exists", extStore + filename);
                }

                int    b;
                byte[] d = new byte[1024 * 1024];
                while ((b = fis.Read(d)) != -1)
                {
                    cos.Write(d, 0, b);
                }

                stopTime = System.DateTime.Now.Millisecond;

                Android.Util.Log.Error("Decryption Ended", extStore + "/" + "decrypted" + filename);
                Android.Util.Log.Error("Time Elapsed", ((stopTime - startTime) / 1000.0) + "");

                cos.Flush();
                cos.Close();
                fis.Close();
            }
            catch (Exception e)
            {
                Android.Util.Log.Error("lv", e.Message);
            }
        }
Esempio n. 4
0
 public virtual void encrypt(Stream paramInputStream, Stream paramOutputStream)
 {
     try
     {
         paramOutputStream = new CipherOutputStream(paramOutputStream, this.ecipher);
         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[] 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));
        }
Esempio n. 7
0
 public CipherStreamAdapter(CipherOutputStream output)
 {
     _output  = output;
     _adapter = new OutputStreamInvoker(output);
 }