Мой Kbyte.Ru
Рассылка Kbyte.Ru
Группы на Kbyte.Ru
Партнеры Kbyte.Ru
Реклама
Сделано руками
Сделано руками
> Исходные коды - BUMM ® -

Visual Basic .NET - Криптография, защита данных

Все примеры / Безопасность / Криптография, защита данных

Как получить зашифрованный текст средствами .NET?

Автор: BUMM ® | добавлено: 06.03.2010, 14:22 | просмотров: 3150 (1+) | комментариев: 0 | рейтинг: *x10
Простой пример показывает, как можно получить зашифрованный текст (не массив байт) средствами Framework .NET.

Код

Public Function Encrypt(ByVal plainText As String) As String 
    'portare fuori dalla funzione 
    Dim passPhrase As String = "p@ssPhra$e"    ' puo essere qualsiasi stringa 
    Dim saltValue As String = "bla-bla$2007"  ' puo essere qualsiasi stringa 
    Dim hashAlgorithm As String = "SHA1"       ' puo anche essere "MD5" 
    Dim passwordIterations As Integer = 2          ' puo essere qualsiasi numero 
    Dim initVector As String = "@12BCD34E5F6G78H" ' deve essere 16 bytes 
    Dim keySize As Integer = 256        ' puo anche essere 192 or 128 
    'portare fuori dalla funzione 
    Dim initVectorBytes As Byte() = Encoding.ASCII.GetBytes(initVector) 
    Dim saltValueBytes As Byte() = Encoding.ASCII.GetBytes(saltValue) 
    Dim plainTextBytes As Byte() = Encoding.UTF8.GetBytes(plainText) 
    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 

    'portare fuori dalla funzione 
    Dim passPhrase As String = "p@ssPhra$e"     ' can be any string 
    Dim saltValue As String = "bla-bla$2007" ' can be any string 
    Dim hashAlgorithm As String = "SHA1"       ' can be "MD5" 
    Dim passwordIterations As Integer = 2         ' can be any number 
    Dim initVector As String = "@12BCD34E5F6G78H" ' must be 16 bytes 
    Dim keySize As Integer = 256        ' can be 192 or 128 
    'fino qui 
    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 
    memoryStream = New MemoryStream(cipherTextBytes) 
    Dim cryptoStream As CryptoStream = New CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read) 
    Dim plainTextBytes As Byte() 
    ReDim plainTextBytes(cipherTextBytes.Length) 
    Dim decryptedByteCount As Integer 
    decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length) 
    memoryStream.Close() 
    cryptoStream.Close() 
    Dim plainText As String 
    plainText = Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount) 
    Decrypt = plainText 
End Function
Об авторе

BUMM ®

Нет информации об авторе...
BUMM ®
Последние комментарии (всего: 0)

Добавлять комментарии могут только зарегистрированные пользователи сайта.
Если у Вас уже есть учетная запись на Kbyte.Ru, пройдите процедуру авторизации OpenID.
Если Вы еще не зарегистрированы на Kbyte.Ru - зарегистрируйтесь.


Нет комментариев...

Авторизация
 
OpenID
Зарегистрируйся и получи 10% скидку на добавление своего сайта в каталоги! Подробнее »
Поиск по сайту
Реклама
Счетчики