2014-07-25 3 views
1

vb.net에서 rijndael을 사용하여 암호화하고 해독하는 동안 문제가 있습니다.vb.net rijndael 한도는 64 자

64 자 이상의 문자열에서는 작동하지 않습니다. 내 Rijndael을 구성 중에 내가 그리워 아무것도

Private Function prepareRijn() As Rijndael 
    Dim KEY As String = Left(_KEY, 32) 
    Dim IV As String = Right(_KEY, 32) 
    Dim enc As New System.Text.UTF8Encoding 
    Dim byteKEY() As Byte = enc.GetBytes(KEY) 
    Dim byteIV() As Byte = enc.GetBytes(IV) 
    Dim alg As Rijndael = Rijndael.Create 
    alg.BlockSize = 256 
    alg.KeySize = 256 
    alg.Padding = PaddingMode.Zeros 
    alg.Mode = CipherMode.CBC 
    alg.Key = byteKEY 
    alg.IV = byteIV 
    Return alg 
End Function 
Function decrypt(ByVal encrypted As String) As String 
    encrypted = encrypted.Replace("Q2FrZQ==.", "") 
    Dim enc As New System.Text.UTF8Encoding 
    Dim alg As Rijndael = prepareRijn() 
    Dim ms As New MemoryStream 
    Dim cs As CryptoStream = New CryptoStream(ms, alg.CreateDecryptor, CryptoStreamMode.Write) 
    Dim data() As Byte = Convert.FromBase64String(encrypted) 
    cs.Write(data, 0, data.Length) 
    'ms.SetLength(data.Length) 
    Dim decrypted() As Byte 
    decrypted = ms.ToArray 
    cs.Close() 

    Return enc.GetString(decrypted) 
End Function 
Function encrypt(ByVal decrypt As String) As String 
    decrypt = decrypt + "    " 
    Dim alg As Rijndael = prepareRijn() 
    Dim ms As New MemoryStream() 
    Dim cs As CryptoStream = New CryptoStream(ms, alg.CreateEncryptor(), CryptoStreamMode.Write) 
    Dim data() As Byte = System.Text.Encoding.UTF8.GetBytes(decrypt) 
    cs.Write(data, 0, data.Length) 
    'ms.SetLength(data.Length) 
    Dim encrypted() As Byte = ms.ToArray() 
    cs.Close() 
    Return Convert.ToBase64String(encrypted) 
End Function 

가 : 여기 내 코드?

답변

0

완전한 암호화 클래스를 작성했습니다. 그것은 완벽하게 작동합니다. 두 문자열 및 ByteArrays에 사용할 수 있습니다. 이 클래스는 Base64에서 암호화 된 데이터를 반환합니다. Base64를 원하지 않으면 변환 만 제거하십시오.

Imports System.Text 
Imports System.Security.Cryptography 
Imports System.IO 
Imports System 

Public Class Encryption 

    ' Fields 
    Private Shared sIV As String = "Your IV Key Placed Here 32-Bytes" 
    Private Shared sKey As String = "Your SecKey Placed Here 32-Bytes" 

    ' Methods 
    Public Shared Function DecryptRJ256(ByVal prm_text_to_decrypt As String) As String 
     Dim s As String = prm_text_to_decrypt 
     Dim managed As New RijndaelManaged With { _ 
      .Padding = PaddingMode.Zeros, _ 
      .Mode = CipherMode.ECB, _ 
      .KeySize = &H100, _ 
      .BlockSize = &H100 _ 
     } 
     Dim rgbKey As Byte() = Nothing 
     Dim rgbIV As Byte() = Nothing 
     s = s.Replace("-", "+").Replace("_", "/").Replace("|", "=") 
     rgbKey = Encoding.ASCII.GetBytes(Encryption.sKey) 
     rgbIV = Encoding.ASCII.GetBytes(Encryption.sIV) 
     Dim transform As ICryptoTransform = managed.CreateDecryptor(rgbKey, rgbIV) 
     Dim buffer As Byte() = Convert.FromBase64String(s) 
     Dim buffer4 As Byte() = New Byte((buffer.Length + 1) - 1) {} 
     Dim stream As New MemoryStream(buffer) 
     Dim stream2 As New CryptoStream(stream, transform, CryptoStreamMode.Read) 
     stream2.Read(buffer4, 0, buffer4.Length) 
     Return Encoding.ASCII.GetString(buffer4) 
    End Function 

    Public Shared Function EncryptRJ256(ByVal prm_text_to_encrypt As String) As String 
     Dim s As String = prm_text_to_encrypt 
     Dim managed As New RijndaelManaged With { _ 
      .Padding = PaddingMode.Zeros, _ 
      .Mode = CipherMode.ECB, _ 
      .KeySize = &H100, _ 
      .BlockSize = &H100 _ 
     } 
     Dim buffer As Byte() = Nothing 
     Dim rgbKey As Byte() = Nothing 
     Dim rgbIV As Byte() = Nothing 
     rgbKey = Encoding.ASCII.GetBytes(Encryption.sKey) 
     rgbIV = Encoding.ASCII.GetBytes(Encryption.sIV) 
     Dim transform As ICryptoTransform = managed.CreateEncryptor(rgbKey, rgbIV) 
     Dim stream As New MemoryStream 
     Dim stream2 As New CryptoStream(stream, transform, CryptoStreamMode.Write) 
     buffer = Encoding.ASCII.GetBytes(s) 
     stream2.Write(buffer, 0, buffer.Length) 
     stream2.FlushFinalBlock() 
     Return Convert.ToBase64String(stream.ToArray).Replace("+", "-").Replace("/", "_").Replace("=", "|") 
    End Function 

    Public Shared Function EncryptRJ256(ByVal ArrayByte As Byte()) As String 

     Dim managed As New RijndaelManaged With { _ 
      .Padding = PaddingMode.Zeros, _ 
      .Mode = CipherMode.ECB, _ 
      .KeySize = &H100, _ 
      .BlockSize = &H100 _ 
     } 

     Dim rgbKey As Byte() = Nothing 
     Dim rgbIV As Byte() = Nothing 
     rgbKey = Encoding.ASCII.GetBytes(Encryption.sKey) 
     rgbIV = Encoding.ASCII.GetBytes(Encryption.sIV) 
     Dim transform As ICryptoTransform = managed.CreateEncryptor(rgbKey, rgbIV) 
     Dim stream As New MemoryStream 
     Dim stream2 As New CryptoStream(stream, transform, CryptoStreamMode.Write) 

     stream2.Write(ArrayByte, 0, ArrayByte.Length) 
     stream2.FlushFinalBlock() 
     Return Convert.ToBase64String(stream.ToArray).Replace("+", "-").Replace("/", "_").Replace("=", "|") 

    End Function 

    Public Shared Function getMD5Hash(ByVal input As String) As String 
     Dim md As MD5 = MD5.Create 
     Dim bytes As Byte() = Encoding.ASCII.GetBytes(input) 
     Dim buffer2 As Byte() = md.ComputeHash(bytes) 
     Dim builder As New StringBuilder 
     Dim i As Integer 
     For i = 0 To buffer2.Length - 1 
      builder.Append(buffer2(i).ToString("X2")) 
     Next i 
     Return builder.ToString 
    End Function 

    Public Shared Function FromBase64String(ByVal prm_text_to_decrypt As String) As String 

     Dim s As String = prm_text_to_decrypt 

     s = s.Replace("-", "+").Replace("_", "/").Replace("|", "=") 


     Dim buffer As Byte() = Convert.FromBase64String(s) 
     Return Encoding.ASCII.GetString(buffer) 

    End Function 

End Class 
+0

감사합니다. 내 경우에 매우 잘 작동합니다. – user3073660