Ⅰ 如何让java securityRandom 生成的盐 只包含字母和数字
public static void main(String[] args) throws Exception {
Random RANDOM = new SecureRandom();
byte[] salt = new byte[16];
RANDOM.nextBytes(salt);
- String str = new String(salt,"UTF-8");
+ String str = new BASE64Encoder().encode(salt);
System.out.println(str);
}
Ⅱ Java64字节salt,数据库字段怎么定义
甭管多少字节,数据库用字符类型就行,比如oracle你可以使用varchar2,mysql可以使用varchar定义。
Ⅲ java shiro加盐之后怎么反解密
hash函数是一种单向散列算法,这意味着从明文可以得到散列值,而散列值不可以还原为明文。
验证密码的方法是将用户输入的密码与盐值按照加密时使用的hash算法再hash一次,并与数据库中存储的hash值作比较,若两者一致则认为密码正确。
Ⅳ java md5加密 index页面代码
/**
*将指定byte数组转换成16进制字符串
*@paramb
*@return
*/
(byte[]b){
StringBufferhexString=newStringBuffer();
for(inti=0;i<b.length;i++){
Stringhex=Integer.toHexString(b[i]&0xFF);
if(hex.length()==1){
hex='0'+hex;
}
hexString.append(hex.toUpperCase());
}
returnhexString.toString();
}
/**
*获得加密后的16进制形式口令
*@parampassword
*@return
*@
*@
*/
(Stringpassword)
,UnsupportedEncodingException{
//声明加密后的口令数组变量
byte[]pwd=null;
//随机数生成器
SecureRandomrandom=newSecureRandom();
//声明盐数组变量
byte[]salt=newbyte[SALT_LENGTH];
//将随机数放入盐变量中
random.nextBytes(salt);
//声明消息摘要对象
MessageDigestmd=null;
//创建消息摘要
md=MessageDigest.getInstance("MD5");
//将盐数据传入消息摘要对象
md.update(salt);
//将口令的数据传给消息摘要对象
md.update(password.getBytes("UTF-8"));
//获得消息摘要的字节数组
byte[]digest=md.digest();
//因为要在口令的字节数组中存放盐,所以加上盐的字节长度
pwd=newbyte[digest.length+SALT_LENGTH];
//将盐的字节拷贝到生成的加密口令字节数组的前12个字节,以便在验证口令时取出盐
System.array(salt,0,pwd,0,SALT_LENGTH);
//将消息摘要拷贝到加密口令字节数组从第13个字节开始的字节
System.array(digest,0,pwd,SALT_LENGTH,digest.length);
//将字节数组格式加密后的口令转化为16进制字符串格式的口令
returnbyteToHexString(pwd);
}
publicstaticvoidmain(String[]args){//测试方法
try{
System.out.println(getEncryptedPwd("123456"));
}catch(NoSuchAlgorithmExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}catch(UnsupportedEncodingExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
在jsp页面中可以调用getEncryptedPwd方法
Ⅳ 在java中怎么通过md5和salt来修改密码
MD5很简单,有专门的类,自己定义一个加密用的saltKey。
还有自己写简单的加密解密可以用异或算法,一个字符串于某字符异或就加密了,再与这个字符异或又解密了。很简单的算法。不过比较容易破解
Ⅵ java 给密码字段加密
要是你想做得正规些,MD5或SHA1就是最好的选择了。它们至今都还十分安全。没发现有比这两者更好的解决方案。
MD5就是MD5,是一种算法,公开的,唯一的,没有安全的版本和普通的版本之分。大家用的MD5都是一样的。请始终记住,公开的才是安全的。密码学中都有讲的。
为了安全,你可以在MD5加密时,加些盐。举个例,将用户名,密码,和自定义的一些字符串连起来,然后再进行MD5计算。如:MyUsernameMyPasswordSalt。这么长的串是不容易破解的。你甚至可以连续使用两次MD5。
如果你光是对密码MD5加密,则网上有专门的破解工具,对于位数比较小的密码,极易破解,甚至秒杀。。
Ⅶ java如何实现md5加密的压缩为zip
public class ZipUtil {
private static final String ALGORITHM = "PBEWithMD5AndDES";
private static Logger logger = Logger.getLogger(ZipUtil.class);
public static void zip(String zipFileName, String inputFile,String pwd) throws Exception {
zip(zipFileName, new File(inputFile), pwd);
}
/**
* 功能描述:压缩指定路径下的所有文件
* @param zipFileName 压缩文件名(带有路径)
* @param inputFile 指定压缩文件夹
* @return
* @throws Exception
*/
public static void zip(String zipFileName, String inputFile) throws Exception {
zip(zipFileName, new File(inputFile), null);
}
/**
* 功能描述:压缩文件对象
* @param zipFileName 压缩文件名(带有路径)
* @param inputFile 文件对象
* @return
* @throws Exception
*/
public static void zip(String zipFileName, File inputFile,String pwd) throws Exception {
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));
zip(out, inputFile, "",pwd);
out.close();
}
/**
*
* @param out 压缩输出流对象
* @param file
* @param base
* @throws Exception
*/
public static void zip(ZipOutputStream outputStream, File file, String base,String pwd) throws Exception {
if (file.isDirectory()) {
File[] fl = file.listFiles();
outputStream.putNextEntry(new ZipEntry(base + "/"));
base = base.length() == 0 ? "" : base + "/";
for (int i = 0; i < fl.length; i++) {
zip(outputStream, fl[i], base + fl[i].getName(), pwd);
}
}
else {
outputStream.putNextEntry(new ZipEntry(base));
FileInputStream inputStream = new FileInputStream(file);
//普通压缩文件
if(pwd == null || pwd.trim().equals("")){
int b;
while ((b = inputStream.read()) != -1){
outputStream.write(b);
}
inputStream.close();
}
//给压缩文件加密
else{
PBEKeySpec keySpec = new PBEKeySpec(pwd.toCharArray());
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
SecretKey passwordKey = keyFactory.generateSecret(keySpec);
byte[] salt = new byte[8];
Random rnd = new Random();
rnd.nextBytes(salt);
int iterations = 100;
PBEParameterSpec parameterSpec = new PBEParameterSpec(salt, iterations);
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, passwordKey, parameterSpec);
outputStream.write(salt);
byte[] input = new byte[64];
int bytesRead;
while ((bytesRead = inputStream.read(input)) != -1) {
byte[] output = cipher.update(input, 0, bytesRead);
if (output != null){
outputStream.write(output);
}
}
byte[] output = cipher.doFinal();
if (output != null){
outputStream.write(output);
}
inputStream.close();
outputStream.flush();
outputStream.close();
}
}
file.delete();
}
public static void unzip(String zipFileName, String outputDirectory)throws Exception {
ZipInputStream inputStream = new ZipInputStream(new FileInputStream(zipFileName));
unzip(inputStream, outputDirectory, null);
}
/**
* 功能描述:将压缩文件解压到指定的文件目录下
* @param zipFileName 压缩文件名称(带路径)
* @param outputDirectory 指定解压目录
* @return
* @throws Exception
*/
public static void unzip(String zipFileName, String outputDirectory, String pwd)throws Exception {
ZipInputStream inputStream = new ZipInputStream(new FileInputStream(zipFileName));
unzip(inputStream, outputDirectory, pwd);
}
public static void unzip(File zipFile, String outputDirectory, String pwd)throws Exception {
ZipInputStream inputStream = new ZipInputStream(new FileInputStream(zipFile));
unzip(inputStream, outputDirectory,pwd);
}
public static void unzip(ZipInputStream inputStream, String outputDirectory, String pwd) throws Exception{
ZipEntry zipEntry = null;
FileOutputStream outputStream = null;
try{
while ((zipEntry = inputStream.getNextEntry()) != null) {
if (zipEntry.isDirectory()) {
String name = zipEntry.getName();
name = name.substring(0, name.length() - 1);
File file = new File(outputDirectory + File.separator + name);
if(! file.exists()){
file.mkdirs();
}
}else {
File file = new File(outputDirectory + File.separator + zipEntry.getName());
File parentFile = file.getParentFile();
if(! parentFile.exists()){
parentFile.mkdirs();
}
file.createNewFile();
outputStream = new FileOutputStream(file);
if(pwd == null || pwd.trim().equals("")){
int b;
byte[] buffer = new byte[1024];
while ((b = inputStream.read(buffer)) != -1){
outputStream.write(buffer);
}
outputStream.flush();
}else{
PBEKeySpec keySpec = new PBEKeySpec(pwd.toCharArray());
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
SecretKey passwordKey = keyFactory.generateSecret(keySpec);
byte[] salt = new byte[8];
inputStream.read(salt);
int iterations = 100;
PBEParameterSpec parameterSpec = new PBEParameterSpec(salt, iterations);
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, passwordKey, parameterSpec);
byte[] input = new byte[64];
int bytesRead;
while ((bytesRead = inputStream.read(input)) != -1) {
byte[] output = cipher.update(input, 0, bytesRead);
if (output != null){
outputStream.write(output);
}
}
byte[] output = cipher.doFinal();
if (output != null){
outputStream.write(output);
}
outputStream.flush();
}
}
}
}
catch(IOException ex){
logger.error(ex);
throw ex;
}catch(Exception ex){
logger.error(ex);
throw ex;
}finally{
if(inputStream != null){
inputStream.close();
}
if(outputStream != null){
outputStream.close();
}
}
}
public static byte[] getByteArrayFromFile(String fileName) throws Exception{
FileInputStream fi = null;
try{
File file = new File(fileName);
long size = file.length();
if (size > Integer.MAX_VALUE) {
throw new Exception("文件太大");
}
fi = new FileInputStream(file);
byte[] buffer = new byte[1024];
byte[] all = new byte[(int) size];
int offset = 0;
int len = 0;
while ((len = fi.read(buffer)) > -1) {
System.array(buffer, 0, all, offset, len);
offset += len;
}
return all;
}catch(Exception ex){
logger.error(ex);
throw ex;
}finally{
if(fi != null){
fi.close();
}
}
}
public static void createFileFromByteArray(byte[] b,String destName) throws Exception{
FileOutputStream os = null;
try{
File destFile = new File(destName);
File parentFile = destFile.getParentFile();
if(! parentFile.exists()){
parentFile.mkdirs();
}
os = new FileOutputStream(destName);
os.write(b);
os.flush();
}catch(Exception e){
logger.error(e);
throw e;
}finally{
if(os != null){
os.close();
}
}
}
public static void main(String[] args) throws Exception{
String fileName = "E:\\sunjinfu\\test\\test.zip";
String outputDir = "E:\\sunjinfu\\test\\csv\\123";
unzip(fileName, outputDir);
}
自己好好研究一下,我只用了其中一部分,你需要就调试调试,也许会有点错误。
Ⅷ 一个php加密方法,怎么用java实现,高分!
先找出来是什么算法,JAVA 里面的现成的算法还是较多的。
看加密的方法,应该是 blowfish 请网络 还是较容易找到的 blowfish JAVA 实现
Ⅸ java如何读取一个加密后的.xls文件
近日来,研究了一下Excel Biff8(xls 97-2007)与OpenXML(ECMA-376)的加密文档的读取(这还是为了我们世界先进Grid而做的 ^__^)。有些成果,写在这里,希望能给要做类似功能的XD们一些参考。
如有不详,请联系:[email protected] / [email protected]
前提:
1. 加密文档:指Wookbook级的加密,就是在Save Excel文档时在General Settings中设置open password之后的文档;
2. 打开:需要用户传入密码。并非破解。但请勿将本文方法添加暴力模块使用 :-) ;
3. 本文涉及较多为,密钥计算,关于解密细节请参考微软相关文档;
使用的加密算法: RC4, SHA1, MD5, AES-128(其中RC4并不包含在所有版本的.NET Framework中,AES算法可以在.NET Framework 3.5中找到)
本文示例依赖 .NET Framework 3.5
A. Biff8 的加密文档读取
1. 通过文档中FILEPASS的record取得,文档的加密信息(关于Biff文档的格式问题,请参阅Biff的微软文档)
其中Biff8可以使用两种方法加密:Biff8标准加密算法和Biff8扩充加密算法。本文主要讨论最常用的Biff标准加密算法
2. 通过FILEPASS的结构,获得如下信息:
salt(加密随机数,16 bytes)
password verifier (密码效验器,16 bytes)
password verifier hash(密码效验器Hash,16 bytes)
3. 通过以上信息,生成解密key。并通过密码效验器,验证密码:
i. 将密码转化成unicode数组,并进行MD5 Hash;
ii. 将hash结果与salt串联,然后将byte数组,反复串联16次(336 bytes) ,然后再进行MD5 Hash;
iii. 将上步hash结果的前五位,串联上4 bytes的block值(在密码验证阶段为0,在以后解密阶段为block的index) ,然后进行MD5 Hash;
iv. 将上步hash结果的前16位,作为key
v. 使用RC4对称加密算法,将password verifier和password verifier hash分别解密,然后对password verifier的解密结果进行MD5 hash,其值应和password verifier hash的解密结果一致,即为密码正确。
vi. 之后进行逐个record的解密。excel biff8加密原则基本为,record的标示不加密,长度不加密,个别record不加密(见文档);另外,在record解密时,还需要通过block的值重新计算解密key,block的大小为1024.
4. 详细请参照示例代码;
B. OpenXML(ECMA-376) 加密文档的读取
1. 通常来说,xlsx文件相当于一个zip文件,可以用zip程序,直接打开。而在加密后,为了安全性考虑,微软使用了 structured storage(一种OLE文档存储方式)存储(可以用7-zip或者OLE document viewer打开,windows也有相应API来操作此类结构)。在上述文档中,有一个叫做“EncryptedPackage”加密的package,就是一个zip包通过AES算法进行加密之后的结果。我们将使用和A一样的方式来检查密码,但生成key的方法不同;OpenXML的加密类型也有多种,我们这里就讨论常用的用AES-128进行加密的流程;
2. 通过文档的“EncryptedInfo”部分,需要过的一下信息(关于此部分的结构,请参考[MS-OFFCRYPTO].pdf)
salt(加密随机数,16 bytes)
password verifier (密码效验器,16 bytes)
password verifier hash(密码效验器Hash,32 bytes)
3. 通过以上信息,生成解密key。并通过密码效验器,验证密码:
i. 首先,定义一个H函数,其有两个输入,内部使用SHA1算法将两个输入串联之后的结果hash返回;
ii. 先将salt与password(password的unicode数组)进行H计算,h = H(salt, password) ;
iii.然后设iterator为0x00000000,将其转为4byte的数组,然后进行H计算,h1 = H(iterator, h);
iv.将上面的iterator递增一,然后再与h1进行H计算,h2 = H(iterator,h1),然后将这个递增和计算过程重复50000次,最后计算过的iterator为49999即可;
v. 现在有计算结果h50000,将h50000再与0x00000000(4 byte数组)进行H计算,Hfinal = H(h50000, 0x00000000);
vi. 生成一个64byte的数组,将每位都初始化成0x36,然后将这个数组与Hfinal异或;(关于这个地方,微软文档中写的有错误,按照原文的方法生成的key不正确,要不是文档的作者回信告诉我要使用这个法子,就算我想破头也想不出来啊 T__T)
vii.将异或结果,进行SHA1 hash,结果的前16byte就是解密的key;
viii.初始化AES算法,key长度为128,模式为ECB模式,Padding为none; 然后将password verifier 和password verifier hash分别解密;
ix. password verifier 解密后的SHA1 hash结果应该与password verifier hash解密后的前20byte相同;
4. 关于"EncryptedPackage" 的解密则更为简单,只许将“EncryptedPackage”读入,去除前8byte的size信息后,进行AES解密,即为未加密的标准openxml文档。
参考:
[MS-OFFCRYPTO].pdf
[MS-XLS].pdf
ECMA-376 standards
Reply by "winnow", 2008-09-10, 1:17
-----------------------------------------------------
总结一下, 关于这两种基于密码的加密方法, 基本上都是基于RFC2898 建议, 思想是这样:
输入是用户的密码:password, 输出是提供给加密函数的密钥:key.
考虑安全, 需要使同样的password生成的key不一样, 这样用相同的password加密后的结果就无法比较. 需要一个随机数salt.
另外, 为了使暴力破解的代价增大, 考虑使用一个循环多次的过程, 需要循环次数:iteration_count.
概念上, 生成方法为: 将password和salt进行某种运算, 配合一个Hash函数, 以某种方式循环iteration_count次, 在最后的结果里取一部分作为key输出.
具体参照RFC2898中的建议方法PBKDF1和PBKDF2.
这样, 用户输入的密码与一个随机数组合, 经过一定代价的运算, 就生成了可以供加密函数使用的密钥. 使用这个密钥和一个加密函数, 就可以进行加密了.
在应用中, 为了快速判断密码是否错误. 生成一个随机数verifier, 用一个Hash函数计算verifier的hash值:verifier_hash, 分别加密verifier和verifier_hash并保存.
解密的时候, 先分别解密出verifier和verifier_hash, 计算verifier的hash值, 与verifier_hash比较, 如果一致, 即说明密码正确.