2014-07-10 23 views
0

我正在製作一個自定義的加密應用程序,而我卡在替換部分。 例如我有字符串"AB12AB12",我想加密它,以便每個A變成1,每個B a 2,每1個A和每2個B.如果一切順利,輸出應該是12AB12AB,但這是什麼發生:VB如何一次替換多個字符?

Private Sub Encrypt() 
Dim str As String = "AB12AB12" 
str = str.Replace("A", "1") 'now the output is "1B121B12"' 
str = str.Replace("B", "2") 'now the output is "12121212"' 
str = str.Replace("1", "A") 'this is where it goes wrong, output is now "A2A2A2A2" 
str = str.Replace("2", "B") 'now the output is "ABABABAB" and it should be "12AB12AB" 
output = str 
End Sub 

問題是,使用此代碼,你不斷取代所有包括以前的替換。也許有一個選項喜歡同時替換每個字符,以便替換不會被替換...

+0

不要做直替換英寸按字符進行。 – durbnpoisn

+2

這裏有一個相當有Doh因子。不要使用已經出現在原始字符串中的字符進行編碼,您無法再次恢復原始字符。這樣做根本沒有意義,一個簡單的替代密碼被頻繁的表格打亂了。使用真正的加密,System.Security.Cryptography命名空間。 –

+0

這不是任何加密夥伴,不要自己滾動至少任何私人... – Codexer

回答

2

當您使用Replace進行替換時,您會得到一個字符串,它具有處理的混合字符未經處理的,你不能分辨他們。從開始處理字符串結束,而不是:

Private Function Encrypt(str as string) as String 
    Dim source As String = "AB12" 
    dim dest as string = "12AB" 
    Dim result As New StringBuilder(str.Length) 
    For Each c As Char In str 
    result.Append(dest(source.IndexOf(c))) 
    Next 
    return result.ToString() 
End Function 

注:我離開了名Encrypt的方法,儘管替代密碼是不是通常被認爲是當今加密。

0

之所以失敗是因爲如果你修改海峽與"A" -> "1",這些'1'字符將被用來轉換回來的時候還有:

"AB12AB12" --a>1--> "1B121B12" 
"1B121B12" --b>2--> "12121212" 
"12121212" --1>a--> "A2A2A2A2" 
"A2A2A2A2" --2>b--> "ABABABAB" 

基本上有兩種方法可以做到這一點:

  • 字符每個字符
  • 使用「臨時字符

字,每字

在這裏,你取每次迭代的字符並將其轉化爲相應的值:如果你不知道有'3'

Private Sub Encrypt() 
    Dim str As String = "AB12AB12" 
    Dim fmc As Char() = {"A","B","1","2"} 
    Dim toc As Char() = {"1","2","A","B"} 
    Dim sb As New StringBuilder(str.Length) 
    Dim i As Integer 
    For i As Integer = 1 To str.Length-1 
     sb.Append(fmc(Array.IndexOf(toc,GetChar(str,i)))) 
    Next 
    output = sb.ToString() 
End Sub 

臨時性質

(或另一個字符會發生,你可以簡單說明):

Private Sub Encrypt() 
    Dim str As String = "AB12AB12" 
    str = str.Replace("A", "3") 
    str = str.Replace("1", "A") 
    str = str.Replace("3", "1") 
    str = str.Replace("B", "3") 
    str = str.Replace("2", "B") 
    str = str.Replace("3", "B") 
    output = str 
End Sub 

但是這會降低性能。


作爲@HansPassant已經指出的一個註釋,你最好不要設計自己的加密系統。這個例子可以通過頻率分析來解決。

+0

非常感謝!我不會用這個來加密任何嚴肅的事情,這只是爲了好玩和教育。 – Kazem

0

在這裏,我提出了不同的方法比你討論,檢查準備

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
      MsgBox(encrypt(plain_text.Text, replace_it.Text, replace_with.Text)) 
'display the result in the message box 
    End Sub 
  • 其中plain_text.Text:輸入文本
  • replace_it。文本:要替換的字符
  • replace_with.Text:該字符正在替換。

以下是用於執行加密功能:

Public Function encrypt(ByVal str As String, ByVal frm As String, ByVal replace As String) As String 
    Dim cipher As String 
    cipher = "" 
    For i As Integer = 0 To Len(str) - 1 'check each character separately 
     If getbyte(str, i) = frm Then 
      cipher = cipher & replace 'replace the character if is same as the requested 
     Else 
      cipher = cipher & getbyte(str, i) 
     End If 
    Next 
    Return cipher.ToString 
End Function 

函數來獲取每個字符的字符串

Private Function getbyte(ByVal s As String, ByVal place As Integer) As String 
    If place < Len(s) Then 
     place = place + 1 
     getbyte = Mid(s, place, 1) 
    Else 
     getbyte = "" 
    End If 
End Function