当前位置: 首页 > 编程语言 > C#

如何在没有IV的情况下使用AES128加解密?分享

时间:2023-04-10 23:30:29 C#

不带IV的AES128如何加解密?我目前需要一种使用AES-128对称加密在C#中加密字符串和解密字节数组的方法。我找不到怎么做的方法,但也许我遗漏了一些东西。ImportNamespace以上是C#学习教程:如何使用AES128加解密不用IV?如果分享的内容对你有用,需要了解更多C#学习教程,希望大家多多关注——usingSystem;使用System.IO;使用系统文本;使用System.Security.Cryptography;staticvoidMain(string[]args){stringvalue="@arifansari300<3>";字符串encryptedValue=EncryptDecrypt.Encrypt(值);字符串decryptedValue=EncryptDecrypt.Decrypt(encryptedValue);}publicstaticstringEncrypt(stringclearText){string=EncryptionKey"MAKV2SPBNI99212";byte[]clearBytes=Encoding.Unicode.GetBytes(clearText);使用(Aesencryptor=Aes.Create()){Rfc2898DeriveBytespdb=newRfc2898DeriveBytes(加密密钥,新字节[]{0x49,0x6e16,0x20,0x4d,0x65,0x64,0x76,0x65,0x64,0x65,0x76});encryptor.Key=pdb.GetBytes(32);encryptor.IV=pdb.GetBytes(16);使用(MemoryStreamms=newMemoryStream()){使用(CryptoStreamcs=newCryptoStream(ms,encryptor.CreateEncryptor(),CryptoStreamMode.Write)){cs.Write(clearBytes,0,clearBytes.Length);cs.关闭();}clearText=Convert.ToBase64String(ms.ToArray());}}返回明文;}publicstaticstringDecrypt(stringcipherText){stringEncryptionKey="MAKV2SPBNI99212";byte[]cipherBytes=Convert.FromBase64String(cipherText);使用(Aes加密器=Aes.Create()){Rfc2898DeriveBytespdb=newRfc2898DeriveBytes(加密密钥,新字节[]{0x49、0x76、0x61、0d、6e、0x4、0x64、0x76、0x65、0x64、0x65、0x76});encryptor.Key=pdb.GetBytes(32);encryptor.IV=pdb.GetBytes(16);使用(MemoryStreamms=newMemoryStream()){使用(CryptoStreamcs=newCryptoStream(ms,encryptor.CreateDecryptor(),CryptoStreamMode.Write)){cs.Write(cipherBytes,0,cipherBytes.Length);cs.关闭();}cipherText=Encoding.Unicode.GetString(ms.ToArray());}}返回密文;}本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如有转载请注明出处: