Skip to content

Instantly share code, notes, and snippets.

@imzjy
Created August 26, 2014 04:07
Show Gist options
  • Save imzjy/4e2cbca52ff0461ee34b to your computer and use it in GitHub Desktop.
Save imzjy/4e2cbca52ff0461ee34b to your computer and use it in GitHub Desktop.
Encrypt and Decrypt with public key and private key(RSA based)
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;
namespace ConsoleTester
{
public class BitConverter
{
static public string EncodeTo64(string toEncode)
{
byte[] toEncodeAsBytes
= System.Text.ASCIIEncoding.ASCII.GetBytes(toEncode);
string returnValue
= System.Convert.ToBase64String(toEncodeAsBytes);
return returnValue;
}
static public string DecodeFrom64(string encodedData)
{
byte[] encodedDataAsBytes
= System.Convert.FromBase64String(encodedData);
string returnValue =
System.Text.ASCIIEncoding.ASCII.GetString(encodedDataAsBytes);
return returnValue;
}
static public byte[] GetBytes(string s)
{
return Convert.FromBase64String(EncodeTo64(s));
}
static public string GetString(byte[] b)
{
return DecodeFrom64(Convert.ToBase64String(b));
}
}
class Program
{
//Reference:http://stackoverflow.com/questions/21702662/system-security-cryptography-cryptographicexception-bad-length-in-rsacryptoser
//RSA is only meant to be used for encrypting small amounts of data. The exact amount you can encrypt depends on the key length + the amount used by the padding. A 1024 bit key would allow for a bit above 100 bytes.
//Since RSA is QUITE SLOW, the usual way to encrypt large messages is using hybrid encryption. In hybrid encryption you use a fast symmetric encryption algorithm (like AES) for encrypting the data with a random key. The random key is then encrypted with RSA and send along with the symmetric key encrypted data.
static void Main(string[] args)
{
string data = "this string is often very short, used for key from symmetric encryption ";
byte[] binaryData = BitConverter.GetBytes(data);
//密钥对生成:private key: rsa.ToXmlString(true), public key rsa.ToXmlString(false);
//加密用公钥
RSACryptoServiceProvider rsaPublic = new RSACryptoServiceProvider();
rsaPublic.FromXmlString("<RSAKeyValue><Modulus>wRF/xutZI+fDG+2381DXXYmcGPdc1QHv7ymVGcAT+YHyjXAdlJlODGhsNT3TRuVc6XTInKblPflPTWt90SQ2llOZuFQyBo6McqZh1yKpquDzhPxMrFeX/hERYMYFapNATvKC+D61/4KCET9UFIJ37X3TYJ5xduo6r66M46DZ9uE=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>");
var encryptedBinary = rsaPublic.Encrypt(binaryData, true);
//解密用私钥
RSACryptoServiceProvider rsaPrivate = new RSACryptoServiceProvider();
rsaPrivate.FromXmlString("<RSAKeyValue><Modulus>wRF/xutZI+fDG+2381DXXYmcGPdc1QHv7ymVGcAT+YHyjXAdlJlODGhsNT3TRuVc6XTInKblPflPTWt90SQ2llOZuFQyBo6McqZh1yKpquDzhPxMrFeX/hERYMYFapNATvKC+D61/4KCET9UFIJ37X3TYJ5xduo6r66M46DZ9uE=</Modulus><Exponent>AQAB</Exponent><P>/xqGK4jsKFqPx4kTtWl1g+SuXEcnu/1nfDYz6C2RqI8IqAeYM67NCyyZr+95vKoatrHpJjV3J2iESWnOR27Cyw==</P><Q>wb8r/W1F+9530AoiUySwG1lAX67cpqt5lJj8sSes+YQwbQs2pwcGgK5NXgbDjRZxVjpX2WPUjq5ZULro6ma7gw==</Q><DP>KXWLFqozDz/gnCIoBNS115Cj0bVyvLerSOGaQAKf6JAObRiG4OdoxYcjFxfJZiMTIlv5s07/JXV0AmZisvNKTw==</DP><DQ>mJ8p3gc4fAJxy3ZQN8LM355dMYKj2r91lPM3C66/egcBxsRwlv7XxoKH+6vEW930BgMLtPoJNSRuw0OZuxyWVQ==</DQ><InverseQ>dSdahMN927+HTuKg3AUpkGHPv56XplZDswQriVMBuyVfLZoYnDnMr19ErB/3RcfPjHaocu5L4x9S+KQ5IR6KVQ==</InverseQ><D>mnz/KtP9sy5rwDha3nbMrktDvfKJtORk8pHqff1lvlxOEwEomlSF0IXqODwtiGhWDXTmZftiKlIJM+EUfzqj9gTrZMRJjrI558ukaeLzLJEk1YLMFH94qlMfo17O5YqJ0hor+uLLBGLM5oU0GtiGXJE0T+gR/5gsJZkwtoVrJXU=</D></RSAKeyValue>"); //key created by rsa.ToXmlString(true) and key.pub created by rsa.rsa.ToXmlString(false);
var decryptedBinary = rsaPrivate.Decrypt(encryptedBinary, true);
var decryptedString = BitConverter.GetString(decryptedBinary);
if (data == decryptedString)
{
Console.WriteLine("success!");
}
Console.ReadKey();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment