RSA加密解密与签名验证

关于RSACryption帮助类定义见RSACryption

一、加密与解密

            //定义明文和密文变量
            string plaintext = "天道酬勤,厚德载物!";
            string ciphertext = "";

            //产生密钥
            string prikey = "", pubkey = "";
            rsa.RSAKey(out prikey, out pubkey);

            //加密解密过程
            ciphertext = rsa.RSAEncrypt(pubkey, plaintext);
            plaintext = rsa.RSADecrypt(prikey, ciphertext);
View Code

 

二、签名与验证

            //获取明文Hash值
            byte[] plaintextHash = null;
            rsa.GetHash(plaintext, ref plaintextHash);
            //获取签名密文
            string SignatureCiphertext = null;
            rsa.SignatureFormatter(prikey, plaintextHash, ref SignatureCiphertext);
            //验证
            string verify = rsa.SignatureDeformatter(pubkey, plaintextHash, SignatureCiphertext) ? "OK!没问题" : "NO!内容被篡改了";
View Code

 

Tips:利用RSA的签名和验证功能还可简单实现软件注册码功能,签名即注册码,验证即验证。

相关链接

RSA分段加密【解决“不正确的长度”的异常】

posted @ 2014-07-28 12:59  i迷倪  阅读(328)  评论(0编辑  收藏  举报