Click here to Skip to main content
15,915,975 members
Home / Discussions / Visual Basic
   

Visual Basic

 
GeneralVB6 Web Browser Control Pin
Sarfraz Ahmed Chandio11-Feb-08 22:37
Sarfraz Ahmed Chandio11-Feb-08 22:37 
QuestionHow to run an .exe automatically after the set-up is installed..? Pin
Balagurunathan S11-Feb-08 22:11
Balagurunathan S11-Feb-08 22:11 
AnswerRe: How to run an .exe automatically after the set-up is installed..? Pin
Christian Graus11-Feb-08 22:20
protectorChristian Graus11-Feb-08 22:20 
GeneralRe: How to run an .exe automatically after the set-up is installed..? Pin
Balagurunathan S11-Feb-08 22:24
Balagurunathan S11-Feb-08 22:24 
GeneralRe: How to run an .exe automatically after the set-up is installed..? Pin
Christian Graus11-Feb-08 22:31
protectorChristian Graus11-Feb-08 22:31 
GeneralRe: How to run an .exe automatically after the set-up is installed..? Pin
Balagurunathan S11-Feb-08 22:36
Balagurunathan S11-Feb-08 22:36 
GeneralRe: How to run an .exe automatically after the set-up is installed..? Pin
Christian Graus11-Feb-08 22:51
protectorChristian Graus11-Feb-08 22:51 
QuestionWhat is the wrong in my code. Pin
sudhakar7911-Feb-08 21:34
sudhakar7911-Feb-08 21:34 
I am new to .Net. and I am sorry for my bad english.

Below is my code to encrypting and decrypting the image by using DES algorithm. but I am getting an exception please any body can help me in this issue.Please very urgent

exception like "Parameter is not valid."



Imports System
Imports System.IO
Imports System.Security.Cryptography
Imports System.Text
Imports System.Collections.Generic
Imports System.Drawing


Public Class Encrypt
Dim passPhrase As String = "water"
Dim saltValue As String = "EncryptionAndDecryption"
Dim hashAlgorithm As String = "SHA1"
Dim passwordIterations As Integer = 1
Dim initVector As String = "@1B2c3D4e5F6g7H8"
Dim keySize As Integer = 256
Public Function Encrypt(ByVal imagepath As String) As String
Dim initVectorBytes As Byte() = Encoding.ASCII.GetBytes(initVector)
Dim saltValueBytes As Byte() = Encoding.ASCII.GetBytes(saltValue)

Dim imageIn As System.Drawing.Image
imageIn = Drawing.Image.FromFile(imagepath)
Dim ms As New MemoryStream()
imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
Dim plainTextBytes As Byte() = ms.ToArray()
'Dim fs As FileStream = New FileStream(imagepath, FileMode.Open, FileAccess.Read)
'Dim plainTextBytes As Byte() = New Byte(fs.Length) {}
Dim password As PasswordDeriveBytes = New PasswordDeriveBytes(passPhrase, saltValueBytes, hashAlgorithm, passwordIterations)
Dim keyBytes As Byte() = password.GetBytes(keySize / 8)
Dim symmetricKey As RijndaelManaged = New RijndaelManaged()
symmetricKey.Mode = CipherMode.CBC
Dim encryptor As ICryptoTransform = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes)
Dim memoryStream As MemoryStream = New MemoryStream()
Dim cryptoStream As CryptoStream = New CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write)
cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length)
cryptoStream.FlushFinalBlock()

Dim cipherTextBytes As Byte() = memoryStream.ToArray()
memoryStream.Close()
cryptoStream.Close()
Dim cipherText As String = Convert.ToBase64String(cipherTextBytes)

Return cipherText

End Function
Public Function Decrypt(ByVal cipherText As String) As String
Dim initVectorBytes As Byte() = Encoding.ASCII.GetBytes(initVector)
Dim saltValueBytes As Byte() = Encoding.ASCII.GetBytes(saltValue)
Dim cipherTextBytes As Byte() = Convert.FromBase64String(cipherText)
Dim password As PasswordDeriveBytes = New PasswordDeriveBytes(passPhrase, saltValueBytes, hashAlgorithm, passwordIterations)
Dim keyBytes As Byte() = password.GetBytes(keySize / 8)
Dim symmetricKey As RijndaelManaged = New RijndaelManaged()
symmetricKey.Mode = CipherMode.CBC
Dim decryptor As ICryptoTransform = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes)
Dim memoryStream As MemoryStream = New MemoryStream(cipherTextBytes)
Dim cryptoStream As CryptoStream = New CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read)

Dim plainTextBytes As Byte() = New Byte(cipherTextBytes.Length) {}
Dim decryptedByteCount As Integer = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length)
'memoryStream.Close()
'cryptoStream.Close()
Dim plaintext As String = Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount)


Dim count() As Byte = New Byte(decryptedByteCount) {}
Dim img As System.Drawing.Image
Dim stream As New MemoryStream(count, 0, count.Length)

stream.Write(count, 0, count.Length)
' img = New Bitmap(stream)
'img.Save("C:\test.bmp")
'Dim myimage As New System.Drawing.Bitmap(stream)

img = Image.FromStream(stream, True) // getting exception here like "Parameter is not valid" 'myimage.Save("C:\test.bmp")
img.Save("C:\test.bmp")
memoryStream.Close()
cryptoStream.Close()
'Dim bitImage As Bitmap = New Bitmap(System.Drawing.Image.FromStream(stream))

'bitImage.Save("C:\test.bmp")

' Dim memStream As MemoryStream = New MemoryStream(2388139)
' Dim bitImage As Bitmap = New Bitmap(System.Drawing.Image.FromStream(memStream))

' bitImage.Save( ("C:\test.bmp")
Return plaintext
End Function


End Class

in module I have this code.

Imports System
Imports System.IO
Imports System.Security
Imports System.Security.Cryptography
Imports System.Runtime.InteropServices
Imports System.Text



Module Module1

' Call this function to remove the key from memory after it is used for security.
<DllImport("kernel32.dll")> _
Public Sub ZeroMemory(ByVal addr As IntPtr, ByVal size As Integer)
End Sub

' Function to generate a 64-bit key.
Function GenerateKey() As String
' Create an instance of a symmetric algorithm. The key and the IV are generated automatically.
Dim desCrypto As DESCryptoServiceProvider = DESCryptoServiceProvider.Create()

' Use the automatically generated key for encryption.
Return ASCIIEncoding.ASCII.GetString(desCrypto.Key)

End Function

Sub EncryptFile(ByVal sInputFilename As String, _
ByVal sOutputFilename As String, _
ByVal sKey As String)

Dim fsInput As New FileStream(sInputFilename, _
FileMode.Open, FileAccess.Read)
Dim fsEncrypted As New FileStream(sOutputFilename, _
FileMode.Create, FileAccess.Write)

Dim DES As New DESCryptoServiceProvider()

'Set secret key for DES algorithm.
'A 64-bit key and an IV are required for this provider.
DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey)

'Set the initialization vector.
DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey)

'Create the DES encryptor from this instance.
Dim desencrypt As ICryptoTransform = DES.CreateEncryptor()
'Create the crypto stream that transforms the file stream by using DES encryption.
Dim cryptostream As New CryptoStream(fsEncrypted, _
desencrypt, _
CryptoStreamMode.Write)

'Read the file text to the byte array.
Dim bytearrayinput(fsInput.Length - 1) As Byte
fsInput.Read(bytearrayinput, 0, bytearrayinput.Length)
'Write out the DES encrypted file.
cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length)
cryptostream.Close()
End Sub

Sub DecryptFile(ByVal sInputFilename As String, _
ByVal sOutputFilename As String, _
ByVal sKey As String)

Dim DES As New DESCryptoServiceProvider()
'A 64-bit key and an IV are required for this provider.
'Set the secret key for the DES algorithm.
Console.WriteLine(sKey)
DES.Key() = ASCIIEncoding.ASCII.GetBytes(sKey)
'Set the initialization vector.
DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey)

'Create the file stream to read the encrypted file back.
'Dim fsread As New FileStream(sInputFilename, FileMode.Open, FileAccess.Read)
Dim fsread As New FileStream(sInputFilename, FileMode.Open, FileAccess.Read)



'Create the DES decryptor from the DES instance.
Dim desdecrypt As ICryptoTransform = DES.CreateDecryptor()
'Create the crypto stream set to read and to do a DES decryption transform on incoming bytes.
Dim cryptostreamDecr As New CryptoStream(fsread, desdecrypt, CryptoStreamMode.Read)
'Print out the contents of the decrypted file.
Dim fsDecrypted As New StreamWriter(sOutputFilename)
fsDecrypted.Write(New StreamReader(cryptostreamDecr).ReadToEnd)
fsDecrypted.Flush()
fsDecrypted.Close()
End Sub

Public Sub Main()


Dim obj As Encrypt = New Encrypt()
Dim image As Image
'image = Drawing.Image.FromFile("C:\Documents and Settings\lakshmi\Desktop\lakshmi\008 (2).JPG")


Dim ar As String = obj.Encrypt("C:\Documents and Settings\sudhakar\My Documents\My Pictures\last5minutesofexam.bmp")
Dim str As String = obj.Decrypt(ar)
'Must be 64 bits, 8 bytes.
Dim sSecretKey As String

' Get the key for the file to encrypt.
' You can distribute this key to the user who will decrypt the file.
sSecretKey = GenerateKey()
'sSecretKey = "@fffe7e95"
' For additional security, pin the key.
Dim gch As GCHandle = GCHandle.Alloc(sSecretKey, GCHandleType.Pinned)


' Encrypt the file.
'EncryptFile("D:\Inbox path laxmi.txt", _
' "D:\Encrypted.txt", _
' sSecretKey)

'' Decrypt the file.
'DecryptFile("D:\Encrypted.txt", _
' "D:\Decrypted.txt", _
' sSecretKey)

' Remove the key from memory.
ZeroMemory(gch.AddrOfPinnedObject(), sSecretKey.Length * 2)
gch.Free()
End Sub

End Module

Thanks and Regards,
sudhakar venati,
Mob: 0 984 55 989 71.

AnswerRe: What is the wrong in my code. Pin
Christian Graus11-Feb-08 22:10
protectorChristian Graus11-Feb-08 22:10 
Generali am using Split Container in windows application. It work properly. Pin
Piyush Vardhan Singh11-Feb-08 20:46
Piyush Vardhan Singh11-Feb-08 20:46 
GeneralRe: i am using Split Container in windows application. It work properly. Pin
Christian Graus11-Feb-08 22:11
protectorChristian Graus11-Feb-08 22:11 
Generalwhich component is use for access excel workshee [modified] Pin
sandip khairnar11-Feb-08 20:20
sandip khairnar11-Feb-08 20:20 
GeneralRe: which component is use for access excel workshee Pin
John_Adams12-Feb-08 0:14
John_Adams12-Feb-08 0:14 
GeneralTo retrieve data from diffrent textbox to put in one field Pin
zaimah11-Feb-08 18:41
zaimah11-Feb-08 18:41 
GeneralRe: To retrieve data from diffrent textbox to put in one field Pin
Christian Graus11-Feb-08 18:50
protectorChristian Graus11-Feb-08 18:50 
GeneralRe: To retrieve data from diffrent textbox to put in one field Pin
zaimah11-Feb-08 20:45
zaimah11-Feb-08 20:45 
GeneralRe: To retrieve data from diffrent textbox to put in one field Pin
zaimah15-Feb-08 15:10
zaimah15-Feb-08 15:10 
GeneralWebbrowser control need help Pin
King of Kingz11-Feb-08 18:36
King of Kingz11-Feb-08 18:36 
GeneralRe: Webbrowser control need help Pin
Christian Graus11-Feb-08 18:51
protectorChristian Graus11-Feb-08 18:51 
Generalmaximize window application Pin
nitin_ion11-Feb-08 18:18
nitin_ion11-Feb-08 18:18 
GeneralRe: maximize window application Pin
Christian Graus11-Feb-08 18:52
protectorChristian Graus11-Feb-08 18:52 
GeneralRe: maximize window application Pin
nitin_ion11-Feb-08 19:48
nitin_ion11-Feb-08 19:48 
GeneralRe: maximize window application Pin
Ajay.k_Singh11-Feb-08 20:41
Ajay.k_Singh11-Feb-08 20:41 
QuestionHow can I click the link to navigate to the link. Pin
SekharOne11-Feb-08 18:00
SekharOne11-Feb-08 18:00 
QuestionCompare colors Pin
nishkarsh_k11-Feb-08 17:19
nishkarsh_k11-Feb-08 17:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.