下面展示这些 代码部分
。
import java.nio.charset.Charset;
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.*;
import java.security.spec.*;
import javax.crypto.spec.*;
//算法
public class Aes
{
static final String ALGORITHM = "AES";
static Charset charset = Charset.forName("UTF-8");
public static SecretKey generateKey() throws NoSuchAlgorithmException { // 生成密钥
KeyGenerator secretGenerator = KeyGenerator.getInstance(ALGORITHM);
SecureRandom secureRandom = new SecureRandom();
secretGenerator.init(secureRandom);
SecretKey secretKey = secretGenerator.generateKey();
return secretKey;
}
public static byte[] encrypt(byte[] content, SecretKey secretKey) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException { // 加密
return aes(content,Cipher.ENCRYPT_MODE,secretKey);
}
public static byte[] decrypt(byte[] contentArray, SecretKey secretKey) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException { // 解密
byte[] result = aes(contentArray,Cipher.DECRYPT_MODE,secretKey);
return result;
}
private static byte[] aes(byte[] contentArray, int mode, SecretKey secretKey)
throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(mode, secretKey);
byte[] result = cipher.doFinal(contentArray);
return result;
}
//获取key
public static SecretKey getKey(byte[] kes){
SecretKey secretKey=null;
try
{
KeyGenerator secretGenerator = KeyGenerator.getInstance("AES");
byte[] sadkey=kes;//Aes.generateKey().getEncoded()
SecureRandom secureRandom = new SecureRandom();
secureRandom.setSeed(sadkey);
secretGenerator.init(secureRandom);
secretKey = secretGenerator.generateKey();
}
catch (Exception e)
{}
return secretKey;
}
//获取key byte类型
public static byte[] getbyteKey(){
byte[] keysss=null;
//方法一
try
{
KeyGenerator secretGenerator = KeyGenerator.getInstance(ALGORITHM);
SecureRandom secureRandom = new SecureRandom();
secretGenerator.init(secureRandom);
SecretKey secretKey = secretGenerator.generateKey();
keysss = secretKey.getEncoded();
}
catch (NoSuchAlgorithmException e)
{}
/*
//方法二
try
{
keysss = generateKey().getEncoded();
}
catch (NoSuchAlgorithmException e)
{}
*/
return keysss;
}
/*
byte[] mi= getbyteKey();
SecretKey secretKey = getKey(mi);
String sad="待加jcjgvv国际服减肥法韩剧发货还差个独女结果迷惑不解hcc几个高Vhhc互个粉附近的烘干机发布会好的就睡觉大V好家常菜你打和他爸登记过TV好彻底v的开发是呀密";
byte[] sady=null;//加密了
byte[] sada=null;//解密后
sady=Aes.encrypt(sad.getBytes("utf-8"),secretKey);
sada=Aes.decrypt(sady,secretKey);
Toast.makeText(MainActivity.this,"加密后:_"+new String(sady)+"解密后:_"+new String(sada),Toast.LENGTH_SHORT).show();
*/
}
上面一些就是笔记记录,内容一部分来自网络,进行修改优化后而来