なんだかアプリケーションのセキュリティ強化絡みの仕事がまわってきそうなので、
色々勉強しないとなぁと思っています。
とはいっても 赤間さんの本 ばかり眺めているとだんだん眠くなってくるので、
前の案件で特定のフィールドを MD5 で暗号化する要件があったのを思い出し、
最初に MD5CryptoServiceProvider を使ってお手軽に暗号化したものの、
複合復号化をあーじゃこーじゃやってうまくいかない。うがー。
へんてこな文字列が帰ってきたり、例外吐いたりして
もう嫌じゃーとなってきたので、10 行でズバリ!! を眺めてました。
そしたら、TripleDESCryptoServiceProvider を使っていたので
マネてそれを使ってうにゃうにゃやって、一応実装できました。
他言語での MD5 による暗号化/複合復号化の実装はどんな感じなのかなぁと思って検索したら
こんなのを見つけました。すごいぞ javascript。ワタシは好きじゃないけど。
JavaScript MD5
たぶん使わないけど一応自分用メモとしてコード書いておきます。
# 追記
# なちゃさんと凪瀬さんその他大勢のご指摘により、以下のコードはインチキな事が判明しました。
# もっと精進しないと...
Public Class MD5Test
'''
''' MD5 によるハッシュ化(was 暗号化)←嘘
'''
''' 暗号化する文字列
''' パスワード
Public Function Encrypt(ByVal plainText As String, Optional ByVal pass As String = "") As String
Dim provider As System.Security.Cryptography.TripleDESCryptoServiceProvider = _
New System.Security.Cryptography.TripleDESCryptoServiceProvider()
Dim p As System.Security.Cryptography.PasswordDeriveBytes = _
New System.Security.Cryptography.PasswordDeriveBytes(pass, New Byte(-1) {})
With provider
.IV = New Byte(7) {}
.Key = p.CryptDeriveKey("RC2", "MD5", 128, New Byte(7) {})
End With
Using memStream As System.IO.MemoryStream = New System.IO.MemoryStream(plainText.Length * 2)
Using ctream As System.Security.Cryptography.CryptoStream = _
New System.Security.Cryptography.CryptoStream(memStream, _
provider.CreateEncryptor(), _
Security.Cryptography.CryptoStreamMode.Write)
Dim plainBytes As Byte() = System.Text.Encoding.Default.GetBytes(plainText)
ctream.Write(plainBytes, 0, plainBytes.Length)
ctream.FlushFinalBlock()
Dim encryptedBytes As Byte() = New Byte(CInt(memStream.Length) - 1) {}
memStream.Position = 0
memStream.Read(encryptedBytes, 0, CInt(memStream.Length))
Return Convert.ToBase64String(encryptedBytes)
End Using
End Using
End Function
'''
''' 解読(was 復号化)
'''
''' MD5によるハッシュ(was 暗号)化されたBase64エンコード文字列←嘘
''' パスワード
Public Function Decrypt(ByVal base64Text As String, Optional ByVal pass As String = "") As String
Dim provider As System.Security.Cryptography.TripleDESCryptoServiceProvider = _
New System.Security.Cryptography.TripleDESCryptoServiceProvider()
Dim p As System.Security.Cryptography.PasswordDeriveBytes = _
New System.Security.Cryptography.PasswordDeriveBytes(pass, New Byte(-1) {})
With provider
.IV = New Byte(7) {}
.Key = p.CryptDeriveKey("RC2", "MD5", 128, New Byte(7) {})
End With
Dim encryptedBytes As Byte() = Convert.FromBase64String(base64Text)
Using memStream As System.IO.MemoryStream = New System.IO.MemoryStream(base64Text.Length)
Using ctream As System.Security.Cryptography.CryptoStream = _
New System.Security.Cryptography.CryptoStream(memStream, _
provider.CreateDecryptor(), _
Security.Cryptography.CryptoStreamMode.Write)
ctream.Write(encryptedBytes, 0, encryptedBytes.Length)
ctream.FlushFinalBlock()
Dim decreptedBytes As Byte() = New Byte(CInt(memStream.Length) - 1) {}
memStream.Position = 0
memStream.Read(decreptedBytes, 0, CInt(memStream.Length))
Return System.Text.Encoding.Default.GetString(memStream.ToArray())
End Using
End Using
End Function
End Class