上海网站排名推广,网站域名是网站架构吗,WordPress后花园,wordpress 七牛上传序
本文主要研究一下PBE算法
PBE
PBE即Password Based Encryption#xff0c;基于口令的加密#xff0c;它是一种组合算法#xff0c;即一般是哈希对称算法#xff0c;比如PBEWithMD5AndDES#xff0c;就是用MD5做哈希#xff0c;用DES做加解密#xff0c;而其密钥则…序
本文主要研究一下PBE算法
PBE
PBE即Password Based Encryption基于口令的加密它是一种组合算法即一般是哈希对称算法比如PBEWithMD5AndDES就是用MD5做哈希用DES做加解密而其密钥则是口令salt基于哈希函数计算而来
java示例 public void testPBEWithIvParameter() throws NoSuchPaddingException, NoSuchAlgorithmException, IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException, InvalidKeyException, InvalidKeySpecException {String algorithm PBEWithMD5AndDES;char[] passwd 123456.toCharArray();PBEKeySpec pbeKeySpec new PBEKeySpec(passwd);SecretKeyFactory kf SecretKeyFactory.getInstance(algorithm);SecretKey key kf.generateSecret(pbeKeySpec);byte[] salt new byte[8];Random random new Random();random.nextBytes(salt);Cipher cp Cipher.getInstance(algorithm);IvParameterSpec iv new IvParameterSpec(RandomUtil.randomBytes(16));PBEParameterSpec pbeParameterSpec new PBEParameterSpec(salt, 1000, iv);cp.init(Cipher.ENCRYPT_MODE, key, pbeParameterSpec);byte[] data helloworld.getBytes(StandardCharsets.UTF_8);byte[] encrypted cp.doFinal(data);System.out.println(Base64.encode(encrypted));Cipher cpDecrypt Cipher.getInstance(algorithm);cpDecrypt.init(Cipher.DECRYPT_MODE, key, pbeParameterSpec);byte[] decryptBytes cpDecrypt.doFinal(encrypted);System.out.println(new String(decryptBytes));}几个参数一个是口令即passwd一个是salt随机盐值一个是ivParameter golang示例
func Encrypt(message string, password string, salt []byte) (string, error) {keyObtentionIterations : 1000md5key, iv : getMd5DerivedKey(password, salt, keyObtentionIterations)encrypted, err : desEncrypt([]byte(message), md5key, iv)if err ! nil {return , err}result : encryptedif includePlainIvInEncryptionResults() {result append(iv, result...)}if includePlainSaltInEncryptionResults() {result append(salt, result...)}return base64.StdEncoding.EncodeToString(result), nil
}小结
PBE即Password Based Encryption基于口令的加密它是一种组合算法即一般是哈希对称算法比如PBEWithMD5AndDES就是用MD5做哈希用DES做加解密而其密钥则是口令salt基于哈希函数计算而来当使用固定salt和不使用ivParameter的DES的时候同一个值每次加密生成的密文是一样的而使用随机salt和随机iv的时候每次生成的密文是不一样的这个时候密文会包含随机的salt和iv信息在解密的时候能够正确解出明文