350 posts
Posted 31 March 2014 - 10:38 PM
Hi everyone! :D/>
I am trying to make a encryption/decryption program for windows.
Some code is done and because i am new to vb.net i took this function from a website:Spoiler
Function EncryptDecrypt(ByVal text1 As String, ByVal key As String, ByVal isEncrypt As Boolean) As String
Dim char1 As String
Dim char2 As String
Dim cKey As Byte
Dim strLength As Integer
Dim Result As String = ""
Dim j As Integer = -1
If text1 <> "" And IsNumeric(key) Then
strLength = text1.Length
For i As Integer = 0 To strLength - 1
char1 = text1.Substring(i, 1)
If j < key.Length - 1 Then
j = j + 1
Else
j = 0
End If
cKey = Val(key.Substring(j, 1))
If isEncrypt Then
If (Asc(char1) + cKey) > 255 Then
char2 = Chr(Asc(char1) + cKey - 255)
Else
char2 = Chr(Asc(char1) + cKey)
End If
Else
If (Asc(char1) - cKey) < 1 Then
char2 = Chr(Asc(char1) - cKey + 255)
Else
char2 = Chr(Asc(char1) - cKey)
End If
End If
Result &= char2
Next
Else
MsgBox("Enter text or key!")
End If
Return Result
End Function
The function works great and the program is just like i want.Now heres my problem: My friend uses a tablet (android) and i want to make this app to android as well (not for selling or to be at the store, just for our group of friends and my family).I already have eclipse with the android SDK and everything needed (i did a test program with a simple tring displayed on the tablet), but i cant do this app becasue i am really noob at java. :P/> Can anyone do the code above work for java, or if someone can help me doing this app?I know that i may be wanting too much without learning and experimenting first but it would be really apreciated :)/>
Edited on 03 April 2014 - 04:25 PM
264 posts
Location
Where you aren't
Posted 01 April 2014 - 12:39 AM
This is actually just a really confusing implementation of ROT. Here's a direct JS port:
Spoiler
function EnDe (text1, key, isEncrypt) {
var char1, char2, cKey, strLength;
function Asc(thing) {
return thing.charCodeAt(0);
}
var Result = "";
var j = -1;
if (text1.length >= 1 && !isNaN(parseInt(key))) {
strLength = text1.length;
for (var i=0; i<=strLength-1 ;i++) {
char1 = text1.substr(i, 1);
if (j < key.length -1) {
j = j + 1;
} else {
j = 0;
}
cKey = parseInt(key.substr(j, 1));
if (isEncrypt) {
if ((Asc(char1) + cKey) > 255) {
char2 = String.fromCharCode(Asc(char1) + cKey - 255);
} else {
char2 = String.fromCharCode(Asc(char1) + cKey);
}
} else {
if ((Asc(char1) - cKey) < 1) {
char2 = String.fromCharCode(Asc(char1) - cKey - 255);
} else {
char2 = String.fromCharCode(Asc(char1) - cKey);
}
}
Result += char2;
}
}
return Result;
}
And here's a rewritten JS version that isn't stupid:
https://gist.github....kesonxx/9904781
195 posts
Location
Cambridgeshire, England
Posted 01 April 2014 - 09:14 AM
This is actually just a really confusing implementation of ROT. Here's a direct JS port:
Firstly he was after Java not javascript (the 2 contrary to name are not related).
And that is most certainly not ROT. Rot would shift all characters in a given string by n places, typically used with 13 on upper case only text. This shifts each character by the value of the corresponding character in the key with a modulo for where the values overflow. Closest I can think of from the top of my head that also does this is Solitaire but that also has some complexities of its own.
Problem with this is that if 2 messages are sent using the same key it does become a rather trivial operation to reverse engineer the key and from that the messages.
7083 posts
Location
Tasmania (AU)
Posted 01 April 2014 - 01:02 PM
Here's a stab at it; I've not tested it, as I've not played with Java for a little while and am curious as to whether I can still get something this simple right without the help of a compiler. ;)/>
Spoiler
public static string Decrypt(String text1, String key, boolean isEncrypt) {
int j = -1;
char[] textChar = text1.toCharArray(), keyChar = key.toCharArray();
for (int i=0;i<keyChar.length;i++) if (keyChar[i] < 48 || keyChar[i] > 57) {
System.out.println("Warning: Decrypt function called with invalid key.");
return "";
} else keyChar[i] -= 48;
if (textChar.length > 0 && keyChar.length > 0) {
for (int i=0;i<textChar.length;i++) {
j++;
if (j == keyChar.length) j = 0;
if (isEncrypt) {
if (textChar[i] + keyChar[j] > 255) {
textChar[i] += keyChar[j] - 255;
} else {
textChar[i] += keyChar[j];
}
} else {
if (textChar[i] - keyChar[j] < 1) {
textChar[i] -= keyChar[j] + 255;
} else {
textChar[i] -= keyChar[j];
}
}
}
} else {
System.out.println("Warning: Decrypt function called with empty text.");
return "";
}
return String.valueOf(textChar);
}
There may be some "loss of precision" errors lurking in there, but they should be easy to sort out if so.
350 posts
Posted 01 April 2014 - 06:23 PM
Here's a stab at it; I've not tested it, as I've not played with Java for a little while and am curious as to whether I can still get something this simple right without the help of a compiler. ;)/>
Spoiler
public static string Decrypt(String text1, String key, boolean isEncrypt) {
int j = -1;
char[] textChar = text1.toCharArray(), keyChar = key.toCharArray();
for (int i=0;i<keyChar.length;i++) if (keyChar[i] < 48 || keyChar[i] > 57) {
System.out.println("Warning: Decrypt function called with invalid key.");
return "";
} else keyChar[i] -= 48;
if (textChar.length > 0 && keyChar.length > 0) {
for (int i=0;i<textChar.length;i++) {
j++;
if (j == keyChar.length) j = 0;
if (isEncrypt) {
if (textChar[i] + keyChar[j] > 255) {
textChar[i] += keyChar[j] - 255;
} else {
textChar[i] += keyChar[j];
}
} else {
if (textChar[i] - keyChar[j] < 1) {
textChar[i] -= keyChar[j] + 255;
} else {
textChar[i] -= keyChar[j];
}
}
}
} else {
System.out.println("Warning: Decrypt function called with empty text.");
return "";
}
return String.valueOf(textChar);
}
There may be some "loss of precision" errors lurking in there, but they should be easy to sort out if so.
Java is case sensitive right? Your code had string instead of String :P/> (or is just me being stupid? XD)
Edited on 01 April 2014 - 04:24 PM
7083 posts
Location
Tasmania (AU)
Posted 01 April 2014 - 10:36 PM
Indeed, that'd need to be switched to an upper-case S.
350 posts
Posted 01 April 2014 - 10:47 PM
Indeed, that'd need to be switched to an upper-case S.
Its already done :)/> but no more errors in your code, good job with no compiler :D/>
The code is working fine and just like i expected, thanks. Now i will see if zekesonxx can help me further with the android part.
Or if someone can:
I can't get a input to be stored in a variable O.o one of the most simple things and i can't…
Spoiler
final EditText userinputET = (EditText) findViewById(R.id.txtin);
final String in = userinputET.getText().toString();
Button mbtn = (Button) findViewById(R.id.btn);
//final Button btn = (Button) findViewById(R.id.btn);
mbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Context context = getApplicationContext();
Toast toast = Toast.makeText(context ,in , 3);
toast.show();
}
});
I put the toast to test the string but so far i only go a blank toast… Any ideas of what's the matter?
Edited on 01 April 2014 - 08:48 PM
264 posts
Location
Where you aren't
Posted 02 April 2014 - 02:08 AM
This is actually just a really confusing implementation of ROT. Here's a direct JS port:
Firstly he was after Java not javascript (the 2 contrary to name are not related).
And that is most certainly not ROT. Rot would shift all characters in a given string by n places, typically used with 13 on upper case only text. This shifts each character by the value of the corresponding character in the key with a modulo for where the values overflow. Closest I can think of from the top of my head that also does this is Solitaire but that also has some complexities of its own.
Problem with this is that if 2 messages are sent using the same key it does become a rather trivial operation to reverse engineer the key and from that the messages.
I'm fully aware that JavaScript and Java are two different languages, I mock people for thinking that all the time.
And it is ROT, just not how it normally works. Instead of being based on the alphabet (abcd…wxyz) it's based on ASCII character codes. Let's say your key is "435" and your text is "this will become rotated". This is how the text will be rotated:
t h i s w i l l b e c o m e r o t a t e d
4 3 5 4 3 5 4 3 5 4 3 5 4 3 5 4 3 5 4 3 5 4 3 5
The ASCII code for "t" is 116. Add four to that, and you get 120. The ASCII character at number 120 is "x".
"h"->104->107->"k"
"i"->105->110->"n"
…and so on.
350 posts
Posted 02 April 2014 - 05:38 PM
The function that bomb bloke wrote works fine but i can't use it in my code.
Is there anyone who already devolped an app or that know about the android app devolpment? I mean, i did everything like it should be and i tried various ways.
If somone can help that would be really apreciated :)/>
And thanks to the ones that are already helping me :)/>