Skip to content

Commit

Permalink
Encrypt with DES
Browse files Browse the repository at this point in the history
  • Loading branch information
viren-nadkarni committed May 26, 2017
1 parent 7462564 commit 48010fd
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ android {
applicationId "com.appknox.mfva"
minSdkVersion 19
targetSdkVersion 25
versionCode 4
versionCode 5
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
Expand Down
33 changes: 32 additions & 1 deletion app/src/main/java/com/appknox/mfva/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,19 @@
import android.view.View;
import android.widget.Button;

import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;


public class MainActivity extends AppCompatActivity {
Expand Down Expand Up @@ -114,7 +125,27 @@ public void onClick(View v) {
byte[] hash = md.digest(quote.getBytes());
Snackbar.make(v, "MD5:" + hash.toString() + ": " + quote, Snackbar.LENGTH_SHORT).show();
} catch (NoSuchAlgorithmException e) {
//pass
Snackbar.make(v, e.toString(), Snackbar.LENGTH_SHORT).show();
}
}
});

Button buttonEncrypt = (Button) findViewById(R.id.button_encrypt);
buttonEncrypt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
String quote = "Even if you're not doing anything wrong, you are being watched and recorded. - Edward Snowden";

SecretKey keyspec = new SecretKeySpec("Gangnam!".getBytes(), "DES");
Cipher c = Cipher.getInstance("DES/ECB/ZeroBytePadding", "BC");
c.init(Cipher.ENCRYPT_MODE, keyspec);
c.doFinal(quote.getBytes());

Snackbar.make(v, quote, Snackbar.LENGTH_SHORT).show();
} catch (NoSuchAlgorithmException | NoSuchProviderException | NoSuchPaddingException | BadPaddingException |
IllegalBlockSizeException | InvalidKeyException e) {
Snackbar.make(v, e.toString(), Snackbar.LENGTH_SHORT).show();
}
}
});
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,10 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/button_calculate_checksum"/>

<Button
android:id="@+id/button_encrypt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/button_encrypt"/>
</LinearLayout>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<string name="button_api_requests">API requests</string>
<string name="button_start_api_requests">Start API requests</string>
<string name="button_calculate_checksum">Calculate checksum with MD5</string>
<string name="button_encrypt">Encrypt with DES/ECB</string>

<string name="permission_custom">Custom Permission</string>
</resources>

0 comments on commit 48010fd

Please sign in to comment.