如何使用C#实现AES算法 [英] How to implement AES algorithm using C#

查看:85
本文介绍了如何使用C#实现AES算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须将用户A的文件发送到服务器

我想要的是:

1.来自用户A文件将以加密方式共享给服务器表单

2.服务器将接收文件,解密并存储此文件。

3.此操作应使用非对称密钥加密概念执行。

4.为用户和服务器使用单独的GUI表格是有利的

任何人都可以帮我实现这个吗?



< b>我尝试过:



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();





        }





    }
}

推荐答案

我们不做你的功课:这是有原因的。它就是为了让你思考你被告知的事情,并试着理解它。它也在那里,以便您的导师可以识别您身体虚弱的区域,并将更多的注意力集中在补救措施上。



亲自尝试,你可能会发现它不是和你想的一样困难!



如果遇到具体问题,请询问相关问题,我们会尽力提供帮助。但我们不打算为你做这一切!
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!


这篇关于如何使用C#实现AES算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆