C#数据Encrypt加密Encrypt解密的算法使用--非对称算法RSACryptoServiceProvider

C#数据加密解密的非对称算法使用---RSACryptoServiceProvider   Asymmetric algorithms--Encrypt Encrypt  

C#数据Encrypt加密Encrypt解密的相关算法可以参考System.Security.Cryptography,这个类库中包含MD5,SHA1,SHA256,SHA384,SHA512

MD5 and SHA256 are two of the HashAlgorithm subtypes provided by the .NET Framework. Here are all the major algorithms, in ascending order of security (and hash length, in bytes):
MD5(16) → SHA1(20) → SHA256(32) → SHA384(48) → SHA512(64)
The shorter the algorithm, the faster it executes. MD5 is more than 20 times faster than SHA512 and is well suited to calculating file checksums. You can hash hundreds
of megabytes per second with MD5 , and then store its result in a Guid . (A Guid happens to be exactly 16 bytes long, and as a value type it is more tractable than a byte array;
you can meaningfully compare Guid s with the simple equality operator, for instance.)
However, shorter hashes increase the possibility of collision (two distinct files yielding the same hash).
Use at least SHA256 when hashing passwords or other securitysensitive data. MD5 and SHA1 are considered insecure for this
purpose, and are suitable to protect only against accidental corruption, not deliberate tampering.
SHA384 is no faster than SHA512 , so if you want more security than SHA256 , you may as well use SHA512

 

引用using System.Security.Cryptography;

主要代码代码如下:

 static void Main(string[] args)
        {
            log4net.Config.XmlConfigurator.Configure();
            Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

            TestEncryptgrapg();

            Console.ReadLine();
        }

        public static void TestEncryptgrapg()
        {
            using var rsa = new RSACryptoServiceProvider();
            File.WriteAllText("PublicKeyOnly.xml", rsa.ToXmlString(false));
            File.WriteAllText("PublicPrivate.xml", rsa.ToXmlString(true));

            byte[] data = Encoding.UTF8.GetBytes("Message to encrypt");
            string publicKeyOnly = File.ReadAllText("PublicKeyOnly.xml");
            string publicPrivate = File.ReadAllText("PublicPrivate.xml");
            byte[] encrypted, decrypted;
            using (var rsaPublicOnly = new RSACryptoServiceProvider())
            {
                rsaPublicOnly.FromXmlString(publicKeyOnly);
                encrypted = rsaPublicOnly.Encrypt(data, true);
                // 下面的这解密就会报错,因为需要私钥解密
                // decrypted = rsaPublicOnly.Decrypt (encrypted, true);
            }
            using (var rsaPublicPrivate = new RSACryptoServiceProvider())
            {
                // With the private key we can successfully decrypt:
                rsaPublicPrivate.FromXmlString(publicPrivate);
                decrypted = rsaPublicPrivate.Decrypt(encrypted, true);
                string ss = Encoding.UTF8.GetString(decrypted);
                WriteLog(ss);
            }
        }

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用python的cryptography库来实现AES加密解密,以及使用zeropadding进行填充。 首先,你需要安装cryptography库。可以使用以下命令来安装: ``` pip install cryptography ``` 接下来,你可以使用以下代码示例来实现AES加密解密,并使用zeropadding进行填充: ```python from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes from cryptography.hazmat.primitives import padding from cryptography.hazmat.backends import default_backend import os def aes_encrypt(plain_text, key): # 生成一个随机的初始化向量 iv = os.urandom(16) # 使用zeropadding进行填充 padder = padding.ZeroPadding(128).padder() padded_data = padder.update(plain_text) + padder.finalize() # 创建AES加密器对象 cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=default_backend()) encryptor = cipher.encryptor() # 执行加密操作 cipher_text = encryptor.update(padded_data) + encryptor.finalize() return iv + cipher_text def aes_decrypt(cipher_text, key): # 提取初始化向量和密文 iv = cipher_text[:16] cipher_text = cipher_text[16:] # 创建AES解密器对象 cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=default_backend()) decryptor = cipher.decryptor() # 执行解密操作 padded_data = decryptor.update(cipher_text) + decryptor.finalize() # 使用zeropadding进行取消填充 unpadder = padding.ZeroPadding(128).unpadder() plain_text = unpadder.update(padded_data) + unpadder.finalize() return plain_text # 测试代码 key = os.urandom(32) # 生成一个随机密钥 plain_text = b"Hello, AES!" # 原始文本 # 加密 cipher_text = aes_encrypt(plain_text, key) print("Cipher Text: ", cipher_text) # 解密 decrypted_text = aes_decrypt(cipher_text, key) print("Decrypted Text: ", decrypted_text) ``` 这段代码中,我们使用了AES加密算法和CBC模式来加密解密数据。同时,我们使用了zeropadding进行填充,确保数据长度满足加密算法的要求。需要注意的是,密钥的长度必须符合AES算法的要求(16、24、或32字节)。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值