Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ CipherTxt/out
*/*.iml
.idea
gen
out/*
2 changes: 1 addition & 1 deletion CipherTxt/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
package="com.joshuahou.ciphertxt"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true">
<application android:icon="@drawable/icon" android:label="@string/app_name">


<activity android:name="com.joshuahou.ciphertxt.CipherTxtMain">
Expand Down
Binary file modified CipherTxt/res/drawable-hdpi/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified CipherTxt/res/drawable-ldpi/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified CipherTxt/res/drawable-mdpi/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 6 additions & 3 deletions CipherTxt/res/layout/main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,17 @@
<Button android:id="@+id/encryptbutton"
android:text="@string/encrypt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
android:layout_height="wrap_content"
android:onClick="encryptButtonClick"/>
<Button android:id="@+id/decryptbutton"
android:text="@string/decrypt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
android:layout_height="wrap_content"
android:onClick="decryptButtonClick"/>
<Button android:id="@+id/copybutton"
android:text="@string/copy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
android:layout_height="wrap_content"
android:onClick="copyButtonClick"/>
</LinearLayout>
</LinearLayout>
64 changes: 28 additions & 36 deletions CipherTxt/src/com/joshuahou/ciphertxt/CipherTxtMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,33 @@
import android.app.Activity;
import android.os.Bundle;
import android.text.ClipboardManager;
import android.util.Base64;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class CipherTxtMain extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}

final EditText editText = (EditText) findViewById(R.id.message);
final Button encryptButton = (Button) findViewById(R.id.encryptbutton);
encryptButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
String password = ((EditText) findViewById(R.id.password)).getText().toString();
editText.setText(encrypt(password, editText.getText().toString()));
}
});

final Button decryptButton = (Button) findViewById(R.id.decryptbutton);
decryptButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
String password = ((EditText) findViewById(R.id.password)).getText().toString();
editText.setText(decrypt(password, editText.getText().toString()));
}
});
public void decryptButtonClick(View button) {
EditText editText = (EditText) findViewById(R.id.message);
String password = ((EditText) findViewById(R.id.password)).getText().toString();
editText.setText(decrypt(password, editText.getText().toString()));
}

final ClipboardManager clipboardManager = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
final Button copyButton = (Button) findViewById(R.id.copybutton);
copyButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
clipboardManager.setText(editText.getText());
}
});
public void encryptButtonClick(View button) {
EditText editText = (EditText) findViewById(R.id.message);
String password = ((EditText) findViewById(R.id.password)).getText().toString();
editText.setText(encrypt(password, editText.getText().toString()));
}

public void copyButtonClick(View button) {
EditText editText = (EditText) findViewById(R.id.message);
ClipboardManager clipboardManager = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
clipboardManager.setText(editText.getText());
}

private boolean validate(String password, String message) {
Expand All @@ -56,31 +48,31 @@ private String encrypt(String password, String message) {
if (!validate(password, message)) {
return message;
}
return String.format("(%s)", xor(password, message));
String encryptedMessage = Base64.encodeToString(xor(message.getBytes(), password.getBytes()), Base64.NO_WRAP);
return String.format("%s", encryptedMessage);
}

private String decrypt(String password, String message) {
if (!validate(password, message)) {
return message;
}
String encrypted = message.trim();
if (encrypted.charAt(0) != '(' || encrypted.charAt(encrypted.length() - 1) != ')') {

try {
byte[] decoded = Base64.decode(encrypted, Base64.NO_WRAP);
return new String(xor(decoded, password.getBytes()));
} catch (IllegalArgumentException e) {
Toast.makeText(CipherTxtMain.this, "Improperly formatted message block.", Toast.LENGTH_SHORT).show();
return message;
}

encrypted = encrypted.substring(1, encrypted.length() - 1);
return xor(password, encrypted);
}

private String xor(String password, String message) {
byte[] messageBytes = message.getBytes();
byte[] passwordBytes = password.getBytes();
byte[] encryptedBytes = new byte[messageBytes.length];
private byte[] xor(byte[] message, byte[] password) {
byte[] out = new byte[message.length];

for (int i = 0; i < messageBytes.length; i++) {
encryptedBytes[i] = (byte) (messageBytes[i] ^ passwordBytes[i % passwordBytes.length]);
for (int i = 0; i < message.length; i++) {
out[i] = (byte) (message[i] ^ password[i % password.length]);
}
return new String(encryptedBytes);
return out;
}
}