首页 > 编程语言 >[C#]实用方法收集

[C#]实用方法收集

时间:2023-02-16 16:35:23浏览次数:31  
标签:string 收集 C# System mailObject 实用 cipher new using

邮件发送

 

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 = "[email protected]";
            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();
                            }
                        }
                    }
                }
            }
        }
    }
}

 

 

 

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

标签:string,收集,C#,System,mailObject,实用,cipher,new,using
From: https://www.cnblogs.com/sairenjiharuna/p/17127231.html

相关文章

  • C++梳理
    1、基础篇1、C++中的四种智能指针为什么要使⽤智能指针:智能指针其作⽤是管理⼀个指针,避免程序员申请的空间在函数结束时忘记释放,造成内存泄漏这种情况的发⽣。使⽤智能......
  • Unity IL2cpp内存管理
    分配关键类hblkstructhblk{charhb_body[HBLKSIZE];};BoehmGC是按照block来分配内存的,HBLKSIZE表示每个block大小默认值是4096,和页大小一致,猜测合理的值......
  • 新装的PDK调用device不显示
    pdk安装完以后,有些用户在调用的时候显示为空,原因是pdk里面某些文件others没有read权限。   1.cp-a完整的pak路径到others的工作目录下,不能拷贝的文件就是没有权限的......
  • 零代码(低代码)工作流引擎(cube-flowable)
    cube-flowable工作流引擎简介cube-flowable工作流引擎旨在打造一套零代码、领先、且快速实用的引擎工具,助力开发者在面对工作流开发任务时,除去学习工作流框架知识和API......
  • CV-部署芯片接续-CV全流程部署-TF版本
    CV-部署芯片接续-CV全流程部署-TF版本1单个CNN算子importcv2importnumpyasnpimporttensorflowastfimportosfromtensorflow.python.frameworkimportgrap......
  • chatGPT为什么国内用不了,来看看我是如何使用chatGPT
      chatGPT火了这么久在各个平台上看到有很多博主发了使用chatGPT的感受,由于官方限制了中国大陆和中国香港中国台湾地区的访问,导致很多网友无法感受到chatGPT强大,至于为......
  • ignite系列之9-基于Cron的调度
    Runnable和Callable的实例在本地节点可以使用IgniteScheduler.scheduleLocal()方法和Cron语法进行调度用于周期性的执行示例:Igniteignite=Ignition.start(cfg);......
  • CF522A 1200 *
    题意Polycarp在他的微博上发布了一张有趣的照片。他的很多朋友就开始在微博上转发这张图片,这个事情可以被一个字符串描述:name1repostedname2,意思是说name1这个人转发了n......
  • IDEA/PyCharm设置代理功能
    参考步骤测试结果......
  • Linux 上pcntl安装步骤
    一。下载对应的PHP源码包wgethttp://cn2.php.net/get/php-5.5.20.tar.gz/from/this/mirror 二。解压下载的源码文件tar-zxvfphp-5.5.20.tar.gz 三。进入ph......