private void generateKey(char psd[]) throws Exception{
KeyGenerator keyGenerator =KeyGenerator.getInstance("Rijndael");
keyGenerator.init(256);
Key key = keyGenerator.generateKey();
byte [] salt =new byte[8];
SecureRandom random= new SecureRandom();
random.nextBytes(salt);
PBEKeySpec pbeKeySpec = new PBEKeySpec(psd);
SecretKeyFactory keyFortory = SecretKeyFactory.getInstance("PBEWithSHAAndTwofish-CBC");
SecretKey pbeKey=keyFortory.generateSecret(pbeKeySpec);
PBEParameterSpec pbeParaSpec= new PBEParameterSpec(salt, iteration);
Cipher cipher = Cipher.getInstance("PBEWithSHAAndTwofish-CBC");
cipher .init(Cipher.ENCRYPT_MODE, pbeKey, pbeParaSpec);//此句有问题????????????????
byte [] encryptedKeyBytes = cipher.doFinal(key.getEncoded());
FileOutputStream fos = new FileOutputStream(Key_Filename);
fos.write(salt);
fos.write(encryptedKeyBytes);
fos.close();
}此方法是给基于口令的密钥加密之后存储到文件中,这儿程序运行到cipher .init(Cipher.ENCRYPT_MODE, pbeKey, pbeParaSpec)出错提示:java.security.InvalidKeyException: Illegal key size,我把密钥的长度init(256)改为128或64仍然报错,这儿是因为用错了加密算法??????????????
KeyGenerator keyGenerator =KeyGenerator.getInstance("Rijndael");
keyGenerator.init(256);
Key key = keyGenerator.generateKey();
byte [] salt =new byte[8];
SecureRandom random= new SecureRandom();
random.nextBytes(salt);
PBEKeySpec pbeKeySpec = new PBEKeySpec(psd);
SecretKeyFactory keyFortory = SecretKeyFactory.getInstance("PBEWithSHAAndTwofish-CBC");
SecretKey pbeKey=keyFortory.generateSecret(pbeKeySpec);
PBEParameterSpec pbeParaSpec= new PBEParameterSpec(salt, iteration);
Cipher cipher = Cipher.getInstance("PBEWithSHAAndTwofish-CBC");
cipher .init(Cipher.ENCRYPT_MODE, pbeKey, pbeParaSpec);//此句有问题????????????????
byte [] encryptedKeyBytes = cipher.doFinal(key.getEncoded());
FileOutputStream fos = new FileOutputStream(Key_Filename);
fos.write(salt);
fos.write(encryptedKeyBytes);
fos.close();
}此方法是给基于口令的密钥加密之后存储到文件中,这儿程序运行到cipher .init(Cipher.ENCRYPT_MODE, pbeKey, pbeParaSpec)出错提示:java.security.InvalidKeyException: Illegal key size,我把密钥的长度init(256)改为128或64仍然报错,这儿是因为用错了加密算法??????????????
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货