Package Pioneerbarcode.ccw.com.encryptanddecode;
Import Android.os.Bundle;
Import android.support.v7.app.AppCompatActivity;
Import Android.text.TextUtils;
Import android.util.Base64;
Import Android.view.View;
Import Android.widget.Button;
Import Android.widget.TextView;
Import Pioneerbarcode.ccw.com.encryptanddecode.utils.AesUtils;
public class Mainactivity extends Appcompatactivity implements View.onclicklistener {
Private Button btnbase64new;
Private Button Btnbase64older;
Private TextView Tvolder;
Private String Strolderdata;
Private TextView Tvjiamidata;
Private TextView Tvjiemidata;
Private Button Btnaesjiami;
Private Button Btnaesjiemi;
Private TextView Tvaesjiamidata;
Private TextView Tvaesjiemidata;
private static String Aespassword = "hehehe";
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Setview ();
Setlistener ();
}
private void Setlistener () {
Btnbase64new.setonclicklistener (this);
Btnbase64older.setonclicklistener (this);
Btnaesjiami.setonclicklistener (this);
Btnaesjiemi.setonclicklistener (this);
}
private void Setview () {
Btnbase64new = (Button) Findviewbyid (R.id.btn_base64_jiemi);
Btnbase64older = (Button) Findviewbyid (R.id.btn_base64_jiami);
Tvolder = (TextView) Findviewbyid (R.id.tv_older);
Tvjiamidata = (TextView) Findviewbyid (r.id.tv_jiami_data);
Tvjiemidata = (TextView) Findviewbyid (r.id.tv_jiemi_data);
Btnaesjiami = (Button) Findviewbyid (R.id.btn_aes_jiami);
Btnaesjiemi = (Button) Findviewbyid (R.id.btn_aes_jiemi);
Tvaesjiamidata = (TextView) Findviewbyid (r.id.tv_aes_jiami_data);
Tvaesjiemidata = (TextView) Findviewbyid (r.id.tv_aes_jiemi_data);
Strolderdata = Tvolder.gettext (). toString (). Trim ();
}
@Override
public void OnClick (View v) {
Switch (V.getid ()) {
Case R.id.btn_base64_jiemi:
Base64 decryption
Base64decode ();
Break
Case R.id.btn_base64_jiami:
BASE64 encryption
Base64encrypt ();
Break
Case R.id.btn_aes_jiami:
Aes encryption
Aesencrypt ();
Break
Case R.id.btn_aes_jiemi:
Aes decryption
Aesdecode ();
Break
Default
Break
}
}
/**
* Aes Decryption
*/
private void Aesdecode () {
try {
if (! Textutils.isempty (Tvaesjiamidata.gettext (). ToString (). ToString (). Trim ())) {
Aes decrypted data
String data = Aesutils.decrypt (Aespassword, Tvaesjiamidata.gettext (). toString (). Trim ());
Tvaesjiemidata.settext (data);
}
} catch (Exception e) {
E.printstacktrace ();
}
}
/**
* Aes Encryption
*/
private void Aesencrypt () {
try {
String data = Aesutils.encrypt (Aespassword, strolderdata);
After the encrypted string
Tvaesjiamidata.settext (data);
} catch (Exception e) {
E.printstacktrace ();
}
}
/**
* Base64 Decryption
*/
private void Base64decode () {
if (! Textutils.isempty (Tvjiamidata.gettext (). toString (). Trim ())) {
String data = new String (Base64.decode (Tvjiamidata.gettext (). toString (). Trim (). GetBytes (), Base64.default));
Tvjiemidata.settext (data);
}
}
/**
* Base64 Encryption
*/
private void Base64encrypt () {
Here are two ways of encrypting
Here is the Encode method, which returns a byte type of encrypted data that can be converted to a string type using the new string
String olderdata_base64 = new String (Base64.encode (Strolderdata.getbytes (), Base64.default));
Encrypted incoming data is of type Byte, instead of using the Decode method to convert raw data to binary, string-type data uses str.getbytes () to
String olderdata_base64 = base64.encodetostring (Strolderdata.getbytes (), base64.default);
Tvjiamidata.settext (OLDERDATA_BASE64);
}
}
public class Aesutils {
public static string Encrypt (string seed, String cleartext) throws Exception {
byte[] Rawkey = Getrawkey (Seed.getbytes ());
Byte[] result = Encrypt (Rawkey, cleartext.getbytes ());
return Tohex (Result);
}
public static string decrypt (string seed, string encrypted) throws Exception {
byte[] Rawkey = Getrawkey (Seed.getbytes ());
byte[] enc = tobyte (encrypted);
Byte[] result = Decrypt (rawkey, ENC);
return new String (result);
}
private static byte[] Getrawkey (byte[] seed) throws Exception {
Keygenerator KGen = keygenerator.getinstance ("AES");
securerandom sr = securerandom.getinstance ("Sha1prng", "Crypto");
Sr.setseed (seed);
Kgen.init (+, SR); 192 and available
Secretkey skey = Kgen.generatekey ();
Byte[] raw = skey.getencoded ();
return raw;
}
private static byte[] Encrypt (byte[] raw, byte[] clear) throws Exception {
Secretkeyspec Skeyspec = new Secretkeyspec (Raw, "AES");
Cipher Cipher = cipher.getinstance ("AES");
Cipher.init (Cipher.encrypt_mode, Skeyspec,new ivparameterspec (New Byte[cipher.getblocksize ()));
Byte[] Encrypted = cipher.dofinal (clear);
return encrypted;
}
private static byte[] Decrypt (byte[] raw, byte[] encrypted) throws Exception {
Secretkeyspec Skeyspec = new Secretkeyspec (Raw, "AES");
Cipher Cipher = cipher.getinstance ("AES");
Cipher.init (Cipher.decrypt_mode, Skeyspec,new ivparameterspec (New Byte[cipher.getblocksize ()));
byte[] decrypted = cipher.dofinal (encrypted);
return decrypted;
}
private static string Tohex (String txt) {
Return Tohex (Txt.getbytes ());
}
private static string Fromhex (String hex) {
return new String (ToByte (hex));
}
private static byte[] ToByte (String hexstring) {
int len = Hexstring.length ()/2;
Byte[] result = new Byte[len];
for (int i = 0; i < len; i++)
Result[i] = integer.valueof (hexstring.substring (2*i, 2*i+2), +). Bytevalue ();
return result;
}
private static String Tohex (byte[] buf) {
if (buf = = null)
Return "";
StringBuffer result = new StringBuffer (2*buf.length);
for (int i = 0; i < buf.length; i++) {
Appendhex (result, buf[i]);
}
return result.tostring ();
}
Private final static String HEX = "0123456789ABCDEF";
private static void Appendhex (StringBuffer sb, byte b) {
Sb.append (Hex.charat ((b>>4) &0x0f)). Append (Hex.charat (b&0x0f));
}
}
Verified available
Android Base64 and AES Encryption decryption