app直播源码,android AES加密解密实现
app直播源码,android AES加密解密实现
import android.util.Base64;
import android.util.Log;
import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.IvParameterSpec;
/**
* Created by Jane on 2017/10/10.
*/
public class CryptoTools {
public static final String keyStr = "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4";
private static byte[] key= Base64.decode(keyStr.getBytes(),Base64.DEFAULT);
private static byte[] keyiv = { 1, 2, 3, 4, 5, 6, 7, 8 };
/**
* ECB加密,不要IV
* @param data 明文
* @return Base64编码的密文
* @throws Exception
*/
public static byte[] des3EncodeECB(byte[] data)
throws Exception {
Key deskey = null;
DESedeKeySpec spec = new DESedeKeySpec(key);
SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
deskey = keyfactory.generateSecret(spec);
Cipher cipher = Cipher.getInstance("desede" + "/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, deskey);
byte[] bOut = cipher.doFinal(data);
return bOut;
}
/**
* ECB解密,不要IV
* @param data Base64编码的密文
* @return 明文
* @throws Exception
*/
public static byte[] ees3DecodeECB(byte[] data)
throws Exception {
Key deskey = null;
DESedeKeySpec spec = new DESedeKeySpec(key);
SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
deskey = keyfactory.generateSecret(spec);
Cipher cipher = Cipher.getInstance("desede" + "/ECB/PKCS5Padding");
Log.i("des", "decode init before");
cipher.init(Cipher.DECRYPT_MODE, deskey);
Log.i("des", "decode init after" + new String(data,"UTF-8"));
byte[] bOut = cipher.doFinal(data);
Log.i("des", "decode doFinal after");
return bOut;
}
/**
* CBC加密
* @param data 明文
* @return Base64编码的密文
* @throws Exception
*/
public static byte[] des3EncodeCBC( byte[] data)
throws Exception {
Key deskey = null;
DESedeKeySpec spec = new DESedeKeySpec(key);
SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
deskey = keyfactory.generateSecret(spec);
Cipher cipher = Cipher.getInstance("desede" + "/CBC/PKCS5Padding");
IvParameterSpec ips = new IvParameterSpec(keyiv);
cipher.init(Cipher.ENCRYPT_MODE, deskey, ips);
byte[] bOut = cipher.doFinal(data);
return bOut;
}
/**
* CBC解密
* @param data Base64编码的密文
* @return 明文
* @throws Exception
*/
public static byte[] des3DecodeCBC(byte[] data)
throws Exception {
Key deskey = null;
DESedeKeySpec spec = new DESedeKeySpec(key);
SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
deskey = keyfactory.generateSecret(spec);
Cipher cipher = Cipher.getInstance("desede" + "/CBC/PKCS5Padding");
IvParameterSpec ips = new IvParameterSpec(keyiv);
cipher.init(Cipher.DECRYPT_MODE, deskey, ips);
byte[] bOut = cipher.doFinal(data);
return bOut;
}
}
加密:
byte[] temp = CryptoTools.des3EncodeECB(getString(R.string.app_url_use).getBytes("UTF-8"));
String message = new String(Base64.encode(temp, Base64.DEFAULT),"UTF-8");
解密:
byte[] tmpt = Base64.decode(getString(R.string.app_url_use).getBytes("UTF-8"), Base64.DEFAULT);
tmpt = CryptoTools.ees3DecodeECB(tmpt);
String str = new String(tmpt, "UTF-8");
以上就是app直播源码,android AES加密解密实现, 更多内容欢迎关注之后的文章