数据安全java
这些都是靠人的想象和直觉来涉及的,很是不靠谱,而现代计算机加密:算法
总结 数组
1、什么是URL编码?
URL编码是浏览器发送数据给服务器时使用的编码。浏览器
什么是编码?
ascii码就是一种编码,例如安全
字母 | 编码(16进制) |
---|---|
A | 0x41 |
B | 0x42 |
C | 0x43 |
D | 0x44 |
... | ... |
汉字 | Unicode编码 | UTF-8编码 |
---|---|---|
中 | 0x4e2d | 0xe4b8ad |
文 | 0x6587 | 0xe69687 |
编 | 0x7f16 | 0xe7bc96 |
码 | 0x7801 | 0xe7a081 |
... | ... | ... |
URL编码规则是什么?服务器
例子dom
/** * URL编码 */ public class SecURL { public static void main(String[] args) throws UnsupportedEncodingException { String original = "URL 参数"; String encoded = URLEncoder.encode(original,"UTF-8"); System.out.println("编码后:"+encoded); String ori = new String(URLDecoder.decode(encoded,"UTF-8")); System.out.println("解码后:"+ori); } }
总结ide
2、Base64编码
* 什么是Base64编码?
是一种把二进制数据用文本表示的编码算法,例如:byte[]{0xe4,0xb8,0xad} ==> "5Lit"测试
索引 | 编码 | 索引 | 编码 | 索引 | 编码 |
---|---|---|---|---|---|
0 | A | 26 | a | 52 | 0 |
1 | B | 27 | b | 53 | 1 |
2 | C | 28 | c | ... | ... |
3 | D | 29 | d | 61 | 9 |
... | ... | ... | ... | 62 | + |
25 | Z | 51 | z | 63 | / |
目的编码
/** * Base64编码 */ public class SecBase64 { public static void main(String[] args) throws Exception { String original = "Hello\u00ff编码测试"; //withoutPadding()能够去掉编码后“=”这个字节,有没有=对于解码来讲没有影响 String b64 = Base64.getEncoder().withoutPadding().encodeToString(original.getBytes("UTF-8")); System.out.println(b64); String ori = new String(Base64.getDecoder().decode(b64), "UTF-8"); System.out.println(ori); } }
因为标准的base64在url中会引发冲突,因此在url中使用base64编码会使用另一种。
在java中,使用url的base64编码它会把“+”变为“-”,把“/"变为“_”这样在传递url参数的时候不会引发冲突
总结
1、什么是摘要算法?
摘要算法是一种能产生特殊输出格式的算法,这种算法的特色是:不管用户输入多少长度的原始数据,通过计算后输出的密文都是固定长度的,只要原数据稍有改变,输出的“摘要”便彻底不一样,所以,基于这种原理的算法便能对数据完整性提供较为健全的保障。
经常使用的摘要算法主要有MD5和SHA1。D5的输出结果为16字节(128位),SHA1的输出结果为20字节(160位)。
摘要算法(又称哈希算法/数字指纹)
* 计算任意成都数据的摘要(固定长度)
目的:
例如:
输入:任意长度的数据(byte[])
输出:固定长度的数据(byte[n])
hash("hello") = 0x5e918d2
hash("hello,java") = 0x7a9d88e8
hash("hello,bob") = 0xa0dbae2f
Java的Object.hashCode()方法就是一个摘要算法
这就是说相同的输入必须获得相同的输出,当咱们从新equals()方法的时候也要同时从新hashCode()方法
什么是碰撞?
两个不一样的输入获得了相同的输出
例如:
hash("abc") = 0x12345678
hash("xyz") = 0x12345678
这个时候咱们就说发生了碰撞,碰撞能不能呢,碰撞是不能避免的。
输出的字节是固定的,而输入的字节是不肯定的
输出n bits | 范围 |
---|---|
0000000000 | 0 |
0000000001 | 1 |
0000000010 | 2 |
... | ... |
11111111111 | 62235 |
Hash算法的安全性?
经常使用摘要算法
彩虹表
什么是彩虹表呢?是一个预先计算好的经常使用的字符和md5的一个对照表。
抵御彩虹表
public static void main(String[] args) throws Exception{ String str = "MD5摘要算法测试"; byte[] bytes = toMD5(str.getBytes("UTF-8")); //以16进制的方式,打印出byte数组 System.out.println(String.format("%032x",new BigInteger(1,bytes))); } public static byte[] toMD5(byte[] input){ MessageDigest md; try { md = MessageDigest.getInstance("MD5"); }catch (Exception e){ throw new RuntimeException(e); } md.update(input); return md.digest(); }
public static void main(String[] args) throws Exception{ String password = "helloworld"; String salt = "Random salt"; byte[] bytes = toMD5((salt+password).getBytes("UTF-8")); //以16进制的方式,打印出byte数组 System.out.println(String.format("%032x",new BigInteger(1,bytes))); } public static byte[] toMD5(byte[] input){ MessageDigest md; try { md = MessageDigest.getInstance("MD5"); }catch (Exception e){ throw new RuntimeException(e); } md.update(input); return md.digest(); }
2、SHA-1
例子:同md5
public static void main(String[] args) throws Exception{ String str = "MD5摘要算法测试"; byte[] bytes = toMD5(str.getBytes("UTF-8")); //因为SHA-1输出是40个字节,因此用%040x来表示输出 System.out.println(String.format("%040x",new BigInteger(1,bytes))); } public static byte[] toMD5(byte[] input){ MessageDigest md; try { md = MessageDigest.getInstance("SHA-1"); }catch (Exception e){ throw new RuntimeException(e); } md.update(input); return md.digest(); }
jdk并未包含RipeMD160算法,须要单独下载jar包放入jdk中
3、BouncyCastle
如何使用第三方提供的算法?
4、Hmac
Hmac:Hash-based Message Authentication Code
更安全的消息摘要算法
HmacMD5能够看作带安全Salt的MD5
public class Hmac { public static byte[] hmac(String hmacAlgorithm, SecretKey skey,byte[] input)throws Exception{ Mac mac = Mac.getInstance(hmacAlgorithm); mac.init(skey); mac.update(input); return mac.doFinal(); } public static void main(String[] args) throws Exception { String algorithm = "HmacSHA1"; //String algorithm = "HmacSHA256"; //原始数据 String data = "hello world"; //随机生成一个key KeyGenerator keyGen = KeyGenerator.getInstance(algorithm); SecretKey skey = keyGen.generateKey(); //打印key byte[] key = skey.getEncoded(); BigInteger bigInteger = new BigInteger(1, key); System.out.println("Key:"+bigInteger.toString(key.length/2)); //用这个key计算 byte[] result = hmac(algorithm,skey,data.getBytes("UTF-8")); BigInteger resultInteger = new BigInteger(1, result); System.out.println("Hash:"+resultInteger.toString(result.length/2)); } }
总结:
一.什么是对称加密算法
2、经常使用的加密算法
它们的密钥长度各不相同,密钥长度决定了加密的强度。工做模式和填充模式能够看作是加密算法的参数和格式的选择,jdk提供的算法并无提供全部的工做模式及填充模式。
des算法由于密钥果断能够在短期内暴力破解,已经被淘汰