Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,158,308 members, 7,836,346 topics. Date: Wednesday, 22 May 2024 at 05:48 AM

Am Confused - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Am Confused (735 Views)

Am Confused About Java And C++ / What To Do When Your Confused And Coding Just Doesn't Feel Like Fun Anymore! / Confused About Learning C# (2) (3) (4)

(1) (Reply) (Go Down)

Am Confused by pjosh1(m): 6:13pm On Jul 17, 2013
Is cyber security under computer programming.
Re: Am Confused by csharpjava(m): 6:40pm On Jul 17, 2013
Yes, it is called hmacsha256 Algorithm or Cryptography. Below is an example from Microsoft:

http://msdn.microsoft.com/en-us/library/system.security.cryptography.hmacsha256.aspx

"An HMAC can be used to determine whether a message sent over an insecure channel has been tampered with, provided that the sender and receiver share a secret key. The sender computes the hash value for the original data and sends both the original data and hash value as a single message. The receiver recalculates the hash value on the received message and checks that the computed HMAC matches the transmitted HMAC."

using System;
using System.IO;
using System.Security.Cryptography;

public class HMACSHA256example
{

public static void Main(string[] Fileargs)
{
string dataFile;
string signedFile;
//If no file names are specified, create them.
if (Fileargs.Length < 2)
{
dataFile = @"text.txt";
signedFile = "signedFile.enc";

if (!File.Exists(dataFile))
{
// Create a file to write to.
using (StreamWriter sw = File.CreateText(dataFile))
{
sw.WriteLine("Here is a message to sign"wink;
}
}

}
else
{
dataFile = Fileargs[0];
signedFile = Fileargs[1];
}
try
{
// Create a random key using a random number generator. This would be the
// secret key shared by sender and receiver.
byte[] secretkey = new Byte[64];
//RNGCryptoServiceProvider is an implementation of a random number generator.
using (RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider())
{
// The array is now filled with cryptographically strong random bytes.
rng.GetBytes(secretkey);

// Use the secret key to sign the message file.
SignFile(secretkey, dataFile, signedFile);

// Verify the signed file
VerifyFile(secretkey, signedFile);
}
}
catch (IOException e)
{
Console.WriteLine("Error: File not found", e);
}

} //end main
// Computes a keyed hash for a source file and creates a target file with the keyed hash
// prepended to the contents of the source file.
public static void SignFile(byte[] key, String sourceFile, String destFile)
{
// Initialize the keyed hash object.
using (HMACSHA256 hmac = new HMACSHA256(key))
{
using (FileStream inStream = new FileStream(sourceFile, FileMode.Open))
{
using (FileStream outStream = new FileStream(destFile, FileMode.Create))
{
// Compute the hash of the input file.
byte[] hashValue = hmac.ComputeHash(inStream);
// Reset inStream to the beginning of the file.
inStream.Position = 0;
// Write the computed hash value to the output file.
outStream.Write(hashValue, 0, hashValue.Length);
// Copy the contents of the sourceFile to the destFile.
int bytesRead;
// read 1K at a time
byte[] buffer = new byte[1024];
do
{
// Read from the wrapping CryptoStream.
bytesRead = inStream.Read(buffer, 0, 1024);
outStream.Write(buffer, 0, bytesRead);
} while (bytesRead > 0);
}
}
}
return;
} // end SignFile


// Compares the key in the source file with a new key created for the data portion of the file. If the keys
// compare the data has not been tampered with.
public static bool VerifyFile(byte[] key, String sourceFile)
{
bool err = false;
// Initialize the keyed hash object.
using (HMACSHA256 hmac = new HMACSHA256(key))
{
// Create an array to hold the keyed hash value read from the file.
byte[] storedHash = new byte[hmac.HashSize / 8];
// Create a FileStream for the source file.
using (FileStream inStream = new FileStream(sourceFile, FileMode.Open))
{
// Read in the storedHash.
inStream.Read(storedHash, 0, storedHash.Length);
// Compute the hash of the remaining contents of the file.
// The stream is properly positioned at the beginning of the content,
// immediately after the stored hash value.
byte[] computedHash = hmac.ComputeHash(inStream);
// compare the computed hash with the stored value

for (int i = 0; i < storedHash.Length; i++)
{
if (computedHash[i] != storedHash[i])
{
err = true;
}
}
}
}
if (err)
{
Console.WriteLine("Hash values differ! Signed file has been tampered with!"wink;
return false;
}
else
{
Console.WriteLine("Hash values agree -- no tampering occurred."wink;
return true;
}

} //end VerifyFile

} //end class
Re: Am Confused by pjosh1(m): 9:35am On Jul 18, 2013
^^
I dont understand what u writting
Re: Am Confused by csharpjava(m): 9:51am On Jul 18, 2013
p.josh:
^^
I dont understand what u writting

In that case what were you trying to find out?
Re: Am Confused by pjosh1(m): 10:01am On Jul 18, 2013
Is cyber security related to computer science(programming)
Re: Am Confused by csharpjava(m): 10:28am On Jul 18, 2013
p.josh:
Is cyber security related to computer science(programming)

Yes it is and it comes under Cryptography which has to do with encryption.

1 Like

(1) (Reply)

Game Using Tangible Using User Interface / RE: Concerning Our Catastrophic Loss Of Your Valuable Data / Nairaland Its Time To Step-up Your Ad Placement Application

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 18
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.