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

如何使用OpenSSL.NetC#包装器使用AES加密字符串?Share

时间:2023-04-10 22:23:11 C#

如何使用OpenSSL.NetC#包装器使用AES加密字符串?我正在尝试将一些加密数据从我的SharePoint站点发送到我公司的PeopleSoft站点。PeopleSoft人员坚持认为我必须使用OpenSSL库进行加密。我从SourceForge下载并安装了OpenSSL.Net项目。出于我的目的,我需要简单地使用AES加密字符串。我知道如何使用System.Security.Cryptography库执行此操作,但我无法将其转换为OpenSSL.Net端。非常令人沮丧,因为我可以在智能感知中看到我认为我需要的一切!有没有人有使用OpenSSL.Net包装器使用AES执行字符串加密/解密的示例?谢谢!-Notch这是适合我的示例。我使用复制粘贴简化了它,但没关系。由于兼容JS库,我使用的是文本密码,但是OpenSSL本身支持直接使用byte[]Key和IV,所以看你怎么用。为了将二进制数据转换为字符串,只需使用Encoding.UTF8.GetBytes()和Encoding.UTF8.GetString()来回转换即可。以上是C#学习教程:HowtousetheOpenSSL.NetC#wrappertoencryptastringwithAES?如果分享的内容对你有用,需要了解更多C#学习教程,希望大家多多关注——publicByte[]Encrypt(Byte[]data,Stringpassword){//随机8即可盐的字节varsalt=newByte[]{1,2,3,4,5,6,7,8};using(varcc=newCipherContext(Cipher.AES_256_CBC)){//从字符串passwordbyte[]passwordBytes=Encoding.UTF8.GetBytes(password)构造密钥和初始向量;字节[]四;byte[]key=cc.BytesToKey(MessageDigest.MD5,salt,passwordBytes,1,outiv);varmemoryStream=newMemoryStream();//通过非托管包装器执行加密varaesData=cc.Crypt(data,key,iv,true);//附加盐,使最终数据看起来像Salted___SALT|RESTOFTHEDATAmemoryStream.Write(Encoding.UTF8.GetBytes("Salted__"),0,8);memoryStream.Write(盐,0,8);memoryStream.Write(aesData,0,aesData.Length);返回memoryStream.ToArray();}}publicByte[]Decrypt(Stringpassword,Byte[]encryptedData){byte[]salt=null;//提取salt如果出现if(encryptedData.Length>16){if(Encoding.UTF8.GetString(encryptedData).StartsWith("Salted__")){salt=newByte[8];Buffer.BlockCopy(encryptedData,8,salt,0,8);}}//从原始数组中删除盐intaesDataLength=encryptedData.Length-16;byte[]aesData=newbyte[aesDataLength];Buffer.BlockCopy(encryptedData,16,aesData,0,aesDataLength);using(varcc=newCipherContext(Cipher.AES_256_CBC)){//从字符串密码和盐byte[]passwordBytes=Encoding.UTF8.GetBytes(password)构造密钥和初始向量;字节[]四;byte[]key=cc.BytesToKey(MessageDigest.MD5,salt,passwordBytes,1,outiv);//解密返回cc.Decrypt(aesData,key,iv,0);}}本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处: