I have this small project in VB NET that should play a mp3 file from the clipboard using AxWindowsMediaPlayer1. The code is working up to the last line, where I'd want to play the mp3 (just decrypted) bytes that are in memory instead of saving them into a file and then play the file.
Here's the code. The Form needs 3 Buttons, 1 Label, 1 Windows Media Player. You should choose one mp3 file of yours. Hope that somebody can help me!
Imports System.Security.Cryptography
Imports System.IO
Imports System.Text
Public Class Form1
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
End
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim rijndael As New RijndaelManaged()
rijndael.KeySize = 256
rijndael.BlockSize = 256
rijndael.IV = New [Byte]() {24, 23, 35, 83, 77, 35, 28, 34, 94, 25, 45, 2, 73, 26, 27, 78, 12, 23, 35, 83, 57, 35, 28, 34, 94, 25, 45, 22, 73, 26, 27, 78}
Dim password As Byte() = New Byte(31) {}
UTF8Encoding.UTF8.GetBytes("123abc").CopyTo(password, 0)
Dim file_to_read As New CryptoStream(File.OpenRead("C:\Users\User\Desktop\aa.mp3"),
rijndael.CreateEncryptor(password, rijndael.IV), CryptoStreamMode.Read)
Dim file_to_write As Stream = File.OpenWrite("C:\Users\User\Desktop\bb.mp3")
file_to_read.CopyTo(file_to_write)
file_to_write.Flush()
file_to_write.Close()
file_to_read.Close()
Label1.Text = "Crypted!"
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim rijndael As New RijndaelManaged()
rijndael.KeySize = 256
rijndael.BlockSize = 256
rijndael.IV = New [Byte]() {24, 23, 35, 83, 77, 35, 28, 34, 94, 25, 45, 2, 73, 26, 27, 78, 12, 23, 35, 83, 57, 35, 28, 34, 94, 25, 45, 22, 73, 26, 27, 78}
Dim password As Byte() = New Byte(31) {}
UTF8Encoding.UTF8.GetBytes("123abc").CopyTo(password, 0)
Dim mp3Bytes() As Byte
Using file_to_read As New CryptoStream(File.OpenRead("C:\Users\User\Desktop\bb.mp3"), rijndael.CreateDecryptor(password, rijndael.IV), CryptoStreamMode.Read)
Using memory_stream As New MemoryStream
file_to_read.CopyTo(memory_stream)
mp3Bytes = memory_stream.GetBuffer
End Using
End Using
Label1.Text = "Decrypted!"
Clipboard.Clear() 'clear the clipboard
Clipboard.SetAudio(mp3Bytes) 'set the mp3 audio file bytes to the clipboard
If Clipboard.ContainsAudio Then
Dim bytes_from_clipboard() As Byte = {}
Using clipboard_stream As Stream = Clipboard.GetAudioStream
ReDim bytes_from_clipboard(CInt(clipboard_stream.Length) - 1)
clipboard_stream.Read(bytes_from_clipboard, 0, CInt(clipboard_stream.Length))
End Using
'UP TO THIS POINT IT IS OKAY. FROM HERE THE FREELANCER'S TASK BEGINS
'I would like to play with Windows Media Player the mp3 file just decrypted into the clipboard.
AxWindowsMediaPlayer1.Ctlcontrols.play() ' ???? WHAT CAN I DO HERE? IS THERE ANY SOLUTION?
End If
End Sub
End Class