Consult the AES encryption and decryption under Android (encryption, decryption

The use of online general encryption and decryption algorithm, I test the encryption is normal, but use the decryption error. What is the reason.
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);
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);
byte[] decrypted = cipher.doFinal(encrypted);
return decrypted;
}

Started by Lyle at November 29, 2016 - 6:22 AM

/** The following is the DES algorithm package */
package security;

import java.security.Key;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class DES {
private Key key;

public DES(String strkey) {
setKey(strkey);
}

public DES() {
setKey("areful");
}

/**
* Gets the key
*/
public String getKey() throws Exception {
return Base64Utils.encode(key.getEncoded());
}

/**
* According to the parameter generation KEY
*/
public void setKey(String strKey) {
try {
KeyGenerator _generator = KeyGenerator.getInstance("DES");
_generator.init(new SecureRandom(strKey.getBytes()));
this.key = _generator.generateKey();
_generator = null;
} catch (Exception e) {
throw new RuntimeException(
"Error initializing SqlMap class. Cause: " + e);
}
}

/**
* String encryption plaintext ciphertext output input, String
*/
public String getEncString(String strMing) {
byte[] byteMi = null;
byte[] byteMing = null;
String strMi = "";
BASE64Encoder base64en = new BASE64Encoder();
try {
byteMing = strMing.getBytes("UTF8");
byteMi = this.getEncCode(byteMing);
strMi = base64en.encode(byteMi);
} catch (Exception e) {
throw new RuntimeException(
"Error initializing SqlMap class. Cause: " + e);
} finally {
base64en = null;
byteMing = null;
byteMi = null;
}
return strMi;
}

/**
* Decryption ciphertext to String input, String output Express
*
* @param strMi
* @return
*/
public String getDesString(String strMi) {
BASE64Decoder base64De = new BASE64Decoder();
byte[] byteMing = null;
byte[] byteMi = null;
String strMing = "";
try {
byteMi = base64De.decodeBuffer(strMi);
byteMing = this.getDesCode(byteMi);
strMing = new String(byteMing, "UTF8");
} catch (Exception e) {
throw new RuntimeException(
"Error initializing SqlMap class. Cause: " + e);
} finally {
base64De = null;
byteMing = null;
byteMi = null;
}
return strMing;
}

/**
* Encryption with byte[] inputs, byte[] ciphertext output
*
* @param byteS
* @return
*/
private byte[] getEncCode(byte[] byteS) {
byte[] byteFina = null;
Cipher cipher;
try {
cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, key);
byteFina = cipher.doFinal(byteS);
} catch (Exception e) {
throw new RuntimeException(
"Error initializing SqlMap class. Cause: " + e);
} finally {
cipher = null;
}
return byteFina;
}

/**
* Decryption ciphertext to byte[] input, byte[] output to express
*
* @param byteD
* @return
*/
private byte[] getDesCode(byte[] byteD) {
Cipher cipher;
byte[] byteFina = null;
try {
cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, key);
byteFina = cipher.doFinal(byteD);
} catch (Exception e) {
throw new RuntimeException(
"Error initializing SqlMap class. Cause: " + e);
} finally {
cipher = null;
}
return byteFina;
}
}

/** The following is a test class DES class package */
package test;

import security.DES;

public class TestDES {
public static void main(String[] args) {
TestDES t = new TestDES();

t.test();
}

private void test() {
DES des = new DES();
// Set key
// des.setKey("12345678");
String str1 = "String encryption before";
// DES encryption
String str2 = des.getEncString(str1);
String deStr = des.getDesString(str2);
System.out.println("Ciphertext:" + str2);
// DES decryption
System.out.println("Expressly:" + deStr);

}
}

Posted by Eleanor at December 05, 2016 - 6:40 AM

Ha, as DES.

Posted by Eleanor at December 18, 2016 - 7:40 AM

/** Than you, have a look what little things */

package security;

import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

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

public class AES {
/**
* Encryption
*
* @Param content encrypted content
* @Param password encrypted password.
* @return
*/
public byte[] encrypt(String content, String password) {
try {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128, new SecureRandom(password.getBytes()));
SecretKey secretKey = kgen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
Cipher cipher = Cipher.getInstance("AES");// Create a password.
byte[] byteContent = content.getBytes("utf-8");
cipher.init(Cipher.ENCRYPT_MODE, key);// Initialization
byte[] result = cipher.doFinal(byteContent);
return result; // Encryption
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return null;
}
/**Decrypt
* @Param content for decrypting content
* @Param password decryption key
* @return
*/
public byte[] decrypt(byte[] content, String password) {
try {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128, new SecureRandom(password.getBytes()));
SecretKey secretKey = kgen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
Cipher cipher = Cipher.getInstance("AES");// Create a password.
cipher.init(Cipher.DECRYPT_MODE, key);// Initialization
byte[] result = cipher.doFinal(content);
return result; // Encryption
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return null;
}
}

Posted by Eleanor at December 27, 2016 - 8:20 AM

Thank you big brother areful, but strange areful brother your code. Now do not know is what reason, check the Internet many reflect the decryption failure. But nobody can say is what reason, just learning Android, got confused and disoriented by the.

Posted by Lyle at January 05, 2017 - 9:01 AM

The landlord, your problem not solved Android decryption? ? ?

Posted by Chelsea at January 10, 2017 - 9:22 AM