Salut,
Cette petite source fait suite au tutoriel suivant :
https://cadernis.com/d/719
Je trouve qu'il est dommage et complexe d'ajouter une DLL entière au projet alors qu'il suffit de prendre les fonctions dont on se sert.
C'est pourquoi j'ai réuni les fonctions nécessaires dans une classe, que vous pouvez facilement ajouter au projet.
(Nouveau fichier -> RSAManager.vb)
Actuellement pas à jour pour D. 2.9.
Merci d'attendre une nouvelle version.
(Ou de chercher par vous même une solution, il y en a plus d'une !)
Voilà voilà, juste histoire de réduire le poids de vos programmes.
Ancienne version (D. < 2.9.0) :
Cliquez pour révéler
Cliquez pour masquer
Imports System.Collections.Generic
Imports System.IO
Imports System.Security.Cryptography
Imports System.Text
Public Class RSAManager
Private Shared Function CompareByteArrays(ByVal a As Byte(), ByVal b As Byte()) As Boolean
If a.Length <> b.Length Then
Return False
End If
Dim i As Integer = 0
For Each c As Byte In a
If c <> b(i) Then
Return False
End If
i += 1
Next
Return True
End Function
Private Shared Function DecodeX509PublicKey(ByVal x509Key As Byte()) As RSACryptoServiceProvider
Dim seqID As Byte() = {&H30, &HD, &H6, &H9, &H2A, &H86, _
&H48, &H86, &HF7, &HD, &H1, &H1, _
&H1, &H5, &H0}
Dim mem As New MemoryStream(x509Key)
Dim binr As New BinaryReader(mem)
Try
Dim twobytes As UShort = binr.ReadUInt16()
Select Case twobytes
Case &H8130
binr.ReadByte()
Case &H8230
binr.ReadInt16()
Case Else
Return Nothing
End Select
Dim seq As Byte() = binr.ReadBytes(15)
If Not CompareByteArrays(seq, seqID) Then
Return Nothing
End If
twobytes = binr.ReadUInt16()
Select Case twobytes
Case &H8103
binr.ReadByte()
Case &H8203
binr.ReadInt16()
Case Else
Return Nothing
End Select
Dim bt As Byte = binr.ReadByte()
If bt <> &H0 Then
Return Nothing
End If
twobytes = binr.ReadUInt16()
Select Case twobytes
Case &H8130
binr.ReadByte()
Case &H8230
binr.ReadInt16()
Case Else
Return Nothing
End Select
twobytes = binr.ReadUInt16()
Dim lowbyte As Byte
Dim highbyte As Byte = &H0
Select Case twobytes
Case &H8102
lowbyte = binr.ReadByte()
Case &H8202
highbyte = binr.ReadByte()
lowbyte = binr.ReadByte()
Case Else
Return Nothing
End Select
Dim modint As Byte() = {lowbyte, highbyte, &H0, &H0}
Dim modsize As Integer = BitConverter.ToInt32(modint, 0)
Dim firstbyte As Byte = binr.ReadByte()
binr.BaseStream.Seek(-1, SeekOrigin.Current)
If firstbyte = &H0 Then
binr.ReadByte()
modsize -= 1
End If
Dim modulus As Byte() = binr.ReadBytes(modsize)
If binr.ReadByte() <> &H2 Then
Return Nothing
End If
Dim expbytes As Integer = Convert.ToInt32(binr.ReadByte())
Dim exponent As Byte() = binr.ReadBytes(expbytes)
Dim RSA As New RSACryptoServiceProvider()
Dim RSAKeyInfo As New RSAParameters() With { _
.Modulus = modulus, _
.Exponent = exponent _
}
RSA.ImportParameters(RSAKeyInfo)
Return RSA
Catch ex As Exception
Return Nothing
Finally
binr.Close()
End Try
End Function
Public Shared Function Encrypt(ByVal text As String, ByVal key As List(Of Integer)) As List(Of Integer)
Dim array As Byte() = New Byte(key.Count - 1) {}
For i As Integer = 0 To key.Count - 1
array(i) = CByte(key(i))
Next
Dim rsa As RSACryptoServiceProvider = DecodeX509PublicKey(array)
If rsa Is Nothing Then
Return Nothing
End If
Dim result As Byte() = rsa.Encrypt(Encoding.UTF8.GetBytes(text), False)
Dim intArray As New List(Of Integer)
For Each b As Byte In result
intArray.Add(b)
Next
Return intArray
End Function
End Class
Qu'il faut utiliser ainsi :
Dim EncryptedPassword As List (Of Integer) = RSAManager.Encrypt(Message.Salt & Password, Message.Key)
Ancienne version (D. < 2.5.0) :
Cliquez pour révéler
Cliquez pour masquer
Voici le code :
Public Class RSAManager
Private Const Key As String =
"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApHRiGIhIJrNdUJkKGtWC" +
"sSqIza+2gPsjGXhSoDTOcokq59Et8d8SzgF68RvAZXezPO8tnUhlyvaDem4QSFLV" +
"PVAmSRcp47HW4lpp11WHBlDsEEXQTBkM8nDyqSgn8dMANvButRDt/44OKslrfqmV" +
"7ANmZggZ2wXN0T6XWt3FVC66X8+E7rUMUOREQYCDq3zrX4dNYy3y21lyJZeXTkSd" +
"AmijqIHrrwLPTA/wpWLCEaIJ9OAWjds8L6TqONXvnf3qOtI/QsrWv24lRjtmRSeR" +
"eKFIPrk8QQbcd2h4VUi06fJZ2ydCx0pOwU33izN42pmZoCrgdCwghFm1i2feQa0M" +
"vQIDAQAB"
Private Shared Function CompareByteArrays(ByVal a As Byte(), ByVal b As Byte()) As Boolean
If a.Length <> b.Length Then
Return False
End If
Dim i As Integer = 0
For Each c As Byte In a
If c <> b(i) Then
Return False
End If
i += 1
Next
Return True
End Function
Private Shared Function DecodeX509PublicKey(ByVal x509Key As Byte()) As RSACryptoServiceProvider
Dim seqID As Byte() = {&H30, &HD, &H6, &H9, &H2A, &H86, _
&H48, &H86, &HF7, &HD, &H1, &H1, _
&H1, &H5, &H0}
Dim seq As Byte()
Dim mem As New MemoryStream(x509Key)
Dim binr As New BinaryReader(mem)
Dim bt As Byte
Dim twobytes As UShort
Try
twobytes = binr.ReadUInt16()
If twobytes = &H8130 Then
binr.ReadByte()
ElseIf twobytes = &H8230 Then
binr.ReadInt16()
Else
Return Nothing
End If
seq = binr.ReadBytes(15)
If Not CompareByteArrays(seq, seqID) Then
Return Nothing
End If
twobytes = binr.ReadUInt16()
If twobytes = &H8103 Then
binr.ReadByte()
ElseIf twobytes = &H8203 Then
binr.ReadInt16()
Else
Return Nothing
End If
bt = binr.ReadByte()
If bt <> &H0 Then
Return Nothing
End If
twobytes = binr.ReadUInt16()
If twobytes = &H8130 Then
binr.ReadByte()
ElseIf twobytes = &H8230 Then
binr.ReadInt16()
Else
Return Nothing
End If
twobytes = binr.ReadUInt16()
Dim lowbyte As Byte
Dim highbyte As Byte = &H0
If twobytes = &H8102 Then
lowbyte = binr.ReadByte()
ElseIf twobytes = &H8202 Then
highbyte = binr.ReadByte()
lowbyte = binr.ReadByte()
Else
Return Nothing
End If
Dim modint As Byte() = {lowbyte, highbyte, &H0, &H0}
Dim modsize As Integer = BitConverter.ToInt32(modint, 0)
Dim firstbyte As Byte = binr.ReadByte()
binr.BaseStream.Seek(-1, SeekOrigin.Current)
If firstbyte = &H0 Then
binr.ReadByte()
modsize -= 1
End If
Dim modulus As Byte() = binr.ReadBytes(modsize)
If binr.ReadByte() <> &H2 Then
Return Nothing
End If
Dim expbytes As Integer = CInt(binr.ReadByte())
Dim exponent As Byte() = binr.ReadBytes(expbytes)
Dim RSA As New RSACryptoServiceProvider()
Dim RSAKeyInfo As New RSAParameters() With {
.Modulus = modulus,
.Exponent = exponent
}
RSA.ImportParameters(RSAKeyInfo)
Return RSA
Catch e As Exception
Return Nothing
Finally
binr.Close()
End Try
End Function
Public Shared Function Encrypt(ByVal text As String) As String
Dim rsa As RSACryptoServiceProvider = DecodeX509PublicKey(Convert.FromBase64String(Key))
If rsa Is Nothing Then Return ""
Return Convert.ToBase64String(rsa.Encrypt(Encoding.UTF8.GetBytes(text), False))
End Function
End Class
Vous avez juste à l'utiliser ainsi :
Dim EncryptedPassword As String = RSAManager.Encrypt(Key & Password)