Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            RSACryptoServiceProvider provider = PemKeyUtils.GetRSAProviderFromPemFile(@"C:\Users\nmose\Desktop\rsa\rsa_2048_pub.pem");
            AesCryptoServiceProvider aes      = new AesCryptoServiceProvider();


            string originFile = @"C:\Users\nmose\Desktop\rsa\info.txt";
            string encryFile  = @"C:\Users\nmose\Desktop\rsa\info-crypto.bin";
            string decrypFile = @"C:\Users\nmose\Desktop\rsa\info-decrypto.txt";
            string msg        = "";

            CryptoFile.GenerateEncryFile(provider, aes, originFile, encryFile, ref msg);

            RSACryptoServiceProvider providerPrivate = PemKeyUtils.GetRSAProviderFromPemFile(@"C:\Users\nmose\Desktop\rsa\rsa_2048_priv.pem");

            CryptoFile.DecrypFile(providerPrivate, encryFile, decrypFile, ref msg);
        }
Ejemplo n.º 2
0
        public void TestMethod_CryptoFile()
        {
            //要加密的原始数据
            byte[] data = new byte[1024 * 1024 + 7];//假设有一个1M大的文件
            for (int i = 0; i < data.Length; i++)
            {
                data[i] = (byte)(i % 256);
            }

            //使用xuexue这个文本来随便生成一个密钥
            MemoryStream msKey = new MemoryStream();
            StreamWriter sw    = new StreamWriter(msKey);

            while (msKey.Length < 16)
            {
                sw.Write("xuexue");
                sw.Flush();
            }
            msKey.SetLength(16);

            byte[] key = msKey.ToArray();//生成一个16字节的密钥


            MemoryStream msIn    = new MemoryStream(data); //使用原始数据生成一个流
            MemoryStream msAES   = new MemoryStream();     //加密后的数据
            CryptoFile   cfFile1 = CryptoFile.Encrypt(msIn, "加密测试123", msAES, key);

            msAES.Position = 0;                           //把加密后的数据流置回头

            MemoryStream msEDResult = new MemoryStream(); //解密的结果流
            CryptoFile   cfFile     = CryptoFile.Decrypt(msAES, msEDResult, key);

            byte[] dataEDResult = msEDResult.ToArray();

            //确认结果和原先是否一致
            Assert.IsTrue(data.Length == dataEDResult.Length);
            for (int i = 0; i < data.Length; i++)
            {
                Assert.IsTrue(data[i] == dataEDResult[i]);
            }

            Assert.IsTrue(cfFile1.info == cfFile.info);
            Assert.IsTrue(cfFile1.md5 == cfFile.md5);
        }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            /*
             * ArgumentCollection baseArgs = new ArgumentCollection();
             *
             * baseArgs.Add(new Argument("verbose", 'v'));
             *
             * ArgumentCollection syncArgs = new ArgumentCollection()
             * {
             *  Name = "sync",
             * };
             *
             * baseArgs.SubCollections.Add(syncArgs);
             *
             * var parser = new ArgumentsParser()
             * {
             *  AllOptions = baseArgs
             * };
             *
             * parser.Parse(args);
             */
            // sync [file]

            var crypt = new CryptoFile();

            crypt.Gen();
            crypt.GetCryptoKey();

            using (var f = new MemoryStream(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }))
            {
                using (var memstr = new MemoryStream())
                {
                    crypt.EncryptStreamToStream(f, memstr);
                    memstr.Seek(0, SeekOrigin.Begin);
                    using (var memstr2 = new MemoryStream())
                    {
                        crypt.DecryptStreamToStream(memstr, memstr2);
                        var data = memstr2.ToArray();
                    }
                }
            }
        }