[C#]实用方法收集

邮件发送

 

using System;
using System.Net.Mail;
using System.Net;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            #region 发送邮件
            string strToMial = "aaa@bbb";
            string strFromMial = "ccc@qq.com";
            string strUserID = "ddd";
            string strUserPassWord = "eee";
            string strServerAddress = "smtp.qq.com";
            string strSubject = "邮件主题";
            string strBody = "邮件内容";
            MailSender(strToMial, strFromMial, strUserID, strUserPassWord, strServerAddress, strSubject, strBody);
            #endregion
        }

        public bool MailSender(string TOMAIL, string FROMMAIL, string USERID, string USERPASSWORD, string SERVERADDRESS, string SUBJECT, string BODY)
        {
            try
            {
                //创建一个邮件对像
                MailMessage mailObject = new MailMessage();
                //设置发件人
                mailObject.From = new MailAddress(FROMMAIL);
                //设置收件人
                mailObject.To.Add(new MailAddress(TOMAIL));
                //设置邮件主题
                mailObject.SubjectEncoding = Encoding.UTF8;
                mailObject.Subject = SUBJECT;
                //设置邮件内容
                mailObject.BodyEncoding = Encoding.UTF8;
                mailObject.Body = BODY;
                //创建一个发送邮件的对象
                SmtpClient smtpClient = new SmtpClient();
                //服务地址  如QQ邮箱  smtp.qq.com
                smtpClient.Host = SERVERADDRESS;
                //帐号和授权码
                smtpClient.Credentials = new NetworkCredential(USERID, USERPASSWORD);
                smtpClient.Send(mailObject);
                return true;
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }
    }
}

 

AES加密

 

using System;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Security.Cryptography;
using System.Collections.Generic;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string strDEMO = "HelloWorld";
            MessageBox.Show("原始的文本是"+ strDEMO) ;
            MessageBox.Show("加密后的文本是"+ AesEncrypt(strDEMO)) ;
            MessageBox.Show("解密后的文本是"+ AesDecypt(AesEncrypt(strDEMO))) ;
        }

        public static string AesEncrypt(string ENCRYPT)
        {
            string secretKey = "0123456789abcdef";

            byte[] keyBytes = Encoding.UTF8.GetBytes(secretKey);

            using (RijndaelManaged cipher = new RijndaelManaged())
            {
                cipher.Mode = CipherMode.CBC;
                cipher.Padding = PaddingMode.PKCS7;
                cipher.KeySize = 128;
                cipher.BlockSize = 128;
                cipher.Key = keyBytes;
                cipher.IV = keyBytes;

                byte[] valueBytes = Encoding.UTF8.GetBytes(ENCRYPT);

                byte[] encrypted;
                using (ICryptoTransform encryptor = cipher.CreateEncryptor())
                {
                    using (MemoryStream ms = new MemoryStream())
                    {
                        using (CryptoStream writer = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
                        {
                            writer.Write(valueBytes, 0, valueBytes.Length);
                            writer.FlushFinalBlock();
                            encrypted = ms.ToArray();

                            StringBuilder sb = new StringBuilder();
                            for (int i = 0; i < encrypted.Length; i++)
                                sb.Append(Convert.ToString(encrypted[i], 16).PadLeft(2, '0'));
                            return sb.ToString().ToUpperInvariant();
                        }
                    }
                }
            }
        }

        public static string AesDecypt(string DECYPT)
        {
            string secretKey = "0123456789abcdef";

            byte[] keyBytes = Encoding.UTF8.GetBytes(secretKey);

            using (RijndaelManaged cipher = new RijndaelManaged())
            {
                cipher.Mode = CipherMode.CBC;
                cipher.Padding = PaddingMode.PKCS7;
                cipher.KeySize = 128;
                cipher.BlockSize = 128;
                cipher.Key = keyBytes;
                cipher.IV = keyBytes;

                List<byte> lstBytes = new List<byte>();
                for (int i = 0; i < DECYPT.Length; i += 2)
                    lstBytes.Add(Convert.ToByte(DECYPT.Substring(i, 2), 16));

                using (ICryptoTransform decryptor = cipher.CreateDecryptor())
                {
                    using (MemoryStream msDecrypt = new MemoryStream(lstBytes.ToArray()))
                    {
                        using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                        {
                            using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                            {
                                return srDecrypt.ReadToEnd();
                            }
                        }
                    }
                }
            }
        }
    }
}

 

 

 

部分收集自网络仅供个人参考学习,侵删

posted @ 2023-02-16 16:26  SairenjiHaruna  阅读(16)  评论(0编辑  收藏  举报