2017-02-07 8 views
0
Public Function Encrypt(clearText As String) As String 
    Dim EncryptionKey As String = "MAKV2SPBNI99212" 
    Dim clearBytes As Byte() = Encoding.Unicode.GetBytes(clearText) 
    Using encryptor As Aes = Aes.Create() 
     Dim pdb As New Rfc2898DeriveBytes(EncryptionKey, New Byte() {&H49, &H76, &H61, &H6E, &H20, &H4D, _ 
     &H65, &H64, &H76, &H65, &H64, &H65, &H76}) 
     encryptor.Key = pdb.GetBytes(32) 
     encryptor.IV = pdb.GetBytes(16) 
     Using ms As New MemoryStream() 
      Using cs As New CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write) 
       cs.Write(clearBytes, 0, clearBytes.Length) 
       cs.Close() 
      End Using 
      clearText = Convert.ToBase64String(ms.ToArray()) 
     End Using 
    End Using 
    Return clearText 
End Function 

Public Function Decrypt(cipherText As String) As String 
    Dim EncryptionKey As String = "MAKV2SPBNI99212" 
    cipherText = cipherText.Replace(" ", "+") 
    Dim cipherBytes As Byte() = Convert.FromBase64String(cipherText) 
    Using encryptor As Aes = Aes.Create() 
     Dim pdb As New Rfc2898DeriveBytes(EncryptionKey, New Byte() {&H49, &H76, &H61, &H6E, &H20, &H4D, _ 
     &H65, &H64, &H76, &H65, &H64, &H65, &H76}) 
     encryptor.Key = pdb.GetBytes(32) 
     encryptor.IV = pdb.GetBytes(16) 
     Using ms As New MemoryStream() 
      Using cs As New CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write) 
       cs.Write(cipherBytes, 0, cipherBytes.Length) 
       cs.Close() 
      End Using 
      cipherText = Encoding.Unicode.GetString(ms.ToArray()) 
     End Using 
    End Using 
    Return cipherText 
End Function 
+0

Ваш вопрос не ясен. Что ты пытаешься сделать? –

ответ

0

Если я правильно понимаю, вы должны просто нужно расшифровать значение существующего со старым EncryptionKey «MAKV2SPBNI99212», а затем повторно зашифровать с новым EncryptionKey ,

decryptedText = Decrypt(encryptedText, "MAKV2SPBNI99212") 
encryptedText = Encrypt(decryptedText, "MY_NEW_KEY") 

Обновление шифровать и дешифровать функции, чтобы позволить прохождение EncryptionKey в качестве параметра, например:

Public Function Encrypt(ByVal clearText As String, ByVal encryptionKey As String) As String 
    Dim clearBytes As Byte() = Encoding.Unicode.GetBytes(clearText) 
    Using encryptor As Aes = Aes.Create() 
     Dim pdb As New Rfc2898DeriveBytes(encryptionKey, New Byte() {&H49, &H76, &H61, &H6E, &H20, &H4D, _ 
     &H65, &H64, &H76, &H65, &H64, &H65, &H76}) 
     encryptor.Key = pdb.GetBytes(32) 
     encryptor.IV = pdb.GetBytes(16) 
     Using ms As New MemoryStream() 
      Using cs As New CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write) 
       cs.Write(clearBytes, 0, clearBytes.Length) 
       cs.Close() 
      End Using 
      clearText = Convert.ToBase64String(ms.ToArray()) 
     End Using 
    End Using 
    Return clearText 
End Function 

Public Function Decrypt(ByVal cipherText As String, ByVal encryptionKey As String) As String 
    cipherText = cipherText.Replace(" ", "+") 
    Dim cipherBytes As Byte() = Convert.FromBase64String(cipherText) 
    Using encryptor As Aes = Aes.Create() 
     Dim pdb As New Rfc2898DeriveBytes(encryptionKey, New Byte() {&H49, &H76, &H61, &H6E, &H20, &H4D, _ 
     &H65, &H64, &H76, &H65, &H64, &H65, &H76}) 
     encryptor.Key = pdb.GetBytes(32) 
     encryptor.IV = pdb.GetBytes(16) 
     Using ms As New MemoryStream() 
      Using cs As New CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write) 
       cs.Write(cipherBytes, 0, cipherBytes.Length) 
       cs.Close() 
      End Using 
      cipherText = Encoding.Unicode.GetString(ms.ToArray()) 
     End Using 
    End Using 
    Return cipherText 
End Function 
+0

Спасибо, ребята, это очень помогает :) – lemuel