我有一个用dot net编写的代码用于加密和解密方法。我需要在Java中实现它。代码应该同时具有加密和解密方法。
Private DES As New TripleDESCryptoServiceProvider
Private MD5 As New MD5CryptoServiceProvider
Private even As Integer = 1 'สำหรับบวกเพิ่มไปใน private key ของเดือนที่เป็นเลขคู่
Private odd As Integer = 2 'สำหรับบวกเพิ่มไปใน private key ของเดือนที่เป็นเลขคี่
Private Property key() As String
Get
Return GetKey()
End Get
Set(ByVal value As String)
End Set
End Property
Private Function MD5Hash(ByVal value As String) As Byte()
Return MD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(value))
End Function
Private Function GetKey() As String
Dim yyyy As Integer = Year(Now)
If yyyy > 2500 Then
yyyy -= 543
End If
Dim mm As String = Right("0" & Month(Now), 2)
Dim dd As String = Right("0" & Day(Now), 2)
Dim key As Integer = dd & mm & yyyy
If CInt(mm) Mod 2 = 0 Then
key += even
Else
key += odd
End If
Return key
End Function
Public Function Encrypt(ByVal stringToEncrypt As String) As String
DES.Key = MD5Hash(key)
DES.Mode = CipherMode.ECB
Dim Buffer As Byte() = UnicodeEncoding.Unicode.GetBytes(stringToEncrypt)
Return Convert.ToBase64String(DES.CreateEncryptor().TransformFinalBlock(Buffer, 0, Buffer.Length))
End Function
Public Function Decrypt(ByVal encryptedString As String) As String
Try
DES.Key = MD5Hash(key)
DES.Mode = CipherMode.ECB
Dim Buffer As Byte() = Convert.FromBase64String(encryptedString)
Return UnicodeEncoding.Unicode.GetString(DES.CreateDecryptor().TransformFinalBlock(Buffer, 0, Buffer.Length))
Catch ex As Exception
Return "Invalid Key, Decryption Failed."
End Try
End Function
答案 0 :(得分:1)
你有没有想过自己做点什么?
我想提请您注意该页面:http://www.java2s.com/Code/Java/Security/DESCrypterandDecrypter.htm