AdBrite

Your Ad Here

Thursday, October 28, 2010

[C#] Port Scanner

/* Port Scanner
*  http://ProjectGhostt.com
*  FreckleS
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;

namespace Port_Scanner
{
    class Program
    {
        static void Main(string[] args)
        {
            // Scan specific port
            int port = 135;
            if (TestPort(port) == true)
            {
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Port {0} is OPEN!", port);
            }
            else
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Port {0} is CLOSED!", port);
            }

            // Scan a range of ports
            for (int i = 1; i < 1000; i++) // for (int i = startPort; i < stopPort; increment port)
            {
                if (TestPort(i) == true)
                {
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine("Port {0} is OPEN!", i);
                }
                else
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Port {0} is CLOSED!", i);
                }
            }
        }

        static bool TestPort(int port)
        {
            try
            {
                TcpClient tcp = new TcpClient("127.0.0.1", port);
                if (tcp.Connected == true)
                {
                    return true;
                }
            }
            catch
            {
                return false;
            }
            return false;
        }
    }
}

Wednesday, October 27, 2010

Stream.Read Method

When overridden in a derived class, reads a sequence of bytes from the 
current stream and advances the position within the stream by the number of bytes read.
 
public abstract int Read(
byte[] buffer,
int offset,
int count
)

Stream.Write Method

When overridden in a derived class, writes a sequence of bytes to the 
current stream and advances the current position within this stream by
the number of bytes written. 

const int size = 4096;
byte[] bytes = new byte[4096];
int numBytes;
while((numBytes = input.Read(bytes, 0, size)) > 0)
output.Write(bytes, 0, numBytes);

Saturday, October 23, 2010

A Beginner's Simple Encryption Tutorial / Example

// Adam Boulfoul - "A Beginner's Simple Encryption Tutorial / Example" chebby_shabby@hotmail.com
/////////////////////////////////////////////////////////////////////////////////////////////////
// This is not an "uncrackable" type of encryption but once you can understand the basics of
// this type of file encryption you CAN make better ones than me. This is just a quick example
// of basic encryption. Highly commented to help beginners.
/////////////////////////////////////////////////////////////////////////////////////////////////
// Before we start:
// What this basic program does is simply getting a byte from a file then adding 25 to it
// (ofcouse you can change it to whatever you like).
/////////////////////////////////////////////////////////////////////////////////////////////////
// If you have any questions, my e-mail is above
/////////////////////////////////////////////////////////////////////////////////////////////////

#include // We need this for input and output to the user
#include // This is the header to read or write files
#include // We only need this to delete a file using remove()
// As mentioned abode, we simply add 25 to a byte, change this to whatever you like.
#define ENCRYPTION_FORMULA (int) Byte + 25
// The decryption formula is the opposite to encryption formula, every "+" is a "-"
#define DECRYPTION_FORMULA (int) Byte - 25

///////////////////////////////////////////////////////////////////////////////////////////
// TIP: Everytime you see ENCRYPTION_FORMULA ot DECRYPTION_FORMULA mensioned, highlight it
// then press on definition and it will bring you up here! (Only on Visual C++)
///////////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////
// Our main function, put this in your program, FILENAME is the file to encrypt
// and NEW_FILENAME is the new location of the encrypted file
/////////////////////////////////////////////////////////////////////////////////

int Encrypt (char * FILENAME, char * NEW_FILENAME)
{
ifstream inFile; // This is the file that we're going to encrypt and read
ofstream outFile; // Once we encrypt the file, this is it's new location

char Byte; // This is the FILENAME's byte, we'll add 25 to this later

///////////////////////////////////////////////////////////////
// Before we continue:
// ios:: in - Used for reading a file
// ios::out - Used for writing a file
// ios::binary - Used for reading or writing binary files
///////////////////////////////////////////////////////////////

inFile.open(FILENAME, ios::in | ios::binary); // We read this file in binary mode
outFile.open(NEW_FILENAME, ios::out | ios::binary); // And we write the file in binary mode

// eof() stands for End Of File, so while we are still reading the file, continue
while(!inFile.eof())
{
// Remember we need to change a byte so we add 25 to it
char NewByte;

//////////////////////////////////////////////////
// NOTE: Only use the .put() and .get() in loops
// because it only reads 1 Byte at a time!
//////////////////////////////////////////////////

// Out old byte is recieved from the file
Byte = inFile.get();

// If the file that we are reading has an error, turn 0
if (inFile.fail())
return 0;
//Remember our Encryption Formula above?
//Our new byte is recieved from it, see above
NewByte = ENCRYPTION_FORMULA;
// This simple puts the new byte, into the new file!
outFile.put(NewByte);
}

// We have to be neat so we close both the file that we're reading
// and the file that we're writing
inFile.close(); // (File to read)
outFile.close(); // (File to write)

return 1; // Success!
}

///////////////////////////////////////////////////////////////////////////////
// What's the point of encrypting a file if you can't decrypt it?
// If you're wondering why this is not commented it's because I've already
// explained everything above, notice that everything is the same except for
// two lines!
///////////////////////////////////////////////////////////////////////////////

int Decrypt (char * FILENAME, char * NEW_FILENAME)
{
ifstream inFile;
ofstream outFile;

char Byte;

inFile.open(FILENAME, ios::in | ios::binary);
outFile.open(NEW_FILENAME, ios::out | ios::binary);

while(!inFile.eof())
{
char NewByte;

Byte = inFile.get();

if (inFile.fail())
return 0;
/////////////////////////////////////////////////////
NewByte = DECRYPTION_FORMULA; // New Line! We just change the ENCRYPTION_FORMULA
// to DECRYPTION_FORMULA, see above
/////////////////////////////////////////////////////
outFile.put(NewByte);
}

inFile.close();
outFile.close();

return 1;
}

///////////////////////////////////////////////////////////////////////
// WARNING: If you choose to decrypt a file that it already decrypted
// then the file would be encrypted!
///////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////////
// To use the functions in your program (doesn't have to be a DOS program),
// copy the 2 functions above, this is just an example, in the example I'm
// encrypting
///////////////////////////////////////////////////////////////////////////////////

int main()
{
///////////////////////////////////////////////////////////////////
// The functions can be used in not only DOS programs
// Since this is an example, I'll show you how to use them
///////////////////////////////////////////////////////////////////

char EncFile[200]; // This is the string for the file to be encrypted
char NewEncFile[200]; // This is the new location of the encrypted file

char DecFile[200]; // This is the string for the file to be decrypted
char NewDecFile[200]; // This is the new location of the decrypted file

int Choice; //The user's choice variable (1 = Encrypt 2 = Decrypt)

// In case you didn't know, all these "cout" just display a message
// Cin tells the user to input something
cout << "NOTE: You must encrypt the file with the same file extension!"<
cout << "Enter 1 to Encrypt / 2 to Decrypt"<
cin >> Choice; // In this case, our input is the user's choice

switch(Choice)
{
case 1: // Did the user choose to encrypt a file?
cout << "Enter the current Filename: ";
cin >> EncFile; // The user's input for the current file to be encrypted

cout << "Enter the new Filename: ";
cin >> NewEncFile; //The user's input for the new location of the encrypted file

Encrypt(EncFile, NewEncFile); /*********** ENCRYPT FUNCTION ************/
break;

case 2: // Or did the user choose to decrypt a file?
cout << "Enter the current Filename: ";
cin >> DecFile; //Already explained but with the decrypted file this time!

cout << "Enter the new Filename: ";
cin >> NewDecFile;

Decrypt(DecFile, NewDecFile); /*********** DECRYPT FUNCTION ************/
break;
}


return 0; //Exit!
}

//////////////////////////////////////////////////////////////////////////////////
// That's it! Looks so long yet it's so simple and easy to understand!
//////////////////////////////////////////////////////////////////////////////////
// So what we basicly done is open a file and add 25 to every byte of the file!
// Very simple!
//////////////////////////////////////////////////////////////////////////////////
// Any questions to chebby_shabby@hotmail.com
// If this tutorial is successful then my next tutorial is
// "A Beginner's Simple Compression Tutorial / Example
//////////////////////////////////////////////////////////////////////////////////
// by Adam Boulfoul
//////////////////////////////////////////////////////////////////////////////////

Tuesday, October 19, 2010

How to encrypt and decrypt a file by using Visual C#

using System;
using System.IO;
using System.Security;
using System.Security.Cryptography;
using System.Runtime.InteropServices;
using System.Text;

namespace CSEncryptDecrypt
{
class Class1
{
// Call this function to remove the key from memory after use for security
[System.Runtime.InteropServices.DllImport("KERNEL32.DLL", EntryPoint="RtlZeroMemory")]
public static extern bool ZeroMemory(IntPtr Destination, int Length);

// Function to Generate a 64 bits Key.
static string GenerateKey()
{
// Create an instance of Symetric Algorithm. Key and IV is generated automatically.
DESCryptoServiceProvider desCrypto =(DESCryptoServiceProvider)DESCryptoServiceProvider.Create();

// Use the Automatically generated key for Encryption.
return ASCIIEncoding.ASCII.GetString(desCrypto.Key);
}

static void EncryptFile(string sInputFilename,
string sOutputFilename,
string sKey)
{
FileStream fsInput = new FileStream(sInputFilename,
FileMode.Open,
FileAccess.Read);

FileStream fsEncrypted = new FileStream(sOutputFilename,
FileMode.Create,
FileAccess.Write);
DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
ICryptoTransform desencrypt = DES.CreateEncryptor();
CryptoStream cryptostream = new CryptoStream(fsEncrypted,
desencrypt,
CryptoStreamMode.Write);

byte[] bytearrayinput = new byte[fsInput.Length];
fsInput.Read(bytearrayinput, 0, bytearrayinput.Length);
cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length);
cryptostream.Close();
fsInput.Close();
fsEncrypted.Close();
}

static void DecryptFile(string sInputFilename,
string sOutputFilename,
string sKey)
{
DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
//A 64 bit key and IV is required for this provider.
//Set secret key For DES algorithm.
DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
//Set initialization vector.
DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);

//Create a file stream to read the encrypted file back.
FileStream fsread = new FileStream(sInputFilename,
FileMode.Open,
FileAccess.Read);
//Create a DES decryptor from the DES instance.
ICryptoTransform desdecrypt = DES.CreateDecryptor();
//Create crypto stream set to read and do a
//DES decryption transform on incoming bytes.
CryptoStream cryptostreamDecr = new CryptoStream(fsread,
desdecrypt,
CryptoStreamMode.Read);
//Print the contents of the decrypted file.
StreamWriter fsDecrypted = new StreamWriter(sOutputFilename);
fsDecrypted.Write(new StreamReader(cryptostreamDecr).ReadToEnd());
fsDecrypted.Flush();
fsDecrypted.Close();
}

static void Main()
{
// Must be 64 bits, 8 bytes.
// Distribute this key to the user who will decrypt this file.
string sSecretKey;

// Get the Key for the file to Encrypt.
sSecretKey = GenerateKey();

// For additional security Pin the key.
GCHandle gch = GCHandle.Alloc( sSecretKey,GCHandleType.Pinned );

// Encrypt the file.
EncryptFile(@"C:\MyData.txt",
@"C:\Encrypted.txt",
sSecretKey);

// Decrypt the file.
DecryptFile(@"C:\Encrypted.txt",
@"C:\Decrypted.txt",
sSecretKey);

// Remove the Key from memory.
ZeroMemory(gch.AddrOfPinnedObject(), sSecretKey.Length * 2);
gch.Free();
}
}
}

Monday, October 18, 2010

VIsual Studio 2010 Ultimate

Microsoft Visual Studio 2010 Ultimate is the comprehensive suite of application lifecycle management tools for teams to ensure quality results, from design to deployment. Whether you're creating new solutions or enhancing existing applications, Visual Studio 2010 Ultimate lets you bring your vision to life targeting an increasing number of platforms and technologies—including cloud and parallel computing.

Friday, October 15, 2010

Converting a bitmap to a byte array

// Bitmap bytes have to be created via a direct memory copy of the bitmap
private byte[] BmpToBytes_MemStream (Bitmap bmp)
{
MemoryStream ms = new MemoryStream();
// Save to memory using the Jpeg format
bmp.Save (ms, ImageFormat.Jpeg);

// read to end
byte[] bmpBytes = ms.GetBuffer();
bmp.Dispose();
ms.Close();

return bmpBytes;
}
 //Bitmap bytes have to be created using Image.Save()
private Image BytesToImg (byte[] bmpBytes)
{
MemoryStream ms = new MemoryStream(bmpBytes);
Image img = Image.FromStream(ms);
// Do NOT close the stream!

return img;
}

BidVertiser

pocket cents

PocketCents Local Online Advertising