Click here to Skip to main content
15,887,434 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have to send a file from User A to the server
What I want here is:
1. From user A file will be shared to server in encrypted form
2. server will receive the file, decrypt and store this file.
3. This action should perform using asymmetric key encryption concept.
4. It is advantageous to use separate GUI form for both user and server
Can anyone help me to implement this one?

What I have tried:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Security;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.OpenSsl;
using Org.BouncyCastle.Crypto.Generators;
using Org.BouncyCastle.Crypto.Encodings;
using Org.BouncyCastle.Crypto.Engines;


namespace Authentication_Check
{
    class Program
    {
        static void Main(string[] args)
        {
            // Declaring Variables //
            string SourceData;
            byte[] tmpSource;
            byte[] tmpHash;


            // Enter any Text //
            Console.WriteLine("Enter any Tetx");
            SourceData = Console.ReadLine();

            //Create a byte array from Source data
            tmpSource = ASCIIEncoding.ASCII.GetBytes(SourceData);
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine(" Key pair is generating....Please wait for while");
            Console.WriteLine();


            //RSA key pair Generator generates the RSA kay pair based on the Random Number and the strength of key required 
            RsaKeyPairGenerator rsaKeyPairGenerator = new RsaKeyPairGenerator();
            rsaKeyPairGenerator.Init(new KeyGenerationParameters(new SecureRandom(), 2048));
            AsymmetricCipherKeyPair keyPair = rsaKeyPairGenerator.GenerateKeyPair();

            //Extracting the private key from the pair
            RsaKeyParameters privatekey = (RsaKeyParameters)keyPair.Private;
            RsaKeyParameters publickey = (RsaKeyParameters)keyPair.Public;

            //To print the public key in pem format
            TextWriter textWriter1 = new StringWriter();
            PemWriter pemWriter1 = new PemWriter(textWriter1);
            pemWriter1.WriteObject(publickey);
            pemWriter1.Writer.Flush();
            string print_publickey = textWriter1.ToString();
            Console.WriteLine("Public key is: {0}", print_publickey);
            Console.WriteLine();


            // Encryption Process
            IAsymmetricBlockCipher cipher = new OaepEncoding(new RsaEngine());
            cipher.Init(true, publickey);
            byte[] ciphertext = cipher.ProcessBlock(tmpSource, 0, tmpSource.Length);
            string result = Encoding.UTF8.GetString(ciphertext);
            Console.WriteLine("Encrypted text:");
            Console.WriteLine(result);
            Console.WriteLine();
            Console.WriteLine();

            Console.WriteLine("Do you want to decrypt the text? press 'Y' for yes and any other key key for no");
            char input = Console.ReadKey().KeyChar;

            if (input == 'Y' || input == 'y')
                Decryption(ciphertext, privatekey);
        }

        static void Decryption(byte[] ct, RsaKeyParameters pvtkey)
        {
            IAsymmetricBlockCipher Cipher1 = new OaepEncoding(new RsaEngine());
            Cipher1.Init(false, pvtkey);
            byte[] deciphered = Cipher1.ProcessBlock(ct, 0, ct.Length);
            string decipheredText = Encoding.UTF8.GetString(deciphered);
            Console.WriteLine();
            Console.WriteLine();

            Console.WriteLine("Decrypted Text:{0}", decipheredText);
            Console.WriteLine();
            Console.WriteLine();





        }





    }
}
Posted
Updated 26-Dec-18 1:31am

1 solution

We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900