2009-10-07 3 views

답변

-2

C#으로 입력하고 VB로 변환했습니다. 잘하면 그것은 여전히 ​​작동합니다!

Dim input As [Char]() = "Super Secret String".ToCharArray() 
Dim secret As New SecureString() 

For idx As Integer = 0 To input.Length - 1 
    secret.AppendChar(input(idx)) 
Next 
SecurePassword.MakeReadOnly() 

Dim pBStr As IntPtr = Marshal.SecureStringToBSTR(secret) 

Dim output As String = Marshal.PtrToStringBSTR(pBStr) 
Marshal.FreeBSTR(pBStr) 

Dim sha As SHA512 = New SHA512Managed() 
Dim result As Byte() = sha.ComputeHash(Encoding.UTF8.GetBytes(output)) 

편집는 :

그것은 자신에 의해 지적 된 것처럼 및 코멘트에 몇 가지 다른, 나는 여기에 관심이를 가지고 싶어. 이렇게하는 것은 좋은 생각이 아닙니다. 더 이상 안전하지 않은 장소로 바이트를 이동하고 있습니다. 물론 할 수는 있지만 실제로는 안된다.

+0

왜 변환하셨습니까? C#에 있나요? – backslash17

+0

Luke가 VB.net에서 요청했기 때문에 변환했습니다. C#에서이 변환기를 사용해야한다면 http://www.developerfusion.com/tools/convert/vb-to-csharp/ – Joe

+0

Joe! 감사합니다! – Luke

0

사용 된 String 인스턴스 (출력) 만 피하고 문자 배열로 바꾼다면 어떨까요? 이렇게하면 사용 후이 어레이를 닦아 낼 수 있습니다.

public static String SecureStringToMD5(SecureString password) 
    { 
     int passwordLength = password.Length; 
     char[] passwordChars = new char[passwordLength]; 

     // Copy the password from SecureString to our char array 
     IntPtr passwortPointer = Marshal.SecureStringToBSTR(password); 
     Marshal.Copy(passwortPointer, passwordChars, 0, passwordLength); 
     Marshal.ZeroFreeBSTR(passwortPointer); 

     // Hash the char array 
     MD5 md5Hasher = MD5.Create(); 
     byte[] hashedPasswordBytes = md5Hasher.ComputeHash(Encoding.Default.GetBytes(passwordChars)); 

     // Wipe the character array from memory 
     for (int i = 0; i < passwordChars.Length; i++) 
     { 
      passwordChars[i] = '\0'; 
     } 

     // Your implementation of representing the hash in a readable manner 
     String hashString = ConvertToHexString(hashedPasswordBytes); 

     // Return the result 
     return hashString; 
    } 

내가 빠진 것이 있습니까?

+1

예 -이 질문에 대한 답변 : http://stackoverflow.com/questions/14293344/hashing-a-securestring-in-net – RobSiklos

관련 문제