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)
{
string SourceData;
byte[] tmpSource;
byte[] tmpHash;
Console.WriteLine("Enter any Tetx");
SourceData = Console.ReadLine();
tmpSource = ASCIIEncoding.ASCII.GetBytes(SourceData);
Console.WriteLine();
Console.WriteLine();
Console.WriteLine(" Key pair is generating....Please wait for while");
Console.WriteLine();
RsaKeyPairGenerator rsaKeyPairGenerator = new RsaKeyPairGenerator();
rsaKeyPairGenerator.Init(new KeyGenerationParameters(new SecureRandom(), 2048));
AsymmetricCipherKeyPair keyPair = rsaKeyPairGenerator.GenerateKeyPair();
RsaKeyParameters privatekey = (RsaKeyParameters)keyPair.Private;
RsaKeyParameters publickey = (RsaKeyParameters)keyPair.Public;
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();
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();
}
}
}