请问如何从CryptoStream返回解码字节而不是文本 [英] Please how can i return decoded bytes instead of text from a CryptoStream

查看:197
本文介绍了请问如何从CryptoStream返回解码字节而不是文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请问如何在以下代码段中返回解码的字节而不是文本:

 公共共享函数decryptAsText(key As Byte(),密文As Byte(),as Aste())As String 
Dim dec As String = Nothing
尝试
使用rj = New Security.Cryptography.Rijndael使用{.Key = key,.IV = iv,.Mode = Security.Cryptography.CipherMode.CBC,.Padding = PaddingMode.PKCS7}
使用ms =新建IO.MemoryStream(ciphertext)
使用cs =新的安全性。 Cryptography.CryptoStream(ms,rj.CreateDecryptor(key,iv),Security.Cryptography.CryptoStreamMode.Read)
使用sr =新建IO.StreamReader(cs)
dec = sr.ReadToEnd
结束使用
结束使用
结束使用
结束使用
Catch e作为异常
结束尝试

返回dec
结束函数

我的尝试以下失败

 公共共享函数decryptAsBytes(密钥As Byte(),密文as Byte(),iv As Byte())As Byte()
尝试
使用rj = New Security.Cryptography.RijndaelManaged with {.Key = key,.IV = iv,.Mode = Security.Cryptography.CipherMode.CBC,.Padding = PaddingMode.PKCS7 }
使用ms = New IO.MemoryStream(ciphertext)
使用cs = New Security.Cryptography.CryptoStream(ms,rj.CreateDecryptor(key,iv),Security.Cryptography.CryptoStreamMode.Read)
Dim l As Integer = CInt(cs.Length)
Dim b(l - 1)As Byte
cs.Read(b,0,l)
返回b
结束使用
结束使用
结束使用
捕获e作为异常
结束尝试
返回{}
结束功能


解决方案尝试
使用rj = New Security.Cryptography.RijndaelManaged with {.Key = key,.IV = iv,.Mode = Security.Cryptography.CipherMode。 CBC,.Padding = PaddingMode.PKCS7}
使用ms =新的IO.MemoryStream(ciphertext)
使用cs =新的Security.Cryptography.CryptoStream(ms,rj.CreateDecryptor(key,iv),安全性。 Cryptography.CryptoStreamMode.Read)
使用ds =新建IO.MemoryStream()
cs.CopyTo(ds)
返回ds.ToArray
结束使用
结束使用
结束使用
结束使用
捕获e作为异常
结束尝试
返回{}
结束函数
/ pre>

Please how can i return the decoded bytes instead of text in the following snippet:

Public Shared Function decryptAsText(key As Byte(), ciphertext As Byte(), iv As Byte()) As String
    Dim dec As String = Nothing
    Try
        Using rj = New Security.Cryptography.RijndaelManaged With {.Key = key, .IV = iv, .Mode = Security.Cryptography.CipherMode.CBC, .Padding = PaddingMode.PKCS7}
            Using ms = New IO.MemoryStream(ciphertext)
                Using cs = New Security.Cryptography.CryptoStream(ms, rj.CreateDecryptor(key, iv), Security.Cryptography.CryptoStreamMode.Read)
                    Using sr = New IO.StreamReader(cs)
                        dec = sr.ReadToEnd
                    End Using
                End Using
            End Using
        End Using
    Catch e As Exception
    End Try

    Return dec
End Function

My attempt below fails

 Public Shared Function decryptAsBytes(key As Byte(), ciphertext As Byte(), iv As Byte()) As Byte()
        Try
            Using rj = New Security.Cryptography.RijndaelManaged With {.Key = key, .IV = iv, .Mode = Security.Cryptography.CipherMode.CBC, .Padding = PaddingMode.PKCS7}
                Using ms = New IO.MemoryStream(ciphertext)
                    Using cs = New Security.Cryptography.CryptoStream(ms, rj.CreateDecryptor(key, iv), Security.Cryptography.CryptoStreamMode.Read)
                        Dim l As Integer = CInt(cs.Length)
                        Dim b(l - 1) As Byte
                        cs.Read(b, 0, l)
                        Return b
                    End Using
                End Using
            End Using
        Catch e As Exception
        End Try
        Return {}
    End Function

解决方案

Finally got this to work

Public Shared Function decryptAsBytes(key As Byte(), ciphertext As Byte(), iv As Byte()) As Byte()
    Try
        Using rj = New Security.Cryptography.RijndaelManaged With {.Key = key, .IV = iv, .Mode = Security.Cryptography.CipherMode.CBC, .Padding = PaddingMode.PKCS7}
            Using ms = New IO.MemoryStream(ciphertext)
                Using cs = New Security.Cryptography.CryptoStream(ms, rj.CreateDecryptor(key, iv), Security.Cryptography.CryptoStreamMode.Read)
                    Using ds = New IO.MemoryStream()
                        cs.CopyTo(ds)
                        Return ds.ToArray
                    End Using
                End Using
            End Using
        End Using
    Catch e As Exception
    End Try
    Return {}
End Function

这篇关于请问如何从CryptoStream返回解码字节而不是文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆