本文介绍了请帮我解决这个问题。填充无效,无法删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 使用系统; 使用 System.Collections.Generic; 使用 System.Linq; 使用 System.Web; 使用 System.Web.UI; 使用 System.Web.UI.WebControls; 使用 System.Security.Cryptography; 使用 System.Text; public partial class 关于:System.Web.UI.Page { string cipherData; byte [] cipherByte; byte [] textByte1; byte [] textByte2; byte []键; SymmetricAlgorithm desObj; 受保护 void Page_Load( object sender,EventArgs e) { desObj = Rijndael.Create(); } 受保护 void btnEncryption_Click( object sender,EventArgs e) { cipherData = txtST.Text; textByte1 = Encoding.ASCII.GetBytes(cipherData); key = Encoding.ASCII.GetBytes( 0123456789abcdef); desObj.Key = key; desObj.Mode = CipherMode.CBC; desObj.Padding = PaddingMode.PKCS7; System.IO.MemoryStream ms = new System.IO.MemoryStream(); CryptoStream cs = new CryptoStream(ms,desObj.CreateEncryptor(),CryptoStreamMode.Write); cs.Write(textByte1, 0 ,textByte1.Length); cs.Close(); cipherByte = ms.ToArray(); ms.Close(); txtEnc.Text = Encoding.ASCII.GetString(cipherByte); string data = txtEnc.Text; 会话[ encryptData] = data; } 受保护 void btnDecryption_Click( object span sender,EventArgs e) { byte [] takeData = Encoding.Default .GetBytes(( string )会话[ encryptData]); System.IO.MemoryStream ms1 = new System.IO.MemoryStream(takeData); CryptoStream cs1 = new CryptoStream(ms1,desObj.CreateDecryptor(),CryptoStreamMode.Read); cs1.Read(takeData, 0 ,takeData.Length); textByte2 = ms1.ToArray(); cs1.Close(); ms1.Close(); txtDec.Text = Encoding.ASCII.GetString(textByte2); } } 解决方案 世界上有什么让你认为八位的ASCII编码位二进制数据只是有效的文本,你可以反转并从中获取原始输入? 一个简单的测试就证明它不会起作用: byte [] inp = new byte [ 256 ]; for ( int i = 0 ; i < 256 ; i ++) { inp [i ] =( byte )i; } string s = Encoding.ASCII.GetString(inp); byte [] outp = Encoding.Default.GetBytes(s); if (inp.Length!= outp.Length)Console.WriteLine( 长度不同:{0}:{1},inp.Length,outp.Length); for ( int i = 0 ; i < (Math.Min(inp.Length,outp.Length)); i ++) { if (inp [i]!= outp [i])Console.WriteLine( Diff [{0}]:{1}!= {2},i,inp [i],outp [i]); } 结果: Diff [128]:128!= 63 Diff [129]:129!= 63 Diff [130]:130!= 63 Diff [131]:131!= 63 Diff [132]:132!= 63 Diff [133]:133!= 63 Diff [134]:134!= 63 Diff [135]:135!= 63 Diff [136]:136!= 63 差异[137]:137!= 63 差异[138]:138!= 63 差异[139]:139!= 63 差异[140]:140 != 63 差异[141]:141!= 63 差异[142]:142!= 63 差异[143]:143!= 63 差异[144] :144!= 63 差异[145]:145!= 63 差异[146]:146!= 63 差异[147]:147!= 63 差异[ 148]:148!= 63 差异[149]:149!= 63 差异[150]:150!= 63 差异[151]:151!= 63 Diff [152]:152!= 63 Diff [153]:153!= 63 Diff [154]:154!= 63 Diff [155]:155!= 63 Diff [156]:156!= 63 Diff [157]:157!= 63 Diff [158]:158!= 63 Diff [159]:159!= 63 Diff [160]:160!= 63 差异[161] :161!= 63 Diff [162]:162!= 63 Diff [163]:163!= 63 Diff [164]:164!= 63 Diff [ 165]:165!= 63 差异[166]:166!= 63 差异[167]:167!= 63 差异[168]:168!= 63 Diff [169]:169!= 63 Diff [170]:170!= 63 Diff [171]:171!= 63 Diff [172]:172!= 63 Diff [173]:173!= 63 Diff [174]:174!= 63 Diff [175]:175!= 63 Diff [176]:176!= 63 差异[177]:177!= 63 差异[178]:178!= 63 差异[179]:179!= 63 差异[180]:180! = 63 差异[181]:181!= 63 差异[182]:182!= 63 差异[183]:183!= 63 差异[184]: 184!= 63 差异[185]:185!= 63 差异[186]:186!= 63 差异[187]:187!= 63 差异[188 ]:188!= 63 差异[189]:189!= 63 差异[190]:190!= 63 差异[191]:191!= 63 差异[192]:192!= 63 差异[193]:193!= 63 差异[194]:194!= 63 差异[195]:195!= 63 Diff [196]:196!= 63 Di ff [197]:197!= 63 Diff [198]:198!= 63 Diff [199]:199!= 63 Diff [200]:200!= 63 Diff [201]:201!= 63 Diff [202]:202!= 63 Diff [203]:203!= 63 Diff [204]:204!= 63 差异[205]:205!= 63 差异[206]:206!= 63 差异[207]:207!= 63 差异[208]:208! = 63 差异[209]:209!= 63 差异[210]:210!= 63 差异[211]:211!= 63 差异[212]: 212!= 63 Diff [213]:213!= 63 Diff [214]:214!= 63 Diff [215]:215!= 63 Diff [216 ]:216!= 63 Diff [217]:217!= 63 Diff [218]:218!= 63 Diff [219]:219!= 63 Diff [220]:220!= 63 Diff [221]:221!= 63 Diff [222]:222!= 63 Diff [223]:223!= 63 Diff [224]:224!= 63 Diff [225]:225!= 63 Diff [226]:226!= 63 Diff [227]:227!= 63 Diff [228]:228!= 63 Diff [229]:229!= 63 Diff [230]:230!= 63 Diff [231]:231!= 63 Diff [232]:232!= 63 Diff [233]:233!= 63 Diff [234]:234!= 63 Diff [235]:235!= 63 Diff [236]:236! = 63 差异[237]:237!= 63 差异[238]:238!= 63 差异[239]:239!= 63 差异[240]: 240!= 63 Diff [241]:241!= 63 Diff [242]:242!= 63 Diff [243]:243!= 63 Diff [244 ]:244!= 63 差异[245]:245!= 63 差异[246]:246!= 63 差异[247]:247!= 63 差异[248]:248!= 63 Diff [249]:249!= 63 Diff [250]:250!= 63 Diff [251]:251!= 63 Diff [252]:252!= 63 Diff [253]:253!= 63 Diff [254]:254!= 63 Diff [255]:255!= 63 using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Security.Cryptography;using System.Text;public partial class About : System.Web.UI.Page{ string cipherData; byte[] cipherByte; byte[] textByte1; byte[] textByte2; byte[] key; SymmetricAlgorithm desObj; protected void Page_Load(object sender, EventArgs e) { desObj = Rijndael.Create(); } protected void btnEncryption_Click(object sender, EventArgs e) { cipherData = txtST.Text; textByte1 = Encoding.ASCII.GetBytes(cipherData); key = Encoding.ASCII.GetBytes("0123456789abcdef"); desObj.Key = key; desObj.Mode = CipherMode.CBC; desObj.Padding = PaddingMode.PKCS7; System.IO.MemoryStream ms = new System.IO.MemoryStream(); CryptoStream cs = new CryptoStream(ms, desObj.CreateEncryptor(), CryptoStreamMode.Write); cs.Write(textByte1, 0, textByte1.Length); cs.Close(); cipherByte = ms.ToArray(); ms.Close(); txtEnc.Text = Encoding.ASCII.GetString(cipherByte); string data = txtEnc.Text; Session["encryptData"] = data; } protected void btnDecryption_Click(object sender, EventArgs e) { byte[] takeData = Encoding.Default.GetBytes((string)Session["encryptData"]); System.IO.MemoryStream ms1 = new System.IO.MemoryStream(takeData); CryptoStream cs1 = new CryptoStream(ms1, desObj.CreateDecryptor(), CryptoStreamMode.Read); cs1.Read(takeData, 0, takeData.Length); textByte2 = ms1.ToArray(); cs1.Close(); ms1.Close(); txtDec.Text = Encoding.ASCII.GetString(textByte2); }} 解决方案 What in world makes you think that the ASCII encoding of eight bit binary data will be just valid text you can reverse and get your original input from?A simple test would have proved it wont work:byte[] inp = new byte[256];for (int i = 0; i < 256; i++) { inp[i] = (byte)i; }string s = Encoding.ASCII.GetString(inp);byte[] outp = Encoding.Default.GetBytes(s);if (inp.Length != outp.Length) Console.WriteLine("Different length: {0}:{1}", inp.Length, outp.Length);for (int i = 0; i < (Math.Min(inp.Length, outp.Length)); i++) { if (inp[i] != outp[i]) Console.WriteLine(" Diff [{0}] : {1} != {2}", i, inp[i], outp[i]); }Results:Diff [128] : 128 != 63Diff [129] : 129 != 63Diff [130] : 130 != 63Diff [131] : 131 != 63Diff [132] : 132 != 63Diff [133] : 133 != 63Diff [134] : 134 != 63Diff [135] : 135 != 63Diff [136] : 136 != 63Diff [137] : 137 != 63Diff [138] : 138 != 63Diff [139] : 139 != 63Diff [140] : 140 != 63Diff [141] : 141 != 63Diff [142] : 142 != 63Diff [143] : 143 != 63Diff [144] : 144 != 63Diff [145] : 145 != 63Diff [146] : 146 != 63Diff [147] : 147 != 63Diff [148] : 148 != 63Diff [149] : 149 != 63Diff [150] : 150 != 63Diff [151] : 151 != 63Diff [152] : 152 != 63Diff [153] : 153 != 63Diff [154] : 154 != 63Diff [155] : 155 != 63Diff [156] : 156 != 63Diff [157] : 157 != 63Diff [158] : 158 != 63Diff [159] : 159 != 63Diff [160] : 160 != 63Diff [161] : 161 != 63Diff [162] : 162 != 63Diff [163] : 163 != 63Diff [164] : 164 != 63Diff [165] : 165 != 63Diff [166] : 166 != 63Diff [167] : 167 != 63Diff [168] : 168 != 63Diff [169] : 169 != 63Diff [170] : 170 != 63Diff [171] : 171 != 63Diff [172] : 172 != 63Diff [173] : 173 != 63Diff [174] : 174 != 63Diff [175] : 175 != 63Diff [176] : 176 != 63Diff [177] : 177 != 63Diff [178] : 178 != 63Diff [179] : 179 != 63Diff [180] : 180 != 63Diff [181] : 181 != 63Diff [182] : 182 != 63Diff [183] : 183 != 63Diff [184] : 184 != 63Diff [185] : 185 != 63Diff [186] : 186 != 63Diff [187] : 187 != 63Diff [188] : 188 != 63Diff [189] : 189 != 63Diff [190] : 190 != 63Diff [191] : 191 != 63Diff [192] : 192 != 63Diff [193] : 193 != 63Diff [194] : 194 != 63Diff [195] : 195 != 63Diff [196] : 196 != 63Diff [197] : 197 != 63Diff [198] : 198 != 63Diff [199] : 199 != 63Diff [200] : 200 != 63Diff [201] : 201 != 63Diff [202] : 202 != 63Diff [203] : 203 != 63Diff [204] : 204 != 63Diff [205] : 205 != 63Diff [206] : 206 != 63Diff [207] : 207 != 63Diff [208] : 208 != 63Diff [209] : 209 != 63Diff [210] : 210 != 63Diff [211] : 211 != 63Diff [212] : 212 != 63Diff [213] : 213 != 63Diff [214] : 214 != 63Diff [215] : 215 != 63Diff [216] : 216 != 63Diff [217] : 217 != 63Diff [218] : 218 != 63Diff [219] : 219 != 63Diff [220] : 220 != 63Diff [221] : 221 != 63Diff [222] : 222 != 63Diff [223] : 223 != 63Diff [224] : 224 != 63Diff [225] : 225 != 63Diff [226] : 226 != 63Diff [227] : 227 != 63Diff [228] : 228 != 63Diff [229] : 229 != 63Diff [230] : 230 != 63Diff [231] : 231 != 63Diff [232] : 232 != 63Diff [233] : 233 != 63Diff [234] : 234 != 63Diff [235] : 235 != 63Diff [236] : 236 != 63Diff [237] : 237 != 63Diff [238] : 238 != 63Diff [239] : 239 != 63Diff [240] : 240 != 63Diff [241] : 241 != 63Diff [242] : 242 != 63Diff [243] : 243 != 63Diff [244] : 244 != 63Diff [245] : 245 != 63Diff [246] : 246 != 63Diff [247] : 247 != 63Diff [248] : 248 != 63Diff [249] : 249 != 63Diff [250] : 250 != 63Diff [251] : 251 != 63Diff [252] : 252 != 63Diff [253] : 253 != 63Diff [254] : 254 != 63Diff [255] : 255 != 63 这篇关于请帮我解决这个问题。填充无效,无法删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-28 22:27