Advertisement
Falexom

Untitled

Nov 16th, 2023
889
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 4.51 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5.     "strconv"
  6. )
  7.  
  8. func expansion(keyPart []string) []string {
  9.     var expendedKey []string = make([]string, 48)
  10.     expansionTable := [48]int{31, 0, 1, 2, 3, 4, 3, 4, 5, 6, 7, 8, 7, 8, 9, 10, 11, 12, 11, 12, 13, 14, 15, 16, 15, 16, 17, 18, 19, 20, 19, 20, 21, 22, 23, 24, 23, 24, 25, 26, 27, 28, 27, 28, 29, 30, 31, 0,
  11.     }
  12.  
  13.     for key, item := range expansionTable{
  14.         expendedKey[key] = keyPart[item]
  15.     }
  16.     return expendedKey
  17. }
  18.  
  19.  
  20. func bitwise_xor(expendedKey []string) []string {
  21.     var roundKey = []string{"1", "1", "1", "0", "0", "0", "0", "0", "1", "0", "1", "1", "1", "1", "1", "0", "0", "1", "1", "0", "0", "1", "1", "0", "1", "1", "1", "1", "0", "1", "1", "1", "0", "0", "1", "0", "1", "0", "1", "0", "1", "0", "1", "1", "1", "0", "0", "0"}
  22.     var xoredExpendedKey []string = make([]string, 48)
  23.  
  24.     for key, value := range roundKey{
  25.         keyPart, err := strconv.Atoi(value)
  26.        
  27.         if err != nil {
  28.             panic(err)
  29.         }
  30.        
  31.         expendedPart, err := strconv.Atoi(expendedKey[key])
  32.        
  33.         if err != nil {
  34.             panic(err)
  35.         }
  36.        
  37.         res := keyPart ^ expendedPart
  38.         result := strconv.Itoa(res)
  39.         xoredExpendedKey[key] = result
  40.     }
  41.     return xoredExpendedKey
  42. }
  43.  
  44.  
  45. func s_box_generation(xorResultKey []string) []string {
  46.  
  47.     var sBoxTableRow0 = [][]int{{0, 0, 14}, {0, 1, 4}, {0, 2, 13}, {0, 3, 1}, {0, 4, 2}, {0, 5, 15}, {0, 6, 11}, {0, 7, 8}, {0, 8, 3}, {0, 9, 10}, {0, 10, 6}, {0, 11, 12}, {0, 12, 5}, {0, 13, 9}, {0, 14, 0}, {0, 15, 7}}
  48.     var sBoxTableRow1 = [][]int{{1, 0, 0}, {1, 1, 15}, {1, 2, 7}, {1, 3, 4}, {1, 4, 14}, {1, 5, 2}, {1, 6, 13}, {1, 7, 1}, {1, 8, 10}, {1, 9, 6}, {1, 10, 12}, {1, 11, 11}, {1, 12, 9}, {1, 13, 5}, {1, 14, 3}, {1, 15, 8}}
  49.     var sBoxTableRow2 = [][]int{{2, 0, 4}, {2, 1, 1}, {2, 2, 14}, {2, 3, 8}, {2, 4, 13}, {2, 5, 6}, {2, 6, 2}, {2, 7, 11}, {2, 8, 15}, {2, 9, 12}, {2, 10, 9}, {2, 11, 7}, {2, 12, 3}, {2, 13, 10}, {2, 14, 5}, {2, 15, 0}}
  50.     var sBoxTableRow3 = [][]int{{3, 0, 15}, {3, 1, 12}, {3, 2, 8}, {3, 4, 4}, {3, 5, 9}, {3, 6, 1}, {3, 7, 7}, {3, 8, 5}, {3, 9, 11}, {3, 10, 3}, {3, 11, 14}, {3, 12, 10}, {3, 13, 0}, {3, 14, 6}, {3, 15, 13}}
  51.  
  52.     var rowsStable = map[string][][]int{
  53.         "0": sBoxTableRow0,
  54.         "1": sBoxTableRow1,
  55.         "2": sBoxTableRow2,
  56.         "3": sBoxTableRow3,
  57.     }
  58.     var sBoxBase [][]string
  59.     var slicesIndx []int
  60.     var output []string
  61.     length := len(xorResultKey)
  62.     foo := 0
  63.     bar := 0
  64.     for foo < length-1{
  65.         foo += 6
  66.         slicesIndx = append(slicesIndx, foo)
  67.     }
  68.     for _, value := range slicesIndx {
  69.         sliceXor := xorResultKey[bar:value]
  70.         sBoxBase = append(sBoxBase, sliceXor)
  71.         bar = value
  72.     }
  73.     var finalBox [][]int
  74.    
  75.     for _, value := range sBoxBase{
  76.         var newSBox []int
  77.         string_position := value[0] + value[5]
  78.         column_position := value[1] + value[2] + value[3] + value[4]
  79.        
  80.         decimal_string, err := strconv.ParseInt("0b" + string_position, 0, 64)
  81.  
  82.         if err != nil {
  83.             panic(err)
  84.         }
  85.  
  86.         decimal_column, err := strconv.ParseInt("0b" + column_position, 0, 64)
  87.  
  88.         if err != nil {
  89.             panic(err)
  90.         }
  91.  
  92.         map_position := strconv.Itoa(int(decimal_string))
  93.         currentSBox := rowsStable[map_position]
  94.        
  95.         // можно через хешмап, но это перегруз вложенности!
  96.         var to_change int
  97.         for key, _ := range currentSBox {
  98.             if int(decimal_column) == currentSBox[key][1] {
  99.                 to_change = currentSBox[key][2]
  100.                 newSBox = append(newSBox, to_change)
  101.             }
  102.         }
  103.         finalBox = append(finalBox, newSBox)
  104.     }
  105.     for key, _ := range finalBox {
  106.         binary_value := strconv.FormatInt(int64(finalBox[key][0]), 2)
  107.        
  108.         if len(binary_value) < 4 {
  109.             for len(binary_value) < 4 {
  110.                 binary_value = "0" + binary_value
  111.             }
  112.         }
  113.         for key, _ := range string(binary_value) {
  114.             output = append(output, string(binary_value[key]))
  115.         }
  116.     }
  117.     return output
  118.  
  119. }
  120.  
  121. func permutation(sBox[]string) []string{
  122.     var permutatedKey []string = make([]string, 32)
  123.     permutationTable := [32]int{15, 6, 19, 20, 28, 11, 27, 16, 0, 14, 22, 25, 4, 17, 30, 9, 1, 7, 23, 13, 31, 26, 2, 8, 18, 12, 29, 5, 21, 10, 3, 24}
  124.  
  125.     for key, item := range sBox{
  126.         permutatedKey[permutationTable[key]] = item
  127.     }
  128.     return permutatedKey
  129. }
  130.  
  131. func main () {
  132.     var key = []string{"0", "0", "0", "0", "0", "0", "0", "0", "1", "1", "1", "1", "1", "1", "1", "1", "0", "0", "1", "0", "1", "0", "1", "0", "0", "1", "1", "1", "0", "0", "1", "1",
  133.     }
  134.     expended := expansion(key)
  135.     fmt.Println(expended)
  136.     xored := bitwise_xor(expended)
  137.     fmt.Println(xored)
  138.     sboxed := s_box_generation(xored)
  139.     fmt.Println(sboxed)
  140.     permutated := permutation(sboxed)
  141.     fmt.Println(permutated)
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement