No edit summary |
No edit summary |
||
Line 149: | Line 149: | ||
Go version by mrngm : | Go version by mrngm : | ||
<pre> | <pre>package main | ||
import ( | |||
"fmt" | |||
) | |||
func Encrypt(s string) string { | |||
encLen := len(s) | |||
encBytes := []byte(s) | |||
for i := 0; i < encLen; i++ { | |||
oidx := offsetIndex(i + 3) | |||
newpos := oidx % encLen | |||
// fmt.Printf("i: %d (oidx: %d), newpos: %d\n", i, oidx, newpos) | |||
encBytes[newpos], encBytes[i] = encBytes[i], encBytes[newpos] | |||
} | |||
return string(encBytes) | |||
} | |||
func Decrypt(s string) string { | |||
decLen := len(s) | |||
decBytes := []byte(s) | |||
for i := decLen - 1; i >= 0; i-- { | for i := decLen - 1; i >= 0; i-- { | ||
oidx := offsetIndex(i + 3) | oidx := offsetIndex(i + 3) |
Revision as of 07:27, 7 July 2020
EnvisionTEC Ultra Control has a 'Service Mode' that you can activate by entering a 'User code' based off the 'App code' displayed in the program. These are tools to create a valid 'User code' for entering 'Service Mode'. (Only the C++ version has been tested)
C version by Wilco :
#include <stdio.h> #include <stdlib.h> #include <string.h> void SwapCharacter(char * csString, int iPosA, int iPosB) { char ch1 = csString[iPosA]; char ch2 = csString[iPosB]; csString[iPosA] = ch2; csString[iPosB] = ch1; } int GetNextReturn(int iInitialIndex) { int num; do { ++iInitialIndex; num = 2; while (num < iInitialIndex && iInitialIndex % num != 0) ++num; } while (num != iInitialIndex); return iInitialIndex; } int GetPrimaryIndex(int iIndex) { int iInitialIndex = 1; for (int index = 1; index < iIndex; ++index) iInitialIndex = GetNextReturn(iInitialIndex); return iInitialIndex; } char * Decrypt(char * csString) { for (int index = strlen(csString) - 1; index >= 0; --index) { int iPosB = index; int iPosA = GetPrimaryIndex(index + 3) % strlen(csString); SwapCharacter(csString, iPosA, iPosB); } return csString; } char * Encrypt(char * csString) { for (size_t index = 0; index < strlen(csString); ++index) { int iPosB = index; int iPosA = GetPrimaryIndex(index + 3) % strlen(csString); SwapCharacter(csString, iPosA, iPosB); } return csString; } int main(int argc, char **argv) { char str[16] = "test"; if (argc != 2) { fprintf(stderr, "Syntax: %s encrypted\n", argv[0]); exit(1); } strncpy(str, argv[1], sizeof(str)); str[sizeof(str)-1] = '\0'; Decrypt(str); printf(str); }
C++ version by RSpliet :
#include <string> #include <iostream> using namespace std; class crypt { private: void SwapCharacter(string &csString, int iPosA, int iPosB) { char ch1 = csString[iPosA]; char ch2 = csString[iPosB]; csString[iPosA] = ch2; csString[iPosB] = ch1; } int GetNextReturn(int iInitialIndex) { int num; do { ++iInitialIndex; num = 2; while (num < iInitialIndex && iInitialIndex % num != 0) ++num; } while (num != iInitialIndex); return iInitialIndex; } int GetPrimaryIndex(int iIndex) { int iInitialIndex = 1; for (int index = 1; index < iIndex; ++index) iInitialIndex = GetNextReturn(iInitialIndex); return iInitialIndex; } public: string Decrypt(string csString) { for (int index = csString.length() - 1; index >= 0; --index) { int iPosB = index; int iPosA = GetPrimaryIndex(index + 3) % csString.length(); SwapCharacter(csString, iPosA, iPosB); } return csString; } string Encrypt(string csString) { for (int index = 0; index < csString.length(); index++) { int iPosB = index; int iPosA = GetPrimaryIndex(index + 3) % csString.length(); SwapCharacter(csString, iPosA, iPosB); } return csString; } }; int main(int argc, char **argv) { crypt C; string cs = C.Encrypt("piehole"); string ds = C.Decrypt(cs); cout << cs << endl; cout << ds << endl; return 0; }
Go version by mrngm :
package main import ( "fmt" ) func Encrypt(s string) string { encLen := len(s) encBytes := []byte(s) for i := 0; i < encLen; i++ { oidx := offsetIndex(i + 3) newpos := oidx % encLen // fmt.Printf("i: %d (oidx: %d), newpos: %d\n", i, oidx, newpos) encBytes[newpos], encBytes[i] = encBytes[i], encBytes[newpos] } return string(encBytes) } func Decrypt(s string) string { decLen := len(s) decBytes := []byte(s) for i := decLen - 1; i >= 0; i-- { oidx := offsetIndex(i + 3) newpos := oidx % decLen // fmt.Printf("i: %d (oidx: %d), newpos: %d\n", i, oidx, newpos) decBytes[newpos], decBytes[i] = decBytes[i], decBytes[newpos] } return string(decBytes) } func offsetIndex(index int) int { initial := 1 for i := 1; i < index; i++ { initial = next(initial) } return initial } func next(initial int) int { for { initial++ num := 2 for num < initial && initial%num != 0 { num++ } if num == initial { break } } return initial } func main() { fmt.Println(Decrypt(Encrypt("abcdefghijklmnopqrstuvwxyz"))) }