示例#1
0
        public byte[] EncryptText(EncryptTextMessage message)
        {
            byte[] encryptedText = null;
            //Odradi se enkripcija
            if (message.Algorithm == AlgorithmType.RC4)
            {
                RC4 rc = new RC4(message.Key, message.IV);
                encryptedText = rc.Crypt(message.Data);
            }
            else if (message.Algorithm == AlgorithmType.RC4CTR)
            {
                RC4 rc = new RC4(message.Key, message.IV);
                encryptedText = rc.CryptWithCTR(message.Data);
            }
            else if (message.Algorithm == AlgorithmType.A52 || message.Algorithm == AlgorithmType.A52CTR)
            {
                A52 alg = new A52();
                alg.SetKey(message.Key);
                alg.SetF(message.FKeyA52);

                if (message.Algorithm == AlgorithmType.A52)
                {
                    encryptedText = alg.Crypt(message.Data);
                }
                else
                {
                    alg.SetIV(message.IV);
                    encryptedText = alg.CryptWithCTR(message.Data);
                }
            }
            else if (message.Algorithm == AlgorithmType.RSA)
            {
                RSA rsa = new RSA();
                rsa.E = new BigInteger(message.Key);
                rsa.P = new BigInteger(message.P);
                rsa.Q = new BigInteger(message.Q);
                rsa.GenerateRSA();
                //BigInteger result = rsa.Crypt(new BigInteger(message.Data));
                //encryptedText = result.ToByteArray();
                encryptedText = rsa.Crypt(message.Data);
            }
            else if (message.Algorithm == AlgorithmType.TigerHash)
            {
                TigerHash th  = new TigerHash();
                byte[]    msg = message.Data;
                encryptedText = th.ComputeHash(message.Data);
            }
            return(encryptedText);
        }
示例#2
0
        private async Task <byte[]> DecryptFile(byte[] fileBytes, string algorithm, string key)
        {
            CryptoLibrary.CryptoCTRMode encryptor = new CryptoLibrary.CryptoCTRMode();
            if (algorithm == "A52")
            {
                A52 a52 = new A52();
                encryptor.SetCryptoAlgorithm(a52);
            }
            else if (algorithm == "RC4")
            {
                RC4 rc4 = new RC4();
                encryptor.SetCryptoAlgorithm(rc4);
            }

            byte[] keyBytes = Convert.FromBase64String(key);
            return(encryptor.DecryptFile(fileBytes, keyBytes));
        }
示例#3
0
        public byte[] DecryptText(EncryptTextMessage message)
        {
            byte[] decryptedText = null;
            //Odradi se enkripcija
            if (message.Algorithm == AlgorithmType.RC4)
            {
                RC4 rc = new RC4(message.Key, message.IV);
                decryptedText = rc.Decrypt(message.Data);
            }
            else if (message.Algorithm == AlgorithmType.RC4CTR)
            {
                RC4 rc = new RC4(message.Key, message.IV);
                decryptedText = rc.DecryptWithCTR(message.Data);
            }
            else if (message.Algorithm == AlgorithmType.A52 || message.Algorithm == AlgorithmType.A52CTR)
            {
                A52 alg = new A52();
                alg.SetKey(message.Key);
                alg.SetF(message.FKeyA52);

                if (message.Algorithm == AlgorithmType.A52)
                {
                    decryptedText = alg.Decrypt(message.Data);
                }
                else
                {
                    alg.SetIV(message.IV);
                    decryptedText = alg.DecryptWithCTR(message.Data);
                }
            }
            else if (message.Algorithm == AlgorithmType.RSA)
            {
                RSA rsa = new RSA();
                rsa.E = new BigInteger(message.Key);
                rsa.P = new BigInteger(message.P);
                rsa.Q = new BigInteger(message.Q);
                rsa.GenerateRSA();
                //BigInteger result = rsa.Decrypt(new BigInteger(message.Data));
                //decryptedText = result.ToByteArray();
                decryptedText = rsa.Decrypt(message.Data);
            }

            return(decryptedText);
        }
示例#4
0
        // Dekriptovanje fajl
        public EncryptFileResponse Decrypt(EncryptFileMessage decryptMessage)
        {
            EncryptFileResponse response = new EncryptFileResponse();

            //Odradi se enkripcija
            if (decryptMessage.MetaData.AlgorithmType == AlgorithmType.RC4 || decryptMessage.MetaData.AlgorithmType == AlgorithmType.RC4CTR)
            {
                try
                {
                    using (var bw = new BinaryWriter(File.Open
                                                         (Path.Combine(decryptedFilePath, decryptMessage.MetaData.FileName), FileMode.Create, FileAccess.Write, FileShare.None)))
                    {
                        try
                        {
                            BinaryReader br = new BinaryReader(decryptMessage.Data);
                            RC4          rc4;
                            bool         ctr;
                            if (decryptMessage.MetaData.AlgorithmType == AlgorithmType.RC4)
                            {
                                ctr = false;
                                rc4 = new RC4(decryptMessage.MetaData.Key);
                            }
                            else
                            {
                                ctr = true;
                                rc4 = new RC4(decryptMessage.MetaData.Key, decryptMessage.MetaData.IV);
                            }

                            int    buffSize = 2048;
                            byte[] buff     = new byte[buffSize];
                            int    bytesRead;
                            while ((bytesRead = br.Read(buff, 0, buff.Length)) > 0)
                            {
                                if (bytesRead < buffSize)
                                {
                                    byte[] tmp_buff = new byte[bytesRead];
                                    Buffer.BlockCopy(buff, 0, tmp_buff, 0, bytesRead);
                                    byte[] output;

                                    if (ctr)
                                    {
                                        output = rc4.DecryptWithCTR(tmp_buff);
                                    }
                                    else
                                    {
                                        output = rc4.Decrypt(tmp_buff);
                                    }

                                    bw.Write(output);
                                }
                                else
                                {
                                    byte[] output;

                                    if (ctr)
                                    {
                                        output = rc4.DecryptWithCTR(buff);
                                    }
                                    else
                                    {
                                        output = rc4.Decrypt(buff);
                                    }

                                    bw.Write(output);
                                }
                            }
                            response.Finished = true;
                        }
                        catch (Exception ex)
                        {
                            //Console.Write("Exception");
                        }
                    }
                }
                catch (Exception ex)
                {
                }
            }
            else if (decryptMessage.MetaData.AlgorithmType == AlgorithmType.A52 || decryptMessage.MetaData.AlgorithmType == AlgorithmType.A52CTR)
            {
                try
                {
                    using (var bw = new BinaryWriter(File.Open
                                                         (Path.Combine(decryptedFilePath, decryptMessage.MetaData.FileName), FileMode.Create, FileAccess.Write, FileShare.None)))
                    {
                        try
                        {
                            BinaryReader br = new BinaryReader(decryptMessage.Data);

                            A52 a52 = new A52();
                            a52.SetKey(decryptMessage.MetaData.Key);
                            a52.SetF(decryptMessage.MetaData.FKeyA52);
                            bool ctr = false;

                            if (decryptMessage.MetaData.AlgorithmType == AlgorithmType.A52CTR)
                            {
                                a52.SetIV(decryptMessage.MetaData.IV);
                                ctr = true;
                            }

                            int    buffSize = 2048;
                            byte[] buff     = new byte[buffSize];
                            int    bytesRead;
                            while ((bytesRead = br.Read(buff, 0, buff.Length)) > 0)
                            {
                                if (bytesRead < buffSize)
                                {
                                    byte[] tmp_buff = new byte[bytesRead];
                                    Buffer.BlockCopy(buff, 0, tmp_buff, 0, bytesRead);
                                    byte[] output;

                                    if (ctr)
                                    {
                                        output = a52.DecryptWithCTR(tmp_buff);
                                    }
                                    else
                                    {
                                        output = a52.Decrypt(tmp_buff);
                                    }

                                    bw.Write(output);
                                }
                                else
                                {
                                    byte[] output;

                                    if (ctr)
                                    {
                                        output = a52.DecryptWithCTR(buff);
                                    }
                                    else
                                    {
                                        output = a52.Decrypt(buff);
                                    }

                                    bw.Write(output);
                                }
                            }
                            response.Finished = true;
                        }
                        catch (Exception ex)
                        {
                        }
                    }
                }
                catch (Exception ex)
                {
                }
            }

            else
            {
                response.Finished = false;
            }
            return(response);
        }