Windows Azure Cloud Storage ermöglicht es Ihnen bereits ab 0,10€ pro GB/Monat die Vorteile der Cloud zu nutzen.
Willkommen bei dotnet-snippets.de! Snippet hinzufügen Login Registrieren
Snippets in der Datenbank: 1550 | Anzahl registrierter User: 1840 | Besucher online: 175
Hauptmenü
Home
Top Ten
Zufälliger Snippet
FAQs
.NET Community
dotnet-forum.de
dotnet-kicks.de
Social

RSS Feeds
Rss Alle Snippets
Rss C#
Rss VB.NET
Rss C++
Rss ASP.NET
Partner
Member of Microsoft Community Leader/Insider Program (CLIP)

Encrypt and Decrypt Strings


Autor: Jan Welker
Sprache: C#
Bewertung:
8.24 (9 votes)
Anzahl der Aufrufe: 28595
  
Kick it on dotnet-kicks.de  

Beschreibung:

This class encrypt and decrypt strings using the Rijndael algorithm .

Abgelegt unter: Rijndael, Cryptography, Security, Encrypt, Decrypt, cipher, Verschlüsselung.



C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
using System;
using System.IO;
using System.Security.Cryptography;

/// <summary>
/// encrypt and decrypt strings
/// </summary>
public class Encryption
{
    /// <summary>
    /// Encrypts the string.
    /// </summary>
    /// <param name="clearText">The clear text.</param>
    /// <param name="Key">The key.</param>
    /// <param name="IV">The IV.</param>
    /// <returns></returns>
    private static byte[] EncryptString(byte[] clearText, byte[] Key, byte[] IV)
    {
        MemoryStream ms = new MemoryStream();
        Rijndael alg = Rijndael.Create();
        alg.Key = Key;
        alg.IV = IV;
        CryptoStream cs = new CryptoStream(ms, alg.CreateEncryptor(), CryptoStreamMode.Write);
        cs.Write(clearText, 0, clearText.Length);
        cs.Close();
        byte[] encryptedData = ms.ToArray();
        return encryptedData;
    }

    /// <summary>
    /// Encrypts the string.
    /// </summary>
    /// <param name="clearText">The clear text.</param>
    /// <param name="Password">The password.</param>
    /// <returns></returns>
    public static string EncryptString(string clearText, string Password)
    {
        byte[] clearBytes = System.Text.Encoding.Unicode.GetBytes(clearText);
        PasswordDeriveBytes pdb = new PasswordDeriveBytes(Password, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
        byte[] encryptedData = EncryptString(clearBytes, pdb.GetBytes(32), pdb.GetBytes(16));
        return Convert.ToBase64String(encryptedData);
    }

    /// <summary>
    /// Decrypts the string.
    /// </summary>
    /// <param name="cipherData">The cipher data.</param>
    /// <param name="Key">The key.</param>
    /// <param name="IV">The IV.</param>
    /// <returns></returns>
    private static byte[] DecryptString(byte[] cipherData, byte[] Key, byte[] IV)
    {
        MemoryStream ms = new MemoryStream();
        Rijndael alg = Rijndael.Create();
        alg.Key = Key;
        alg.IV = IV;
        CryptoStream cs = new CryptoStream(ms, alg.CreateDecryptor(), CryptoStreamMode.Write);
        cs.Write(cipherData, 0, cipherData.Length);
        cs.Close();
        byte[] decryptedData = ms.ToArray();
        return decryptedData;
    }

    /// <summary>
    /// Decrypts the string.
    /// </summary>
    /// <param name="cipherText">The cipher text.</param>
    /// <param name="Password">The password.</param>
    /// <returns></returns>
    public static string DecryptString(string cipherText, string Password)
    {
        byte[] cipherBytes = Convert.FromBase64String(cipherText);
        PasswordDeriveBytes pdb = new PasswordDeriveBytes(Password, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
        byte[] decryptedData = DecryptString(cipherBytes, pdb.GetBytes(32), pdb.GetBytes(16));
        return System.Text.Encoding.Unicode.GetString(decryptedData);
    }
}
Sie haben Fragen zu diesem Snippet oder brauchen Hilfe bei der .NET Entwicklung?
Freundliche und kompetente Entwickler helfen Ihnen gern weiter im Forum für .NET Entwicklung.



Kommentare:
(Zum Schreiben von Kommentaren bitte anmelden.)

Rainbird schrieb am:  04.11.2006 20:43:26

Schade, dass das Snippet nicht Dokumentiert ist (Kein XML-Doku-Tags, keine Kommentare).
soxxing schrieb am:  28.01.2008 20:49:57

Danke, Vielen Dank
Genau sowas habe ich gesucht.
Leider keine Kommentare, naja man kann nich alles haben ;).
Jan Welker schrieb am:  28.01.2008 21:17:44

Ich habe XML Kommentare hinzugefügt.
thomas3577 schrieb am:  16.12.2009 18:01:27

Ich kenne mich mit dem Thema nicht so aus aber muss man für "cipherText" einfach irgend eine belibige Zeichenfolge wählen? Und muss bei "clearText" die gleiche Zeichenfolge stehen?


schlecht sehr gut
1 2 3 4 5 6 7 8 9 10
Nur angemeldete User können Snippets bewerten.