python生成随机盐 BCrypt使用介绍 Jasypt使用介绍 常用加密库介绍

2017-05-29 15:10:00
admin
原创 2859
摘要:python生成随机盐 BCrypt使用介绍 Jasypt使用介绍 常用加密库介绍

一、python生成随机盐

1、字典破解Dictionary Attack,使用字典单词枚举;

2、暴力破解Brute Force Attack,使用可能单词枚举;

3、彩虹表Rainbow Tables,组合字典破解和暴力破解,用于平衡时间和空间;

4、密码加盐hash,盐放在前面或者后面都可以,一般长度20位;


import random
def getLittleChar():
    return random.choice('abcdefghijklmnopqrstuvwxyz')
def getBigChar():
    return random.choice('ABCDEFGHIJKLMNOPQRSTUVWXYZ')
def getSalt(len):
    salt = ''
    while len > 0:
        salt = salt + random.choice('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')
        len -= 1
    return salt
if __name__ == '__main__':
    print getSalt(20)


二、BCrypt使用介绍

1、解决了明文密码存储的问题,且每个用户盐不一样;

2、明文密码可能在网络中传输,补救方法md5(password,username),即使用用户名做为盐md5一次;

3、终极密码安全算法:md5(md5(password,salt),salt),里面的一层保证传输安全,外面的一层保证存储安全;

4、hashpw可以传空密码"";

5、salt和hashed的长度与密码长度和log_rounds没有关系;


依赖地址:

<dependency>
<groupId>org.mindrot</groupId>
<artifactId>jbcrypt</artifactId>
<version>0.4</version>
</dependency>


代码片段:

String password = "123456";
String salt = BCrypt.gensalt();
String hashed = BCrypt.hashpw(password, salt);
System.out.println(String.format("%2d %s", salt.length(), salt));
System.out.println(String.format("%2d %s", hashed.length(), hashed));
System.out.println(BCrypt.checkpw(password, hashed));


输出:

29 $2a$10$ZDgyHYXFnM4YU2gy1jhuz.
60 $2a$10$ZDgyHYXFnM4YU2gy1jhuz.2ndEoYLRMoZu2r4VQiwWV7ekbeYBzDG
true


三、Jasypt使用介绍

1、PBE是基于口令的加密算法,其中密钥使用口令计算生成,加密算法仍然使用现有算法;

2、密码复杂度基本不会超过8字节密钥,所以加密强度基本不会超过DES算法;

3、PBE常用算法:PBEWithMD5AndDES、PBEWithSHA1AndDESede;

4、PBEKeySpec密码参数可以是null,盐参数必须是8字节;

5、Jasypt是用于简化PBE使用的一个工具库;

6、SimplePBEConfig密码参数不能是null或者空字符串,盐参数默认随机盐,计算密钥迭代次数默认1000;

7、EnvironmentStringPBEConfig相比SimplePBEConfig:可以读取环境变量,可以设置输出结果格式;


依赖地址:

<dependency>
<groupId>org.jasypt</groupId>
<artifactId>jasypt</artifactId>
<version>1.9.3</version>
</dependency>


JDK代码示例:

public static void testPBE() throws Exception {
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
PBEKeySpec keySpec = new PBEKeySpec("mypassword".toCharArray());
SecretKey secretKey = keyFactory.generateSecret(keySpec);
PBEParameterSpec parameterSpec = new PBEParameterSpec("today100".getBytes(), 100);

Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES");
cipher.init(Cipher.ENCRYPT_MODE, secretKey, parameterSpec);
byte cipherData[] = cipher.doFinal("secret".getBytes("UTF-8"));
cipher.init(Cipher.DECRYPT_MODE, secretKey, parameterSpec);
byte plaindData[] = cipher.doFinal(cipherData);
System.out.println(new String(plaindData));
}


Jasypt代码示例:

public static void testPBE2() {
StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
SimplePBEConfig config = new SimplePBEConfig();
config.setAlgorithm("PBEWithMD5AndDES");
config.setPassword("mypassword");
config.setKeyObtentionIterations(100);
config.setSaltGenerator(new StringFixedSaltGenerator("today100"));
encryptor.setConfig(config);

String content="secret";
String ciphertext=encryptor.encrypt(content);
String plaintext = encryptor.decrypt(ciphertext);
System.out.println(plaintext);
System.out.println(ciphertext);
}


四、常用加密库介绍

1、openssl,最广泛使用的C语言加密库;

2、miracl,最轻量级别的C语言加密库;

3、commons-codec,支持各种编码,支持消息摘要;


bcprov库介绍:

1、bcprov-jdk15to18,包含基本加解密功能;

2、bcpkix-jdk15to18,包含公钥基础设施功能;

3、bctls-jdk15to18,包含安全传输套接字功能;

4、bcprov-ext-jdk15to18,包含基本加解密功能,包含NTRU公钥密码算法;

5、org.bouncycastle.util.Strings能够转换byte和char;

发表评论
评论通过审核之后才会显示。