public string rsa(string shortMessagetoEncrypt) { RSACryptoSystem cryptoSystem = new RSACryptoSystem(2048); string msg = shortMessagetoEncrypt; byte[] encypt = cryptoSystem.EncryptString(msg); string decrypt = cryptoSystem.DecryptString(encypt); Dictionary <string, object> json = new Dictionary <string, object>(); json.Add("data", msg); json.Add("encrypt", cryptoSystem.ConvertEncryptString(encypt)); json.Add("decrypt", decrypt); return(JsonConvert.SerializeObject(json)); }
static void LogStringEncryptionDecryption() { var originalString = "I want to see this"; var rsaKeyPair = RSAKeyPairGenerator.GenerateRSAKeyPair(); var encryptedString = RSACryptoSystem.Encrypt(originalString, rsaKeyPair.Public); _logger.Info("Encrypting strings..."); _logger.Info(" Original text: {0} \n Encrypted text: {1}", originalString, encryptedString); originalString = RSACryptoSystem.Decrypt(encryptedString, rsaKeyPair.Private); _logger.Info("Decrypting strings..."); _logger.Info(" Encrypted text: {0} \n Original text: {1}", encryptedString, originalString); }
public void SendMessageTo(IUser to, BigInteger messageContents) { _logger.Info("Sending message from '{0}' to '{1}'. Message contents: {2}", this.Name, to.Name, messageContents); var enctyptedMessageContents = RSACryptoSystem.Encrypt(messageContents, to.PublicKey); var originalSignature = RSACryptoSystem.CreateSignature(messageContents, _rsaKeyPair.Private); _logger.Info("Generated original signature: {0}", originalSignature); var encryptedSignature = RSACryptoSystem.Encrypt(originalSignature, to.PublicKey); _logger.Info("Attaching encrypted signature: {0}", encryptedSignature); var message = new Message(enctyptedMessageContents, encryptedSignature); to.ReceiveMessageFrom(this, message); }
static void LogNumericEncryptionDecryption() { BigInteger originalNumber = BigInteger.Parse("12345678987654321123456789"); var rsaKeyPair = RSAKeyPairGenerator.GenerateRSAKeyPair(); var encryptedNumber = RSACryptoSystem.Encrypt(originalNumber, rsaKeyPair.Public); _logger.Info("Encrypting numbers..."); _logger.Info(" Original number: {0} \n Encrypted number: {1}", originalNumber, encryptedNumber); originalNumber = RSACryptoSystem.Decrypt(encryptedNumber, rsaKeyPair.Private); _logger.Info("Decrypting numbers..."); _logger.Info(" Encrypted number: {0} \n Original number: {1}", encryptedNumber, originalNumber); }
public bool ReceiveMessageFrom(IUser from, Message message) { _logger.Info("Received encrypted message from user '{0}'. Message contents (encrypted): {1}", from.Name, message.EnctyptedContents); var decryptedMessageContents = RSACryptoSystem.Decrypt(message.EnctyptedContents, _rsaKeyPair.Private); _logger.Info("Decrypted message contents: {0}", decryptedMessageContents); _logger.Info("Encrypted signature: {0}", message.EncryptedSignature); var decryptedSignature = RSACryptoSystem.Decrypt(message.EncryptedSignature, _rsaKeyPair.Private); _logger.Info("Decrypted signature: {0}", decryptedSignature); var isSignatureValid = RSACryptoSystem.CheckSignatureIsValid(decryptedMessageContents, decryptedSignature, from.PublicKey); _logger.Info("Is signature valid: {0}", isSignatureValid); return(isSignatureValid); }