FromBase64String的写法

博客提出字符串长度需为4的偶数倍,并给出了Base64字符串转换的代码,同时包含错误处理。当遇到空字符串或长度不符合要求时,会输出相应错误信息。

要求字符串长度必须是4的偶数倍,最好带上错误处理

                Try
                    Me.data = System.Convert.FromBase64String(buffer.Substring(start, 8 * (buffer.Substring(start).Length / 8)))
                Catch exp As System.ArgumentNullException
                    System.Console.WriteLine("Base 64 string is null.")
                    Return
                Catch exp As System.FormatException
                    System.Console.WriteLine("Base 64 length is not 4 or is " + _
                                             "not an even multiple of 4.")
                    Return
                End Try

private void ChangeSaveDG_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e) { if (e.RowIndex & lt; 0 || e.ColumnIndex & lt; 0) return; var dgv = sender as DataGridView; var cell = dgv.Rows[e.RowIndex].Cells[e.ColumnIndex]; var cellValue = cell.Value; // 判断是否为图像格式的 Base64 字符串 if (cellValue is string base64Str & amp; & IsBase64ImageString(base64Str)) { try { Image image = Base64ToImage(base64Str); ShowImageInForm(image); } catch (Exception ex) { MessageBox.Show("无法加载图片:" + ex.Message); } } } // 判断是否是 Base64 字符串且是图像格式 private bool IsBase64ImageString(string s) { if (string.IsNullOrEmpty(s)) return false; try { byte[] data = Convert.FromBase64String(s); using (MemoryStream ms = new MemoryStream(data)) { Image img = Image.FromStream(ms); return true; } } catch { return false; } } // Base64 转 Image private Image Base64ToImage(string base64String) { byte[] imageBytes = Convert.FromBase64String(base64String); using (MemoryStream ms = new MemoryStream(imageBytes)) { return Image.FromStream(ms); } } // 显示图片的独立窗体 private void ShowImageInForm(Image image) { Form imageForm = new Form { Text = "图片查看器", StartPosition = FormStartPosition.CenterScreen, FormBorderStyle = FormBorderStyle.SizableToolWindow, MaximizeBox = false, MinimizeBox = false }; PictureBox pictureBox = new PictureBox { Image = new Bitmap(image), // 避免资源释放问题 SizeMode = PictureBoxSizeMode.Zoom, Dock = DockStyle.Fill }; imageForm.Controls.Add(pictureBox); imageForm.ClientSize = new Size(image.Width, image.Height); imageForm.ShowDialog(); } 请修改错误
09-04
下面的代码中,ide提示缺少hashAlgorithm using System; using System.Security.Cryptography; using System.Text; public class JasyptNetEncryptor { private const int Iterations = 1000; // 与Java默认迭代次数一致 private const int KeySize = 256; // AES-256 public string Encrypt(string plainText, string password) { // 生成随机盐值(16字节) byte[] salt = new byte[16]; using (var rng = RandomNumberGenerator.Create()) { rng.GetBytes(salt); } // 密钥派生 using var deriveBytes = new Rfc2898DeriveBytes( password: Encoding.UTF8.GetBytes(password), salt: salt, iterations: Iterations, hashAlgorithm: HashAlgorithmName.SHA512); byte[] key = deriveBytes.GetBytes(KeySize / 8); // AES加密 using var aes = Aes.Create(); aes.Key = key; aes.GenerateIV(); // 生成随机IV using var encryptor = aes.CreateEncryptor(); byte[] plainBytes = Encoding.UTF8.GetBytes(plainText); byte[] cipherBytes = encryptor.TransformFinalBlock(plainBytes, 0, plainBytes.Length); // 组合: 盐值(16) + IV(16) + 密文 byte[] resultBytes = new byte[salt.Length + aes.IV.Length + cipherBytes.Length]; Buffer.BlockCopy(salt, 0, resultBytes, 0, salt.Length); Buffer.BlockCopy(aes.IV, 0, resultBytes, salt.Length, aes.IV.Length); Buffer.BlockCopy(cipherBytes, 0, resultBytes, salt.Length + aes.IV.Length, cipherBytes.Length); return "ENC(" + Convert.ToBase64String(resultBytes) + ")"; } public string Decrypt(string encryptedText, string password) { // 移除ENC()包装 if (encryptedText.StartsWith("ENC(") && encryptedText.EndsWith(")")) encryptedText = encryptedText.Substring(4, encryptedText.Length - 5); byte[] fullBytes = Convert.FromBase64String(encryptedText); // 提取盐值(16字节)和IV(16字节) byte[] salt = new byte[16]; byte[] iv = new byte[16]; byte[] cipherBytes = new byte[fullBytes.Length - 32]; Buffer.BlockCopy(fullBytes, 0, salt, 0, 16); Buffer.BlockCopy(fullBytes, 16, iv, 0, 16); Buffer.BlockCopy(fullBytes, 32, cipherBytes, 0, cipherBytes.Length); // 密钥派生 using var deriveBytes = new Rfc2898DeriveBytes( password: Encoding.UTF8.GetBytes(password), salt: salt, iterations: Iterations, hashAlgorithm: HashAlgorithmName.SHA512); byte[] key = deriveBytes.GetBytes(KeySize / 8); // AES解密 using var aes = Aes.Create(); aes.Key = key; aes.IV = iv; using var decryptor = aes.CreateDecryptor(); byte[] plainBytes = decryptor.TransformFinalBlock(cipherBytes, 0, cipherBytes.Length); return Encoding.UTF8.GetString(plainBytes); } }
最新发布
11-18
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值