gpt4 book ai didi

C#:Base64编码

转载 作者:行者123 更新时间:2023-12-03 19:22:23 26 4
gpt4 key购买 nike

谁能告诉我这段代码哪里出错了?此代码是用 C#.NET 编写的。我需要编写一个算法,使用 C#.NET 使用 base64 格式对字符串进行编码,然后使用 PHP 使用 base64_decode() 进行解码。请看下面的片段:

System.Security.Cryptography.RijndaelManaged rijndaelCipher = new System.Security.Cryptography.RijndaelManaged();
rijndaelCipher.Mode = System.Security.Cryptography.CipherMode.CBC;
rijndaelCipher.Padding = System.Security.Cryptography.PaddingMode.Zeros;
rijndaelCipher.KeySize = 256;
rijndaelCipher.BlockSize = 128;

byte[] pwdBytes = System.Text.Encoding.UTF8.GetBytes(_key);
byte[] keyBytes = new byte[16];

int len = pwdBytes.Length;
if (len > keyBytes.Length) len = keyBytes.Length;

System.Array.Copy(pwdBytes, keyBytes, len);

rijndaelCipher.Key = keyBytes;
rijndaelCipher.IV = keyBytes;

System.Security.Cryptography.ICryptoTransform transform = rijndaelCipher.CreateEncryptor();

byte[] plainText = Encoding.UTF8.GetBytes(unencryptedString);
byte[] cipherBytes = transform.TransformFinalBlock(plainText, 0, plainText.Length);

return Convert.ToBase64String(cipherBytes);

最佳答案

我认为您的代码示例正在进行“加密”,而您想要“编码”。要在 C# 中使用 Based64 编码字符串,它应该如下所示:

 static public string EncodeTo64(string toEncode)
{
byte[] toEncodeAsBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(toEncode);
string returnValue = System.Convert.ToBase64String(toEncodeAsBytes);
return returnValue;
}

PHP 应如下所示:

 <?php
$str = 'VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw==';
echo base64_decode($str);
?>

关于C#:Base64编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8066384/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com