Last active
January 2, 2021 14:13
-
-
Save fachryansyah/5b54749b1cb29ecb64ea7e9ee25fad02 to your computer and use it in GitHub Desktop.
Hidding Message Steganografi
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// MIT License | |
// Copyright (c) [2020] [fachryansyah] | |
// Permission is hereby granted, free of charge, to any person obtaining a copy | |
// of this software and associated documentation files (the "Software"), to deal | |
// in the Software without restriction, including without limitation the rights | |
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
// copies of the Software, and to permit persons to whom the Software is | |
// furnished to do so, subject to the following conditions: | |
// The above copyright notice and this permission notice shall be included in all | |
// copies or substantial portions of the Software. | |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
// SOFTWARE. | |
/** | |
How the algorithm works | |
1. Convert decimal number to binary | |
every points of input number, you need changing it to the binary and than get the last number of that binary. | |
for example i have "55" decimal number, next changing it to binary and i should get "110111" binary numbers, so the result is "1" because the last number of that binary is "1". | |
2. Repeat the step no.1 for up to 7 points of number | |
do step no.1 for 7 times, example i have "31 31 6 6 51 51 6" decimal number, and i will get "1100110" | |
3. Translate the result of no.2 to words | |
in the problem stated that a-z is 97-122 in decimal number, so now we have the pattern like this: | |
97 = A | |
98 = B | |
99 = C | |
etc.. | |
previously I had a value of "1100110" in binary, we must know what letters are behind the binary numbers. | |
by changing the decimal value 97-122 to binary, we will know the letter. | |
binary | decimal | letters | |
1100001 | 97 | A | |
1100010 | 98 | B | |
1100011 | 99 | C | |
.... | ... | ... | |
1100110 | 103 | F | |
USAGE | |
$ go run steganografi.go | |
**/ | |
package main | |
import ( | |
"fmt" | |
"strings" | |
"strconv" | |
) | |
type Dictionary struct { | |
Name string | |
Binary string | |
} | |
func main () { | |
char := "31 31 6 6 51 51 6 23 25 8 4 2 6 31 61 43 8 21 18 20 42 43 53 15 12 56 31 34 21 49 2 55 18 22 31" // return FAHRI | |
result := steganografi(char) | |
fmt.Println(result) | |
a := "1 1 0 0 0 0 1" // return A | |
resultA := steganografi(a) | |
fmt.Println(resultA) | |
b := "31 31 20 20 20 21 8" // return B | |
resultB := steganografi(b) | |
fmt.Println(resultB) | |
c := "51 61 20 0 0 15 17" // return C | |
resultC := steganografi(c) | |
fmt.Println(resultC) | |
withSpace := "51 61 20 0 0 15 17 31 8 8 8 8 8 8 51 61 20 0 0 15 17" // return C C | |
resultSpace := steganografi(withSpace) | |
fmt.Println(resultSpace) | |
} | |
func steganografi(input string) string { | |
// prepare words | |
words := generateWordList() | |
split := strings.Split(input, " ") | |
// validate if input lower than 7 | |
if len(split) < 7 { | |
return "Input tidak kurang dari 7" | |
} | |
// validate if input more than 7000 | |
if len(split) > 7000 { | |
return "Nilai tidak memiliki kelipatan 7" | |
} | |
// validate if input length not multiples of 7 | |
if len(split) % 7 != 0 { | |
return "Nilai tidak memiliki kelipatan 7" | |
} | |
var binary []string | |
var result string | |
// loop the number | |
temp := "" | |
for i := 0; i < len(split); i++ { | |
// if temp has 7 character, it will be push to array of binary | |
if i % 7 == 0 { | |
binary = append(binary, temp) | |
temp = "" | |
} | |
// convert the number to int64 | |
s, _ := strconv.ParseInt(split[i], 10, 64) | |
// convert again from decimal to binary | |
a := decimalToBinary(s) | |
// get the last number and append to "temp" variable | |
temp += a[len(a)-1:] | |
if i+1 == len(split) { | |
binary = append(binary, temp) | |
temp = "" | |
} | |
} | |
// loop the binary | |
for i := 0; i < len(binary); i++ { | |
// loop the words from dictionary | |
for _, item := range words { | |
// check if binary is equal from dictionary | |
if item.Binary == binary[i] { | |
result += item.Name | |
} | |
} | |
} | |
return result | |
} | |
func decimalToBinary(num int64) string { | |
return strconv.FormatInt(num, 2) | |
} | |
func generateWordList() []Dictionary { | |
var dictionaries []Dictionary | |
// set default words | |
var words = [...]string{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", | |
"N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"} | |
// in the problem we have a-z with values 97 to 122, 97 means the letter A | |
for i := 97; i <= 122; i++ { | |
// apend the world bassed on index | |
dictionaries = append(dictionaries, Dictionary{words[i-97], decimalToBinary(int64(i))}) | |
} | |
// this is default for whitespace | |
dictionaries = append(dictionaries, Dictionary{" ", "1000000"}) | |
return dictionaries | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment