This project has moved and is read-only. For the latest updates, please go here.

Playing a decrypted mp3 file from the clipboard

Feb 25, 2016 at 3:37 AM
Hello!
I have this project in VB that in my intention should play a decrypted mp3 file from the clipboard with AxWindowsMediaPlayer1. I tried to find this information in internet but nothing... I don't want to think that it is impossible!
The Form needs 3 Buttons, 1 Label, 1 Windows Media Player... Of course you should change the mp3 file address to test.
Here's the code:

Imports System.Security.Cryptography
Imports System.IO
Imports System.Text

Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim rij As New RijndaelManaged()
    rij.KeySize = 256
    rij.BlockSize = 256
    rij.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 pwd As Byte() = New Byte(31) {}
    UTF8Encoding.UTF8.GetBytes("123abc").CopyTo(pwd, 0)
    Dim cs As New CryptoStream(File.OpenRead("C:\Users\User\Desktop\aa.mp3"),
  rij.CreateEncryptor(pwd, rij.IV), CryptoStreamMode.Read)
    Dim s As Stream = File.OpenWrite("C:\Users\User\Desktop\bb.mp3")
    cs.CopyTo(s)
    s.Flush()
    s.Close()
    cs.Close()
    Label1.Text = "Crypted!"
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    Dim rij As New RijndaelManaged()
    rij.KeySize = 256
    rij.BlockSize = 256
    rij.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 pwd As Byte() = New Byte(31) {}
    UTF8Encoding.UTF8.GetBytes("123abc").CopyTo(pwd, 0)
    Dim mp3Bytes() As Byte
    Using cs As New CryptoStream(File.OpenRead("C:\Users\User\Desktop\bb.mp3"), rij.CreateDecryptor(pwd, rij.IV), CryptoStreamMode.Read)
        Using ms As New MemoryStream
            cs.CopyTo(ms)
            mp3Bytes = ms.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 btsFromClipboard() As Byte = {}
        Using cbstrm As Stream = Clipboard.GetAudioStream
            ReDim btsFromClipboard(CInt(cbstrm.Length) - 1)
            cbstrm.Read(btsFromClipboard, 0, CInt(cbstrm.Length))
        End Using

        'Now I would like to play the mp3 audio file directly from the clipboard

        AxWindowsMediaPlayer1.Ctlcontrols.play() ' ???? I don't know how to go on

    End If

End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    End
End Sub
End Class
Mar 26, 2016 at 11:56 AM
Well none of that code uses NAudio. You could try putting the decrypted audio into a MemoryStream and then passing that into Mp3FileReader and playing it with WaveOutEvent.