One, des encryption:
First, the web search for a DES encryption algorithm tool class:
Import java.security.*;
Import javax.crypto.*;
public class Deshelper {
private static String Strdefaultkey = "national";
Private Cipher encryptcipher = null;
Private Cipher decryptcipher = null;
/**
* Convert a byte array to a string representing 16 binary values, such as: byte[]{8,18} to: 0813, and public static byte[]
* Hexstr2bytearr (String strin) mutually reversible conversion process
*
* @param ARRB
* Byte array to convert
* @return The converted String
* @throws Exception
* This method does not handle any exceptions, all exceptions are thrown
*/
public static String Bytearr2hexstr (byte[] arrb) throws Exception {
int ilen = Arrb.length;
Each byte is represented by two characters, so the length of the string is twice times the length of the array
StringBuffer sb = new StringBuffer (Ilen * 2);
for (int i = 0; i < Ilen; i++) {
int inttmp = Arrb[i];
Convert negative numbers to positive numbers
while (Inttmp < 0) {
inttmp = inttmp + 256;
}
Numbers less than 0F need to be preceded by 0
if (Inttmp < 16) {
Sb.append ("0");
}
Sb.append (Integer.tostring (inttmp, 16));
}
return sb.tostring ();
}
/**
* Converts a String representing 16 binary values to a byte array, and public static string Bytearr2hexstr (byte[] arrb)
* Reciprocal Reversible conversion process
*
* @param strin
* Strings that need to be converted
* @return converted byte array
* @throws Exception
* This method does not handle any exceptions, all exceptions are thrown
* @author <a href= "Mailto:[email protected]" >LiGuoQing</a>
*/
public static byte[] Hexstr2bytearr (String strin) throws Exception {
byte[] Arrb = Strin.getbytes ();
int ilen = Arrb.length;
Two characters represent one byte, so the length of the byte array is the length of the string divided by 2
byte[] Arrout = new BYTE[ILEN/2];
for (int i = 0; i < ilen; i = i + 2) {
String strtmp = new String (ARRB, I, 2);
ARROUT[I/2] = (byte) integer.parseint (strtmp, 16);
}
return arrout;
}
/**
* Default constructor method, use default key
*
* @throws Exception
*/
Public Deshelper () throws Exception {
This (Strdefaultkey);
}
/**
* Specify key construction methods
*
* @param strkey
* The specified key
* @throws Exception
*/
Public Deshelper (String strkey) throws Exception {
Security.addprovider (New Com.sun.crypto.provider.SunJCE ());
Key key = GetKey (Strkey.getbytes ());
Encryptcipher = Cipher.getinstance ("DES");
Encryptcipher.init (Cipher.encrypt_mode, key);
Decryptcipher = Cipher.getinstance ("DES");
Decryptcipher.init (Cipher.decrypt_mode, key);
}
/**
* Encrypt byte array
*
* @param ARRB
* Byte array to be encrypted
* @return the encrypted byte array
* @throws Exception
*/
Public byte[] Encrypt (byte[] arrb) throws Exception {
Return encryptcipher.dofinal (ARRB);
}
/**
* Encrypt string
*
* @param strin
* Strings to be encrypted
* @return The encrypted string
* @throws Exception
*/
public string Encrypt (string strin) throws Exception {
Return Bytearr2hexstr (Encrypt (Strin.getbytes ()));
}
/**
* Decrypt byte array
*
* @param ARRB
* Byte array to decrypt
* @return the decrypted byte array
* @throws Exception
*/
Public byte[] Decrypt (byte[] arrb) throws Exception {
Return decryptcipher.dofinal (ARRB);
}
/**
* Decrypt string
*
* @param strin
* The string to be decrypted
* @return The decrypted string
* @throws Exception
*/
public string Decrypt (string Strin) throws Exception {
return new String (Decrypt (Hexstr2bytearr (Strin)));
}
/**
* Generate a key from the specified string, the required byte array length for the key is 8 bits less than 8 bits when the back 0, more than 8 bits only take the first 8 bits
*
* @param arrbtmp
* The byte array that makes up the string
* @return The generated key
* @throws java.lang.Exception
*/
Private Key GetKey (byte[] arrbtmp) throws Exception {
Create an empty 8-byte array (default is 0)
byte[] ARRB = new Byte[8];
Converts the original byte array to 8 bits
for (int i = 0; i < arrbtmp.length && i < arrb.length; i++) {
Arrb[i] = Arrbtmp[i];
}
Generate key
Key key = new Javax.crypto.spec.SecretKeySpec (ARRB, "DES");
Return key;
}
}
How to use the DES encryption algorithm:
Here's what I do when I'm developing Android, which involves encrypting the data to the server-side, including the image attachment, to the part of the code.
File File = new file (path);
FileInputStream Stream;
try {
stream = new FileInputStream (file);
int filesize = stream.available ();
SYSTEM.OUT.PRINTLN ("Attachment size:" +filesize);
if (filesize>maxfile) {
Attachment.seterror ("Attachments cannot be greater than 2m!");
Return
}
byte[] bytes = new Byte[filesize];
Stream.read (bytes);//The file must be converted to a byte array before transmitting the attachment.
Deshelper des;
try {
des = new Deshelper ("test");//Custom key
Byte[] encodebytes =des.encrypt (bytes);//When encrypting with the DES algorithm, first encrypt the byte array and then turn to the Bytearr2hexstr method of the DES encryption algorithm to convert the byte array to a string of 16 binary values. The string conversion method of the new string (encodebytes) cannot be used here directly.
String image = Des.bytearr2hexstr (encodebytes);
Jsonobject json = new Jsonobject ();
Json.put ("name", "Henry");
Json.put ("Selectedtypeid", Selectedtypeid);
Json.put ("BeginTime", beginTime);
Json.put ("Reason", reason);
Json.put ("image", image);
String str = json.tostring ();//Convert to JSON string for easy encrypted transmission
SYSTEM.OUT.PRINTLN ("Size of the string to be encrypted:" +str.getbytes (). length);
SimpleDateFormat format1=new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss:SSS");
System.out.println ("String before Encryption:" +STR);
System.out.println ("Start encryption Time:" +format1.format (new Date ()));
String encodestr =des.encrypt (str);
SYSTEM.OUT.PRINTLN ("End Encryption Time:" +format1.format (New Date ()));
System.out.println ("Encrypted string:" +encodestr);
System.out.println ("string size after Encryption:" +encodestr.getbytes (). length);
/************************************** decryption Process **********************************************/
System.out.println ("Start decryption Time:" +format1.format (New Date ()));
String Decodestr=des.decrypt (ENCODESTR);
System.out.println ("End decryption Time:" +format1.format (New Date ()));
System.out.println ("Decrypted string:" +decodestr);
Jsonobject object = new Jsonobject (DECODESTR);
string img = (string) object.get ("image");
Byte[] getbyte =des.hexstr2bytearr (IMG);
You cannot use the Img.getbytes () method here to get the byte array as a parameter of the decrypt method, you must first convert the string img of the 16 binary value to a byte array, calling the Hexstr2bytearr method
Byte[] images= des.decrypt (getbyte);
System.out.println ("Length:" +images.length);
Read the decrypted picture file
Bitmap Bitmap =bitmapfactory.decodebytearray (images, 0, images.length);
。。。。。。
} catch (Exception E1) {
E1.printstacktrace ();
}
}catch (FileNotFoundException E1) {
E1.printstacktrace ();
} catch (IOException e) {
E.printstacktrace ();
} catch (Org.apache.http.ParseException e) {
E.printstacktrace ();
}
If you use BASE64 encryption, it is relatively simple:
Call Android's own tool class Android.util.Base64
To encrypt a byte array
byte[] encode = Base64.encode (bytes, base64.default);
Convert to string for transfer
String image= new string (encode);
To decrypt a string and convert it into a byte array
byte[] Images =base64.decode (image.getbytes (), base64.default);