加密算法(一般有三种形式)
1、安全哈希算法,只能加密,不能解密,属于单向加密,同类算法的还有
SHA1 SHA224 SHA256 SHA384 SHA512 MD5 HmacMD5 HmacSHA1 HmacSHA224 HmacSHA256 HmacSHA384 HmacSHA512 PBKDF2
注意,只要是哈希函数,就存在碰撞,就是两个不同的数据,他们的哈希值相同(SHA1值相同,概率特别小)
应用场景:好比你的游戏你接了vivo的SDK要上vivo平台,支付一笔之后,成功后vivo服务器会返回给你服务器一些参数和sign(他这个sign是经过加密了的),申请参数的时候之前他会给你分配一个appkey,那么你拿到vivo服务器传给你游戏服务器的这些参数之后(除了sign),把这些参数和分配给你的appkey自己加密一下,得到一个新的newSign,那么对比之前的Sign,看是不是对等的,如果是对等的,就说明这个服务器验签成功,没被别人篡改过的。以下可以用md5.可以用
public class SHA {
@Test
public void test(){
String result=Sha2Crypt.sha256Crypt("jett".getBytes());
System.out.println(result);
}
}
2、AES(高级加密标准)原理很复杂,目前没搞懂算法原理,关心的是如何加解密.
https连接支付宝,靠AES进行保护,是一种区块加密技术,把原文分成128位每块的数据,对每块单独进行加密
public class AES {
public static String ALGORITHM="AES";
public static byte[] encrypt(String content,String password) throws Exception{
KeyGenerator kgen = KeyGenerator.getInstance(ALGORITHM);
//用用户密码作为随机数初始化
kgen.init(128,new SecureRandom(password.getBytes()));
//得到一个密钥
SecretKey secretKey=kgen.generateKey();
//对钥密进行基本的编码
byte[] enCodeFormat = secretKey.getEncoded();
//转换成AES专用的密钥
SecretKeySpec key=new SecretKeySpec(enCodeFormat,ALGORITHM);
//创建一个密码器
Cipher cipher=Cipher.getInstance(ALGORITHM);
byte[] byteContent=content.getBytes();
//开始加密了
cipher.init(Cipher.ENCRYPT_MODE,key);
byte[] result=cipher.doFinal(byteContent);
return result;
}
public static byte[] decrypt(byte[] content,String password) throws Exception{
//创建AES的key生产者
KeyGenerator kgen=KeyGenerator.getInstance(ALGORITHM);
//利用用户密码作为随机数初始化
kgen.init(128,new SecureRandom(password.getBytes()));
//根据用户密码,生成一个密钥 (所有对称算法通用的)
SecretKey secretKey=kgen.generateKey();
//对密钥进行基本的编码
byte[] enCodeFormat=secretKey.getEncoded();
//转换成AES专用的密钥 RoundKey
SecretKeySpec key=new SecretKeySpec(enCodeFormat,ALGORITHM);
//创建一个密码器
Cipher cipher=Cipher.getInstance(ALGORITHM);
//解密
cipher.init(Cipher.DECRYPT_MODE,key);
byte[] result=cipher.doFinal(content);
return result;
}
@Test
public void test() throws Exception{
String content="jett53425234523452345234523452345234";
String password="123";
byte[] encryptByte=encrypt(content,password);
System.out.println("加密的数据:"+new String(encryptByte));
byte[] decrypt=decrypt(encryptByte,password);
System.out.println("解密后的效果:"+new String(decrypt));
}
}
3、RSA算法
RSA是目前最有影响力和最常用的公钥加密算法,它能够抵抗到目前为止已知的绝大多数密码攻击,已被ISO推荐为公钥数据加密标准。JDK提供了API生成公私钥。
一般来说是服务器用公钥加密之后,然后把加密串和私钥都发给客户端,让客户端去解密,得到明文.
public class RSA {
public static String ALGORITHM="RSA";
//指定key的位数
public static int KEYSIZE=1024;//65536数字越大等待时间越长,一般这个长度够了
//指定公钥存放的文件
public static String PUBLIC_KEY_FILE="public_key.dat";
//指定私钥存放的文件
public static String PRIVATE_KEY_FILE="private_key.dat";
public static void generateKeyPair() throws Exception{
SecureRandom sr=new SecureRandom();
//需要一个KeyPairGenerator来生成钥对
KeyPairGenerator keyPairGenerator=KeyPairGenerator.getInstance(ALGORITHM);
keyPairGenerator.initialize(KEYSIZE,sr);
//生成
KeyPair keyPair=keyPairGenerator.generateKeyPair();
Key publicKey=keyPair.getPublic();
Key privateKey=keyPair.getPrivate();
ObjectOutputStream objectOutputStream1 = new ObjectOutputStream(new FileOutputStream(PUBLIC_KEY_FILE));
ObjectOutputStream objectOutputStream2 = new ObjectOutputStream(new FileOutputStream(PRIVATE_KEY_FILE));
objectOutputStream1.writeObject(publicKey);
objectOutputStream2.writeObject(privateKey);
objectOutputStream2.close();
objectOutputStream1.close();
}
/**
* 加密
*/
public static String encrypt(String source) throws Exception{
generateKeyPair();
//取出公钥
ObjectInputStream ois=new ObjectInputStream(new FileInputStream(PUBLIC_KEY_FILE));
Key key=(Key)ois.readObject();
ois.close();
//开始使用
Cipher cipher=Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE,key);
byte[] b=source.getBytes();
byte[] b1=cipher.doFinal(b);
//转一下base64
BASE64Encoder encoder=new BASE64Encoder();
return encoder.encode(b1);
}
/**
* 解密
*/
public static String decrypt(String source) throws Exception{
//取出公钥
ObjectInputStream ois=new ObjectInputStream(new FileInputStream(PRIVATE_KEY_FILE));
Key key=(Key)ois.readObject();
ois.close();
//开始使用
Cipher cipher=Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE,key);
BASE64Decoder decoder=new BASE64Decoder();
byte[] b=decoder.decodeBuffer(source);
byte[] b1=cipher.doFinal(b);
return new String(b1);
}
@Test
public void test() throws Exception{
String content="jett12121212121212121";
String password=encrypt(content);
System.out.println("密文"+password);
//到了服务器以后
String target=decrypt(password);
System.out.println("明文"+target);
}
}
Dex加密与解密
64K问题https://developer.android.google.cn/studio/build/multidex.html#keep
查看安卓源码的URL:http://androidxref.com/
网友评论