Exemplo n.º 1
0
        public byte[] SignData(byte[] data, byte[] keyPrivate)
        {
            byte[] signature = new byte[SignatureSize];

            int  result;
            bool fEmpty = true;

            for (int i = 0; i < keyPrivate.Length; i++)
            {
                if (keyPrivate[i] != (byte)0xFF)
                {
                    fEmpty = false;
                    break;
                }
            }

            if (fEmpty) // if we don't have a key any signature will do, so return 0x0
            {
                return(signature);
            }

            while ((result = CryptoWrapper.Crypto_SignBuffer(data, data.Length, keyPrivate, signature, signature.Length)) == (int)CryptoWrapper.CRYPTO_RESULT.CONTINUE)
            {
            }

            if (result != (int)dotNetMFCrypto.CryptoWrapper.CRYPTO_RESULT.SUCCESS)
            {
                throw new ApplicationException("Could not sign data");
            }

            return(signature);
        }
Exemplo n.º 2
0
        public KeyPair CreateKeyPair()
        {
            byte[] seed       = new byte[RandomSeedSize + 2 * sizeof(UInt16)];
            byte[] keyPrivate = new byte[PrivateKeySize];
            byte[] keyPublic  = new byte[PublicKeySize];

            ushort delta1, delta2;

            Random random = new Random();

            for (int i = 0; i < 100; i++)
            {
                random.NextBytes(seed);

                //I'm not sure what this does, it was ported from MetaDataProcessor
                if (CryptoWrapper.Crypto_CreateZenithKey(seed, out delta1, out delta2) != (int)dotNetMFCrypto.CryptoWrapper.CRYPTO_RESULT.SUCCESS)
                {
                    continue;
                }

                byte [] d0 = BitConverter.GetBytes(delta1);
                byte [] d1 = BitConverter.GetBytes(delta2);

                seed[RandomSeedSize]     = d0[0];
                seed[RandomSeedSize + 1] = d0[1];
                seed[RandomSeedSize + 2] = d1[0];
                seed[RandomSeedSize + 3] = d1[1];

                if (CryptoWrapper.Crypto_GeneratePrivateKey(seed, keyPrivate) != (int)dotNetMFCrypto.CryptoWrapper.CRYPTO_RESULT.SUCCESS)
                {
                    continue;
                }

                if (CryptoWrapper.Crypto_PublicKeyFromPrivate(keyPrivate, keyPublic) != (int)dotNetMFCrypto.CryptoWrapper.CRYPTO_RESULT.SUCCESS)
                {
                    continue;
                }

                KeyPair keyPair = new KeyPair();
                keyPair.PrivateKey = keyPrivate;
                keyPair.PublicKey  = keyPublic;

                return(keyPair);
            }

            throw new ApplicationException("Could not generate key pair");
        }
Exemplo n.º 3
0
        private void Button_Click_3(object sender, RoutedEventArgs e)
        {
            if (pwA.Password != pwB.Password)
            {
                MessageBox.Show("Passwords unequal", "Error");
                return;
            }
            if (pwA.Password.Length == 0)
            {
                MessageBox.Show("No Password given", "Error");
                return;
            }
            if (string.IsNullOrWhiteSpace(serverFilePath))
            {
                MessageBox.Show("No Server DLL Specified", "Error");
                return;
            }
            status[1]          = true;
            pb.IsIndeterminate = true;
            var dlg = new Microsoft.Win32.SaveFileDialog()
            {
                RestoreDirectory = true,
                AddExtension     = true,
                DefaultExt       = "*.edll",
                FileName         = Path.GetFileNameWithoutExtension(serverFilePath),
                DereferenceLinks = true,
                Title            = "Save as",
                ValidateNames    = true,
                Filter           = "Encrypted Server *.edll|*.edll"
            };

            if (dlg.ShowDialog() == false)
            {
                pb.IsIndeterminate = false;
                return;
            }
            switch (encryptionType)
            {
            case EncryptedServerConfig.EncryptionType.AES:
                crypto = new ServerCryptoAES();
                break;
            }
            cfg.Encryption = encryptionType;
            var enc     = new CryptoWrapper <IServerCrypto>(crypto);
            var mutated = enc.KeyMutation(pwA.SecurePassword);
            var dat     = new Dictionary <string, byte[]>();

            try {
                foreach (var f in localFiles)
                {
                    var sh = Path.GetFileName(f);
                    dat.Add(sh, enc.Encrypt(File.ReadAllBytes(f), mutated));
                    cfg.EncryptedFiles.Add(sh);
                }
                dat.Add(cfg.ServerFileName, enc.Encrypt(File.ReadAllBytes(serverFilePath), mutated));
                if (encryptionType == EncryptedServerConfig.EncryptionType.CUSTOM)
                {
                    cfg.CryptoFileName = Path.GetFileName(cryptoPath);
                    dat.Add(cfg.CryptoFileName, File.ReadAllBytes(cryptoPath));
                }
            }
            catch (Exception ex) {
                MessageBox.Show(ex.ToString(), "ERROR reading and encrypting files");
                pb.IsIndeterminate = false;
                return;
            }
            try {
                if (File.Exists(dlg.FileName))
                {
                    File.Delete(dlg.FileName);
                }
                using (var fs = File.Open(dlg.FileName, FileMode.CreateNew)) {
                    using (var a = new ZipArchive(fs, ZipArchiveMode.Create)) {
                        foreach (var k in dat.Keys)
                        {
                            using (var eStream = a.CreateEntry(k).Open())
                                eStream.Write(dat[k], 0, dat[k].Length);
                        }
                        using (var configStream = a.CreateEntry(EncryptedServerConfig.ConfigFileName).Open())
                            cfg.Save(configStream);
                    }
                }
            }
            catch (Exception ex) {
                MessageBox.Show(ex.ToString(), "ERROR compressing files");
                pb.IsIndeterminate = false;
                return;
            }
            status[5]          = true;
            pb.IsIndeterminate = false;
            ShowStatus();
        }
Exemplo n.º 4
0
        static void Main(string[] args)
        {
            CryptoWrapper wrapper = new CryptoWrapper();

            Console.WriteLine(wrapper.Encrypt("dada", "baba"));
        }
Exemplo n.º 5
0
        static void Main(string[] args)
        {
            CryptoWrapper wrapper = new CryptoWrapper();

            Console.WriteLine(wrapper.Encrypt("dada", "baba"));
        }