任何人都可以讓我知道我在這段代碼中犯了什麼錯誤嗎?此代碼是用C#.NET編寫的。我需要使用C#.NET編寫一個使用base64格式編碼字符串的算法,然後使用PHP使用base64_decode()進行解碼。請參閱以下的這段:C#:Base64編碼
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);
您有什麼問題?你不能base64解碼和解密在PHP中,或什麼?你有什麼錯誤嗎? –
是的,當我試圖使用PHP中提供的base64_decode()函數解密它時,我正在接收一些垃圾字符。 –
垃圾字符可能是**加密**字符串。爲什麼你首先要加密?請參閱下面的@Mike Duke Hall的答案。 –