System.Security.Cryptography Namespace in .NET - C# Corner [PDF]

Apr 2, 2010 - The System.Security.Cryptography namespace contains support for the most common symmetric (DES, 3DES, RC2,

3 downloads 39 Views 358KB Size

Recommend Stories


c# .net software developer
If you want to go quickly, go alone. If you want to go far, go together. African proverb

C# .Net Developer
This being human is a guest house. Every morning is a new arrival. A joy, a depression, a meanness,

Net-C Voice Panels
Why complain about yesterday, when you can make a better tomorrow by making the most of today? Anon

Programming ArcObjects with C#.NET
No matter how you feel: Get Up, Dress Up, Show Up, and Never Give Up! Anonymous

C# nedir, .Net Framework nedir?
Don’t grieve. Anything you lose comes round in another form. Rumi

Team Leader – C#, .Net, ASP.Net
Be who you needed when you were younger. Anonymous

Perfil-Desarrollador-NET-C#-Contratacion
Nothing in nature is unbeautiful. Alfred, Lord Tennyson

in Calvins Corner Dirksland
Learn to light a candle in the darkest moments of someone’s life. Be the light that helps others see; i

Living in Corner Brook
What we think, what we become. Buddha

[PDF] Download C# 6.0 and the .NET 4.6 Framework Ebook
Don't ruin a good today by thinking about a bad yesterday. Let it go. Anonymous

Idea Transcript


In Focus C# Corner PWA - Latest Technology Web Developers Master Today



System.Security.Cryptography Namespace in .NET Puran Mehra

0

Apr 02 2010

0



Article

49.5k

SystemSecurityCryptographyNamespace.zip Easily Add PDF Word & Excel Function to Your .NET Apps This article has been excerpted from book "The Complete Visual C# Programmer's Guide" from the Authors of C# Corner. The System.Security.Cryptography namespace contains support for the most common symmetric (DES, 3DES, RC2, Rijndael), asymmetric (RSA, DSA), and hash (MD5, SHA-1, SHA-256, SHA- 384, SHA-512) cryptography algorithms. It also includes a helpful class to encrypt and decrypt streams. You can use this class, called CryptoStream, in combination with other stream classes or you can use several CryptoStream class objects together. With a little effort, you can feed the output of one CryptoStream object into another CryptoStream object. Table 22.6 lists the classes and interfaces that are part of the System.Security.Cryptography namespace of the .NET Framework base class library. These classes and interfaces define the abstract object model for encryption algorithms within the .NET Framework. New algorithms may be added to the .NET Framework by subclassing and/or implementing a portion of these classes and interfaces.

Table 22.6: Classes and Interfaces in the System.Security.Cryptography Namespace Listing 22.35 illustrates the use of DES and CryptoStream classes to encipher and decipher a file. Listing 22.35: Cryptostream1.cs, CryptoStream with DES // NOTE: Before you execute the program, first // create a file named c:\myfile.txt with text "myname is bozo..." in it. // After you execute the code, two files will be created on the c: drive. // The c:\ciphered.txt file will include weird ciphered characters like // "¼ş_Eó_ğNª_4û Õ?8Ò¤¤_"5" // The c:\enciphered.txt file will be identical to c:\myfile.txt. using System; using System.IO; using System.Security; using System.Security.Cryptography; using System.Text; class CryptoDESSample { public static void Main(string[] args) { FileStream fsInput = new FileStream(@"c:\myfile.txt", FileMode.Open, FileAccess.Read); FileStream fsCiphered = new FileStream(@"c:\ciphered.txt", FileMode.Create, FileAccess.Write); DESCryptoServiceProvider ourDESProvider = new DESCryptoServiceProvider(); // our 8 byte DES secret key is 12345678 ourDESProvider.Key = ASCIIEncoding.ASCII.GetBytes("12345678"); ourDESProvider.IV = ASCIIEncoding.ASCII.GetBytes("12345678"); ICryptoTransform des1 = ourDESProvider.CreateEncryptor(); ICryptoTransform des2 = ourDESProvider.CreateDecryptor(); CryptoStream cryptostream1 = new CryptoStream(fsCiphered, des1, CryptoStreamMode.Write); byte[] bytearrayinput = new byte[fsInput.Length]; fsInput.Read(bytearrayinput, 0, bytearrayinput.Length); cryptostream1.Write(bytearrayinput, 0, bytearrayinput.Length); cryptostream1.Close(); fsInput.Close(); fsCiphered.Close(); FileStream fsread = new FileStream(@"c:\ciphered.txt", FileMode.Open, FileAccess.Read); CryptoStream cryptostream2 = new CryptoStream(fsread, des2, CryptoStreamMode.Read); StreamWriter fsEnciphered = new StreamWriter(@"c:\enciphered.txt"); fsEnciphered.Write(new StreamReader(cryptostream2).ReadToEnd()); fsEnciphered.Flush(); fsEnciphered.Close(); } } The code in Listing 22.36 uses the SHA1 algorithm to compute the hash value of a given string. Listing 22.36: Hash1.cs, Hashing a String Using SHA-1 Algorithm using System; using System.IO; using System.Security; using System.Security.Cryptography; public class Class1 { public static void Main(string[] args) { String str1 = "MCBinc"; Char[] char1a = str1.ToCharArray(); Byte[] byte1a = new Byte[char1a.Length]; for (int i = 0; i < byte1a.Length; i++) byte1a[i] = (Byte)char1a[i]; // create hash value from str1 using SHA1 instance // returned by CryptoConfig byte[] hash1 = ((HashAlgorithm) CryptoConfig.CreateFromName("SHA1")).ComputeHash(byte1a); // or you can use directly created instance of the SHA1 class // byte[] hash1 = (new SHA1CryptoServiceProvider()).ComputeHash(byte1a ); Console.WriteLine(str1 + @" hashed Value is "); Console.ReadLine(); } } The code in the listing writes the output in Figure 22.9 to the console.

Figure 22.9: Output Generated from Listing 22.36 Conclusion Hope this article would have helped you in understanding System.Security.Cryptography Namespace in .NET. See other articles on the website on .NET and C#. The Complete Visual C# Programmer's Guide covers most of the major components that make up C# and the .net environment. The book is geared toward the intermediate programmer, but contains enough material to satisfy the advanced developer.

.NET

0

System.Security.Cryptography



Namespace

C#

0

Type your comment here and press Enter Key (Minimum 18 characters)

Comment Using

File APIs for .NET Aspose are the market leader of .NET APIs for file business formats – natively work with DOCX, XLSX, PPT, PDF, MSG, MPP, images formats and many more!

TRENDING UP

01

Special Class Of C# Series - Part One - Partial Class

02

Audit Made Easy Without Audit Log - Part One

03

Top 10 Web Application Security Risks In 2017

04

Creating Angular 5 App With Visual Studio 2017 - An Easy Way

05

C# 8.0 - Experimenting With Non-Nullable Reference Type Using Visual Studio 2017

06

Bitcoin Explained In Simple Terms

07

How To Build Secure Websites

08

Implement Validations In Angular 5 App

09

List Of Users With Roles In ASP.NET MVC Identity

10

Building An ASP.NET Core Application With Web API And Code First Development View All

Philadelphia New York London Delhi

and millions of developer friends worldwide.

Sign Up

Enter your email address











Learn ASP.NET MVC

Home

Learn ASP.NET Core

Events

Learn Python

Consultants

Learn JavaScript

Jobs

Learn Xamarin

Career Advice

Learn Oracle

Stories

More...

Partners



About Us Contact Us Privacy Policy Terms Media Kit Sitemap Report a Bug FAQ ©2017 C# Corner. All contents are copyright of their authors.

Smile Life

When life gives you a hundred reasons to cry, show life that you have a thousand reasons to smile

Get in touch

© Copyright 2015 - 2024 PDFFOX.COM - All rights reserved.