SHA1 또는 SHA512 HASH를 생성하기 위해 Securestring을 어떻게 사용합니까?
-
20-09-2019 - |
문제
vb.net 내에서 Securestring Varible을 사용하여 SHA1 또는 SHA512 HASH로 변환하고 싶습니다. Securestring을 Hashalgorithm.com의 바이트 배열로 어떻게 안전하게 변환합니까?
해결책
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))
편집하다:
그것이 나 자신과 다른 사람들에 의해 지적되었으므로, 나는 이것을 여기에 주목하고 싶습니다. 이렇게하는 것은 좋은 생각이 아닙니다. 바이트를 더 이상 안전하지 않은 곳으로 옮기고 있습니다. 당신은 그것을 할 수 있지만 아마도 당신은 아마
다른 팁
유일한 사용 된 문자열 인스턴스 (출력)를 피하고 문자 배열로 바꾸면 어떻게해야합니까? 이렇게하면 사용 후이 배열을 닦을 수 있습니다.
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;
}
내가 놓친 것이 있습니까?
제휴하지 않습니다 StackOverflow