请问我如何 return 解码字节而不是来自 CryptoStream 的文本
Please how can i return decoded bytes instead of text from a CryptoStream
请问我如何 return 解码字节而不是以下代码段中的文本:
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
我的以下尝试失败了
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
终于成功了
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
请问我如何 return 解码字节而不是以下代码段中的文本:
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
我的以下尝试失败了
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
终于成功了
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