C#:Base64编码 [英] C#: Base64 encoding
本文介绍了C#:Base64编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
任何人都可以,请让我知道我在做这个代码的错误?此代码是用C#.NET。我需要写一个算法编码使用C#.NET使用Base64格式的字符串,然后使用PHP与BASE64_DECODE解码()。请参阅下面的SNIPPIT:
System.Security.Cryptography.RijndaelManaged rijndaelCipher =新System.Security.Cryptography.RijndaelManaged();
rijndaelCipher.Mode = System.Security.Cryptography.CipherMode.CBC;
rijndaelCipher.Padding = System.Security.Cryptography.PaddingMode.Zeros;
rijndaelCipher.KeySize = 256;
rijndaelCipher.BlockSize = 128;
字节[] = pwdBytes System.Text.Encoding.UTF8.GetBytes(_key);
字节[] = keyBytes新的字节[16];
INT LEN = pwdBytes.Length;
如果(LEN> keyBytes.Length)LEN = keyBytes.Length;
System.Array.Copy(pwdBytes,keyBytes,LEN);
rijndaelCipher.Key = keyBytes;
rijndaelCipher.IV = keyBytes;
System.Security.Cryptography.ICryptoTransform变换= rijndaelCipher.CreateEncryptor();
字节[] =明文Encoding.UTF8.GetBytes(unencryptedString);
字节[] = cipherBytes transform.TransformFinalBlock(明文,0,plainText.Length);
返回Convert.ToBase64String(cipherBytes);
解决方案
我觉得你的代码示例是做加密并且希望编码。
在C#与Based64编码字符串,它应该是这样的:
的静态公共字符串EncodeTo64(字符串toEncode )
{
字节[] = toEncodeAsBytes System.Text.ASCIIEncoding.ASCII.GetBytes(toEncode);
串的returnValue = System.Convert.ToBase64String(toEncodeAsBytes);
回报率的returnValue;
}
和PHP的应该是这样的:
< PHP
$海峡='VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw ==';
回声BASE64_DECODE($海峡);
>?;
Can anyone please let me know where I made a mistake in this code? This code is written in C#.NET. I need to write an algorithm for encoding a string using base64 format using C#.NET, and then decoded with base64_decode() using PHP. Please see the snippit below:
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);
解决方案
I think your code sample is doing "encryption", and you want "encoding". For encoding a string with Based64 in C#, it should look like this:
static public string EncodeTo64(string toEncode)
{
byte[] toEncodeAsBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(toEncode);
string returnValue = System.Convert.ToBase64String(toEncodeAsBytes);
return returnValue;
}
And the PHP should look like this:
<?php
$str = 'VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw==';
echo base64_decode($str);
?>
这篇关于C#:Base64编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文