saya ingin membuat suatu web service,sblm itu saya awali terlebih dahulu dgn membuat method dalam file .aspx sblm dipindah ke web service.Berikut ini adalah code behind yg saya buat,dengan ini saya mencoba berhasil.
Dim RSA As New RSACryptoServiceProvider()
Dim pubkey As RSAParameters = RSA.ExportParameters(False)
Dim privkey As RSAParameters = RSA.ExportParameters(True)
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim ByteConverter As New UnicodeEncoding()
' Dim dataToEncrypt As Byte() = ByteConverter.GetBytes("Data to Encrypt")
' Dim dataToEncrypt As Byte()
Dim encryptedData() As Byte
Dim decryptedData() As Byte
Dim random1 As String
Dim random2 As String
'Bangkitkan random
random1 = getRandom() 'client terima ini
Session("acak") = random1
'Client mengenkrip
Dim dataToEncrypt As Byte() = ByteConverter.GetBytes(random1)
' Dim RSA As New RSACryptoServiceProvider()
' Dim pubkey As RSAParameters = RSA.ExportParameters(False)
' Dim privkey As RSAParameters = RSA.ExportParameters(True)
Dim base64teks As String = Convert.ToBase64String(dataToEncrypt)
'Create a new instance of RSACryptoServiceProvider to generate
'public and private key data.
Dim base64konv As Byte() = Convert.FromBase64String(base64teks)
Dim base64konv2 As String = ByteConverter.GetString(base64konv)
'Pass the data to ENCRYPT, the public key information
'(using RSACryptoServiceProvider.ExportParameters(false),
'and a boolean flag specifying no OAEP padding.
'encryptedData = RSAEncrypt(dataToEncrypt, RSA.ExportParameters(False), False)
encryptedData = RSAEncrypt(dataToEncrypt, pubkey) 'dikirim ke server
Dim base64enc = Convert.ToBase64String(encryptedData)
' Session("test") = Session.SessionID.ToString
Dim strEnc As String = ByteConverter.GetString(encryptedData)
'Pass the data to DECRYPT, the private key information
'(using RSACryptoServiceProvider.ExportParameters(true),
'and a boolean flag specifying no OAEP padding.
decryptedData = RSADecrypt(encryptedData, privkey) 'server ngedekrip
' Dim hashdecrypt = Convert.ByteConverter.GetString(decryptedData).GetHashCode
txtEnc.Text = ByteConverter.GetString(encryptedData)
'txtEnc.Text = Convert.ToString(encryptedData)
txtDec.Text = ByteConverter.GetString(decryptedData)
' txtDec.Text = Convert.ToString(decryptedData)
If isAuth(encryptedData) Then
Label1.Text = "Cocok"
Else
Label1.Text = "Tidak Cocok"
End If
End Sub
Public Function RSAEncrypt(ByVal DataToEncrypt() As Byte, ByVal RSAKeyInfo As RSAParameters) As Byte()
Try
'Create a new instance of RSACryptoServiceProvider.
' Dim RSA As New RSACryptoServiceProvider()
' Dim RSA As New RSACryptoServiceProvider()
' Dim pubkey As RSAParameters = RSA.ExportParameters(False)
' Dim privkey As RSAParameters = RSA.ExportParameters(True)
'Import the RSA Key information. This only needs
'toinclude the public key information.
RSA.ImportParameters(RSAKeyInfo)
' RSA.ImportParameters(RSA.ExportParameters(False))
'Encrypt the passed byte array and specify OAEP padding.
'OAEP padding is only available on Microsoft Windows XP or
'later.
Return RSA.Encrypt(DataToEncrypt, False)
'Catch and display a CryptographicException
'to the console.
Catch e As CryptographicException
Console.WriteLine(e.Message)
Return Nothing
End Try
End Function
Public Function getRandom() As String
Dim rnd As New Random
Dim bigint As Long
bigint = rnd.Next()
Return bigint.ToString
End Function
Public Function RSADecrypt(ByVal DataToDecrypt() As Byte, ByVal RSAKeyInfo As RSAParameters) As Byte()
Try
'Create a new instance of RSACryptoServiceProvider.
' Dim RSA As New RSACryptoServiceProvider()
' Dim pubkey As RSAParameters = RSA.ExportParameters(False)
' Dim privkey As RSAParameters = RSA.ExportParameters(True)
'Import the RSA Key information. This needs
'to include the private key information.
RSA.ImportParameters(RSAKeyInfo)
'Decrypt the passed byte array and specify OAEP padding.
'OAEP padding is only available on Microsoft Windows XP or
'later.
Return RSA.Decrypt(DataToDecrypt, False)
'Catch and display a CryptographicException
'to the console.
Catch e As CryptographicException
Console.WriteLine(e.ToString())
Return Nothing
End Try
End Function
Public Function ByteToString(ByVal bte As Byte()) As String
Dim ByteConverter As New UnicodeEncoding()
Return ByteConverter.GetString(bte)
End Function
Public Function StringToByte(ByVal str As String) As Byte()
Dim ByteConverter As New UnicodeEncoding()
Return ByteConverter.GetBytes(str)
End Function
Public Function isAuth(ByVal hasilenkrip As Byte()) As Boolean
Dim dekrip As Byte()
dekrip = RSADecrypt(hasilenkrip, privkey)
Return ByteToString(dekrip) = Session("Acak")
End Function
---------------------------------------------------------------------------------------------------------------
tetapi jika saya pindah ke web service, method tersebut jalan untuk Encrypt dan generate Random.tetapi begitu ngedekrip muncul exception, "Bad Key".Berikut ini code yg saya buat setelah dipindah ke webservice
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.ComponentModel
Imports System
Imports System.Security.Cryptography
Imports System.Text
_
_
_
Public Class RSAService
Inherits System.Web.Services.WebService
Dim RSA As New RSACryptoServiceProvider()
Dim pubkey As RSAParameters = RSA.ExportParameters(False)
Dim privkey As RSAParameters = RSA.ExportParameters(True)
_
Public Function HelloWorld() As String
Return "Hello World"
End Function
_
Public Function RSAEncrypt(ByVal DataToEncrypt() As Byte, ByVal RSAKeyInfo As RSAParameters) As Byte()
Try
'Create a new instance of RSACryptoServiceProvider.
' Dim RSA As New RSACryptoServiceProvider()
' Dim RSA As New RSACryptoServiceProvider()
' Dim pubkey As RSAParameters = RSA.ExportParameters(False)
' Dim privkey As RSAParameters = RSA.ExportParameters(True)
'Import the RSA Key information. This only needs
'toinclude the public key information.
RSA.ImportParameters(RSAKeyInfo)
' RSA.ImportParameters(RSA.ExportParameters(False))
'Encrypt the passed byte array and specify OAEP padding.
'OAEP padding is only available on Microsoft Windows XP or
'later.
Return RSA.Encrypt(DataToEncrypt, False)
'Catch and display a CryptographicException
'to the console.
Catch e As CryptographicException
Console.WriteLine(e.Message)
Return Nothing
End Try
End Function
_
Public Function RSADecrypt(ByVal DataToDecrypt() As Byte, ByVal RSAKeyInfo As RSAParameters) As Byte()
Try
'Create a new instance of RSACryptoServiceProvider.
' Dim RSA As New RSACryptoServiceProvider()
' Dim pubkey As RSAParameters = RSA.ExportParameters(False)
' Dim privkey As RSAParameters = RSA.ExportParameters(True)
'Import the RSA Key information. This needs
'to include the private key information.
RSA.ImportParameters(RSAKeyInfo)
'Decrypt the passed byte array and specify OAEP padding.
'OAEP padding is only available on Microsoft Windows XP or
'later.
Return RSA.Decrypt(DataToDecrypt, False)
'Catch and display a CryptographicException
'to the console.
Catch e As CryptographicException
Console.WriteLine(e.ToString())
Return Nothing
End Try
End Function
_
Public Function ByteToString(ByVal bte As Byte()) As String
Dim ByteConverter As New UnicodeEncoding()
Return ByteConverter.GetString(bte)
End Function
_
Public Function StringToByte(ByVal str As String) As Byte()
Dim ByteConverter As New UnicodeEncoding()
Return ByteConverter.GetBytes(str)
End Function
_
Public Function getRandom() As String
Dim rnd As New Random
Dim bigint As Long
bigint = rnd.Next()
Return bigint.ToString
End Function
End Class
------------------------------------------------------------------
saya panggil dgn client spt ini yg cukup sederhana: (pastikan saya sudah add web reference)
Dim objRSA As New MyRSA
TextBox1.Text = objRSA.getRandom
Dim databuatdikirim() As Byte = objRSA.StringToByte(TextBox1.Text)
Dim enkripted() As Byte = objRSA.DoEncrypt(databuatdikirim)
Dim dekripted() As Byte = objRSA.DoDecrypt(enkripted)
TextBox2.Text = objRSA.ByteToString(enkripted)
TextBox3.Text = objRSA.ByteToString(dekripted)
------------------------------------------------------------------------
Sebelumnya saya ucapkan terima kasih, maaf merepotkan