-
-
Save njmube/7b166163fa8cef7c1c4a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
{ | |
static void Main(string[] args) | |
{ | |
string data = "ccccssssss/.'{}#$%^&*"; | |
byte[] binaryData = BitConverter.GetBytes(data); | |
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); | |
rsa.FromXmlString(File.ReadAllText("key")); //key created by rsa.ToXmlString(true) and key.pub created by rsa.rsa.ToXmlString(false); | |
var binaryDigest = rsa.SignData(binaryData, new SHA1CryptoServiceProvider()); | |
string digest = Convert.ToBase64String(binaryDigest); //digest use base64 string to get fix length of digest | |
string stringPassInInternet = digest + data; | |
ReceiverVerifyData(stringPassInInternet); | |
Console.ReadKey(); | |
} | |
static void ReceiverVerifyData(string passStr) | |
{ | |
//receiver knows other people can't sign the data, because other people do not have the private key(only trusted people has privatek key) | |
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); | |
rsa.FromXmlString(File.ReadAllText("key.pub")); | |
string digest = passStr.Substring(0, 172); | |
string data = passStr.Substring(172); | |
var bResult = rsa.VerifyData(BitConverter.GetBytes(data), new SHA1CryptoServiceProvider(), Convert.FromBase64String(digest)); | |
if (bResult) | |
{ | |
Console.WriteLine("send by trusted/verify person"); | |
} | |
else | |
{ | |
Console.WriteLine("send by untrusted/unverify person"); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment