VBA - Le chiffre de César

Implémentation en VBA de la célèbre méthode de chiffrage.
Si vous ne connaissez pas : http://www.nymphomath.ch/crypto/cesar/index.html
Pré-requis :
le code ASCII http://www.nymphomath.ch/crypto/ascii.html
Option Explicit Sub Main_Caesar() Dim Ch As String Ch = Chiffre_Cesar("Ave Caesar morituri te salutant", 7) Debug.Print Ch Debug.Print Chiffre_Cesar(Ch, -7) End Sub Function Chiffre_Cesar(sTe As String, Nbre As Long) As String Dim vT, sG As String, sT As String, i As Long, bA As Byte Const MAJ As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" Const NL As Byte = 26 Const DMA As Byte = 65 - NL Const DMI As Byte = 97 - NL sT = sTe If Nbre < NL And Nbre > NL * -1 Then sG = String(NL * 4, " ") LSet sG = MAJ & MAJ & MAJ vT = Split(StrConv(sG, vbUnicode), Chr(0)) For i = 1 To Len(sT) If Mid(sT, i, 1) Like "[a-zA-Z]" Then bA = Asc(Mid(sT, i, 1)) If Mid(sT, i, 1) = vT(bA - DMA) Then Mid(sT, i) = vT(bA - DMA + Nbre) Else Mid(sT, i) = LCase(vT(bA - DMI + Nbre)) End If End If Next i End If Chiffre_Cesar = sT End Function
Ce document intitulé « VBA - Le chiffre de César » issu de Comment Ça Marche (www.commentcamarche.net) est mis à disposition sous les termes de la licence Creative Commons. Vous pouvez copier, modifier des copies de cette page, dans les conditions fixées par la licence, tant que cette note apparaît clairement.