Esempio n. 1
0
        private static void DecryptDataMacKey()
        {
            string key = "fKpiCXIkJ9Mk4ZnXpWd/5jtu8ib0dTgOzBZp/9EgtjqxNC7cT+YfL2YDMObjFS9AsUecP3uk3R5X3Oos2G/Xz2iqsgbePXHVDMny551chHGdIYse3LwZE08fiI/cgyB16kQSPQSmJhuegwXG8n+ahCyK055JoiYwmXGSvmPqv4dh/K29+0UWmRqYZjQptTpKD7fH9nFMR1TthHZS3sFKW6cFg4ZQ5aDGZBVFJDqOtOLZzpMeSdVoCC5pWWnnXwaANzYZD6v2MbZV3Nvl5no2GMVHRtjBO/9MrFifnd1Y1DNKkzoBGmvVFm7uRtEAPyDExL7Tiey47+t2OUTtD9vtRFQSLze8oszSnb0rgIEcJKE7+guPykkpFOp02OWg7ytVqpqelTU8TgJuT9Ep0UKWJZaH1QhS+9UCTYd7koBwVdSURss6g4PeHr09x+JesK5cJIfl9Xq/QnqMXlsq6jiDkpN1gjIocmfAQ04z5Oz8QNNXesQ0SR9uCN4zIXGyLPqsT2qCmjzvkngXfx90JhS/CRH/7VBbXni1SMg6Nc0EfGfLbS7AImrpgphX0/SGYr0OaXemC5CASPqBjUc0w6c1i2QFP8JrP1P/I5N7nvrJ4WOgN8tGoRDLsZe7r+qHrtCSQ3RRDwwq7JaWJ5aSVm4TR5ReFTs2RelXzGE9oYuj7F4=";

            string strPrivate = File.ReadAllText(@"Keys\mrch-rsaPrivate.key");
            var privateKey = new KeyWorker(strPrivate, KeyFormat.ASN);

            string aesKey = privateKey.Decrypt(key);
            Console.WriteLine(aesKey);
        }
Esempio n. 2
0
        public KeyWorker GenerateWorker()
        {
            if (OnlyPublic)
            {
                KeyWorker pubWorker = new KeyWorker(this.PublicKey, this._format);

                return(pubWorker);
            }
            else
            {
                KeyWorker priWorker = new KeyWorker(this.PrivateKey, this._format);

                return(priWorker);
            }
        }
Esempio n. 3
0
        public static string Encrypt(RsaKeyFormat rsaKeyFormat, string publicKey, string data)
        {
            KeyWorker _keyWorker;

            switch (rsaKeyFormat)
            {
                case RsaKeyFormat.XML:
                    _keyWorker = new KeyWorker(publicKey, KeyFormat.XML);
                    break;
                case RsaKeyFormat.ASN:
                    _keyWorker = new KeyWorker(publicKey, KeyFormat.ASN);
                    break;
                case RsaKeyFormat.PEM:
                    _keyWorker = new KeyWorker(publicKey, KeyFormat.PEM);
                    break;
                default:
                    throw new Exception(string.Format("the rsa key format: {0} is not supported.", rsaKeyFormat));
            }

            return _keyWorker.Encrypt(data);
        }
Esempio n. 4
0
        public KeyWorker GenerateWorker()
        {
            if (OnlyPublic)
            {
                KeyWorker pubWorker = new KeyWorker(this.PublicKey, this._format);

                return pubWorker;
            }
            else
            {
                KeyWorker priWorker = new KeyWorker(this.PrivateKey, this._format);

                return priWorker;
            }
        }
Esempio n. 5
0
        private static void Test(int keySize = 2048)
        {
            //const int keySize = 2048;
            Console.WriteLine("=========================== 密钥长度{0} ===========================", keySize);

            //生成公私钥对
            KeyPair keyPair = KeyGenerator.GenerateKeyPair(KeyFormat.XML, keySize);

            //转换成不同的格式
            KeyPair asnKeyPair = keyPair.ToASNKeyPair();
            KeyPair xmlKeyPair = asnKeyPair.ToXMLKeyPair();
            KeyPair pemKeyPair = xmlKeyPair.ToPEMKeyPair();

            //获取公私钥
            string privateKey = xmlKeyPair.PrivateKey;
            string publicKey = xmlKeyPair.PublicKey;

            //加解密
            KeyWorker privateWorker = new KeyWorker(privateKey, KeyFormat.XML);
            KeyWorker publicWorker = new KeyWorker(publicKey, KeyFormat.XML);

            //XML
            Console.WriteLine(privateWorker.Decrypt(publicWorker.Encrypt("你好!世界")));
            Console.WriteLine(publicWorker.Decrypt(privateWorker.Encrypt("你好!中国")));

            //ASN
            privateWorker = new KeyWorker(asnKeyPair.PrivateKey, KeyFormat.ASN);
            publicWorker = new KeyWorker(asnKeyPair.PublicKey, KeyFormat.ASN);
            Console.WriteLine(privateWorker.Decrypt(publicWorker.Encrypt("你好!世界")));
            Console.WriteLine(publicWorker.Decrypt(privateWorker.Encrypt("你好!中国")));

            //PEM
            privateWorker = new KeyWorker(pemKeyPair.PrivateKey, KeyFormat.PEM);
            publicWorker = new KeyWorker(pemKeyPair.PublicKey, KeyFormat.PEM);
            Console.WriteLine(privateWorker.Decrypt(publicWorker.Encrypt("你好!世界")));
            Console.WriteLine(publicWorker.Decrypt(privateWorker.Encrypt("你好!中国")));
        }