예제 #1
0
        public static string rsa_prikey_decrypt(string pri_key, string data, KeyFormat format, int key_len = 1024)
        {
            var rsa_work = new KeyWorker(pri_key, true, format, key_len);
            var de_data  = rsa_work.Decrypt(data);

            return(de_data);
        }
예제 #2
0
        public static string rsa_pubkey_encrypt(string pub_key, string data, KeyFormat format, int key_len = 1024)
        {
            var rsa_work = new KeyWorker(pub_key, false, format, key_len);
            var en_data  = rsa_work.Encrypt(data);

            return(en_data);
        }
예제 #3
0
        public static string rsa_prikey_sign(string pri_key, string en_data, KeyFormat format, int key_len = 1024)
        {
            var sign_block = deal_string.sha256(en_data).ToUpper();
            var rsa_work   = new KeyWorker(pri_key, true, format, key_len);
            var sign       = rsa_work.Encrypt(sign_block);

            return(sign);
        }
예제 #4
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);
        }
예제 #5
0
        public static bool rsa_pubkey_verify(string pub_key, string en_data, string sign, KeyFormat format, int key_len = 1024)
        {
            var sign_block = deal_string.sha256(en_data).ToUpper();

            var rsa_work    = new KeyWorker(pub_key, false, format, key_len);
            var sign_block1 = rsa_work.Decrypt(sign);

            if (sign_block.Equals(sign_block1))
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
예제 #6
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("你好!中国")));
        }
예제 #7
0
        public static string Decrypt(RsaKeyFormat rsaKeyFormat, string privateKey, string data)
        {
            KeyWorker _keyWorker;

            switch (rsaKeyFormat)
            {
            case RsaKeyFormat.XML:
                _keyWorker = new KeyWorker(privateKey, KeyFormat.XML);
                break;

            case RsaKeyFormat.ASN:
                _keyWorker = new KeyWorker(privateKey, KeyFormat.ASN);
                break;

            case RsaKeyFormat.PEM:
                _keyWorker = new KeyWorker(privateKey, KeyFormat.PEM);
                break;

            default:
                throw new Exception(string.Format("the rsa key format: {0} is not supported.", rsaKeyFormat));
            }

            return(_keyWorker.Decrypt(data));
        }