• C# 加密


    一、RSA加密解密

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Security.Cryptography;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace RSADemoTest
    {
        class Program
        {
            static void Main()
            {
                try
                {
                    string str_Plain_Text = "How are you?How are you?How are you?How are you?=-popopolA";
                    Console.WriteLine("明文:" + str_Plain_Text);
                    Console.WriteLine("长度:" + str_Plain_Text.Length.ToString());
                    Console.WriteLine();
                    RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
                    string str_Public_Key;
                    string str_Private_Key;
                    string str_Cypher_Text = RSA_Encrypt(str_Plain_Text, out str_Public_Key, out str_Private_Key);
                    Console.WriteLine("密文:" + str_Cypher_Text);
                    Console.WriteLine("公钥:" + str_Public_Key);
                    Console.WriteLine("私钥:" + str_Private_Key);
                    string str_Plain_Text2 = RSA_Decrypt(str_Cypher_Text, str_Private_Key);
                    Console.WriteLine("解密:" + str_Plain_Text2);
                    Console.WriteLine();
                    Console.ReadKey();
                }
                catch (ArgumentNullException)
                {
                    Console.WriteLine("Encryption failed.");
                }
            }
            //RSA加密,随机生成公私钥对并作为出参返回
            static public string RSA_Encrypt(string str_Plain_Text, out string str_Public_Key, out string str_Private_Key)
            {
                str_Public_Key = "";
                str_Private_Key = "";
                UnicodeEncoding ByteConverter = new UnicodeEncoding();
                byte[] DataToEncrypt = ByteConverter.GetBytes(str_Plain_Text);
                try
                {
                    RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
                    str_Public_Key = Convert.ToBase64String(RSA.ExportCspBlob(false));
                    str_Private_Key = Convert.ToBase64String(RSA.ExportCspBlob(true));
                    byte[] bytes_Cypher_Text = RSA.Encrypt(DataToEncrypt, false);
                    str_Public_Key = Convert.ToBase64String(RSA.ExportCspBlob(false));
                    str_Private_Key = Convert.ToBase64String(RSA.ExportCspBlob(true));
                    string str_Cypher_Text = Convert.ToBase64String(bytes_Cypher_Text);
                    return str_Cypher_Text;
                }
                catch (CryptographicException e)
                {
                    Console.WriteLine(e.Message);
                    return null;
                }
            }
            //RSA解密
            static public string RSA_Decrypt(string str_Cypher_Text, string str_Private_Key)
            {
                byte[] DataToDecrypt = Convert.FromBase64String(str_Cypher_Text);
                try
                {
                    RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
                    byte[] bytes_Public_Key = Convert.FromBase64String(str_Private_Key);
                    RSA.ImportCspBlob(bytes_Public_Key);
                    byte[] bytes_Plain_Text = RSA.Decrypt(DataToDecrypt, false);
                    UnicodeEncoding ByteConverter = new UnicodeEncoding();
                    string str_Plain_Text = ByteConverter.GetString(bytes_Plain_Text);
                    return str_Plain_Text;
                }
                catch (CryptographicException e)
                {
                    Console.WriteLine(e.ToString());
                    return null;
                }
            }
        }
    }
    View Code
  • 相关阅读:
    uni-app 发起请求,Toast 消息提示 ,跳转页面
    uView初识
    uni-app初识
    docker目录 /var/lib/docker/containers 日志清理
    Linux中使用pigz工具更快的压缩和解压文件
    docker 修改默认网段
    LayaAir提示:版本不匹配!全局tsc(2.7.2)!=VS Code的语言服务(2.1.5)。可能出现不一致的编译错误
    C++ 格式化 浮点为字符串
    安装 ta-lib
    编译 python 代码
  • 原文地址:https://www.cnblogs.com/eric-gms/p/5803617.html
Copyright © 2020-2023  润新知