Jump to content

RSA Program

Recommended Posts

RSA is the only asymmetric algorithim available to the .NET C# API. It is useful for moving data from one computer to another without malicious Mallory intercepting or changing the transmission. The RSA part is actually ideal for transmitting keys to the destination computer. Use the key transmission and apply it to a separate symmetric transmission for large files for increased performance.  Big files or transmissions get symmetricly encrypted. Doing a lot of C# development and research lately. I could probably make $50 per company by tooling around my city, and trading the printouts for money, but I am not currently interested. My contribution to the security of the business environment. The algorithm works by swapping public keys between computers, then sending the information along to the destination computer. The network code is not included, but you could see how this could be broken up into controllers or client server programs.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace RSA_Login {

    class Program {

        static void Main(string[] args) {
            RSALogin client = new RSALogin();
            RSALogin server = new RSALogin();
            client.PublicKey = server.GetPublicKey();
            server.PublicKey = client.GetPublicKey();
            byte[] cipherText = server.Encrypt("Hello");
            String password = client.Decrypt(cipherText);
        }//end method

    }//end class

    /// <summary>
    /// RSALogin object used to preform Asymmetric trade of data. 
    /// </summary>
    public class RSALogin {

        RSACryptoServiceProvider alg = new RSACryptoServiceProvider();
        HashAlgorithm hashAlg = HashAlgorithm.Create("SHA1");

        public RSAParameters PublicKey { get; set; }

        public RSALogin() { }

        /// <summary>
        /// Get other computer's public key. Used to trade keys. 
        /// </summary>
        /// <returns></returns>
        public RSAParameters GetPublicKey() {
            alg = new RSACryptoServiceProvider();
            RSAParameters publicParams = alg.ExportParameters(false);
            return publicParams;

        /// <summary>
        /// Encrypt data to send to client. 
        /// </summary>
        /// <param name="plaintext"></param>
        /// <returns></returns>
        public byte[] Encrypt(String plaintext) {
            byte[] plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plaintext);

            byte[] signature = alg.SignData(plainTextBytes, hashAlg);

            byte[] cipherText = alg.Encrypt(plainTextBytes, true);
            return cipherText;
        }//end method

        /// <summary>
        /// Decrypt the cipher text from the server. 
        /// </summary>
        /// <param name="cipherText"></param>
        /// <returns></returns>
        public String Decrypt(byte[] cipherText) {
            byte[] plainTextBytes = alg.Decrypt(cipherText, true);

            byte[] signature = alg.SignData(plainTextBytes, hashAlg);
            bool sigValid = alg.VerifyData(plainTextBytes, hashAlg, signature);

            if (!sigValid)
                throw new CryptographicException("Signature is not valid. ");

            String plainText = new System.Text.UTF8Encoding().GetString(plainTextBytes);

            return plainText;
        }//end method

    }//end method

}//end namespace


Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...