use of javax.crypto.spec.PBEKeySpec in project robovm by robovm.
the class PBEKeySpecTest method testGetIterationCount.
/**
* getIterationCount() method testing. Tests that returned value is equal
* to the value specified in the constructor.
* Also it checks that the method returns 0 if iterationCount is not
* specified.
*/
public void testGetIterationCount() {
char[] password = new char[] { '1', '2', '3', '4', '5' };
byte[] salt = new byte[] { 1, 2, 3, 4, 5 };
int iterationCount = 10;
PBEKeySpec pbeks = new PBEKeySpec(password, salt, iterationCount);
assertTrue("The returned iterationCount is not equal to the specified " + "in the constructor.", pbeks.getIterationCount() == iterationCount);
pbeks = new PBEKeySpec(password);
assertTrue("The getIterationCount() method should return 0 " + "if the iterationCount is not specified.", pbeks.getIterationCount() == 0);
}
use of javax.crypto.spec.PBEKeySpec in project robovm by robovm.
the class PBEKeySpecTest method testPBEKeySpec2.
/**
* PBEKeySpec(char[] password, byte[] salt, int iterationCount, int
* keyLength) method testing. Tests the behavior of the method in the case
* of inappropriate parameters and checks that array objects specified as
* a parameters are copied during the object initialization.
*/
public void testPBEKeySpec2() {
char[] password = new char[] { '1', '2', '3', '4', '5' };
byte[] salt = new byte[] { 1, 2, 3, 4, 5 };
int iterationCount = 10;
int keyLength = 10;
try {
PBEKeySpec pbeks = new PBEKeySpec(null, salt, iterationCount, keyLength);
assertTrue("An empty char[] should be used in case of null input " + "char array.", pbeks.getPassword().length == 0);
} catch (IllegalArgumentException e) {
fail("Unexpected IllegalArgumentException was thrown.");
} catch (NullPointerException e) {
fail("Unexpected NullPointerException was thrown.");
}
try {
new PBEKeySpec(password, null, iterationCount, keyLength);
fail("A NullPointerException should be was thrown " + "in the case of null salt.");
} catch (IllegalArgumentException e) {
fail("Unexpected IllegalArgumentException was thrown.");
} catch (NullPointerException e) {
}
try {
new PBEKeySpec(password, new byte[0], iterationCount, keyLength);
fail("An IllegalArgumentException should be thrown " + "in the case of empty salt.");
} catch (IllegalArgumentException e) {
}
try {
new PBEKeySpec(password, salt, -1, keyLength);
fail("An IllegalArgumentException should be thrown " + "in the case of negative iterationCount.");
} catch (IllegalArgumentException e) {
}
try {
new PBEKeySpec(password, salt, iterationCount, -1);
fail("An IllegalArgumentException should be thrown " + "in the case of negative keyLength.");
} catch (IllegalArgumentException e) {
}
try {
new PBEKeySpec(password, salt, 0, keyLength);
fail("An IllegalArgumentException should be thrown " + "in the case of zero iterationCount.");
} catch (IllegalArgumentException e) {
}
try {
new PBEKeySpec(password, salt, iterationCount, 0);
fail("An IllegalArgumentException should be thrown " + "in the case of zero keyLength.");
} catch (IllegalArgumentException e) {
}
PBEKeySpec pbeks = new PBEKeySpec(password, salt, iterationCount, keyLength);
password[0]++;
assertFalse("The change of password specified in the constructor " + "should not cause the change of internal array.", password[0] == pbeks.getPassword()[0]);
salt[0]++;
assertFalse("The change of salt specified in the constructor " + " should not cause the change of internal array.", salt[0] == pbeks.getSalt()[0]);
}
use of javax.crypto.spec.PBEKeySpec in project robovm by robovm.
the class BcKeyStoreSpi method makePBECipher.
protected Cipher makePBECipher(String algorithm, int mode, char[] password, byte[] salt, int iterationCount) throws IOException {
try {
PBEKeySpec pbeSpec = new PBEKeySpec(password);
SecretKeyFactory keyFact = SecretKeyFactory.getInstance(algorithm, BouncyCastleProvider.PROVIDER_NAME);
PBEParameterSpec defParams = new PBEParameterSpec(salt, iterationCount);
Cipher cipher = Cipher.getInstance(algorithm, BouncyCastleProvider.PROVIDER_NAME);
cipher.init(mode, keyFact.generateSecret(pbeSpec), defParams);
return cipher;
} catch (Exception e) {
throw new IOException("Error initialising store of key store: " + e);
}
}
use of javax.crypto.spec.PBEKeySpec in project robovm by robovm.
the class PKCS12KeyStoreSpi method wrapKey.
protected byte[] wrapKey(String algorithm, Key key, PKCS12PBEParams pbeParams, char[] password) throws IOException {
PBEKeySpec pbeSpec = new PBEKeySpec(password);
byte[] out;
try {
SecretKeyFactory keyFact = SecretKeyFactory.getInstance(algorithm, bcProvider);
PBEParameterSpec defParams = new PBEParameterSpec(pbeParams.getIV(), pbeParams.getIterations().intValue());
Cipher cipher = Cipher.getInstance(algorithm, bcProvider);
cipher.init(Cipher.WRAP_MODE, keyFact.generateSecret(pbeSpec), defParams);
out = cipher.wrap(key);
} catch (Exception e) {
throw new IOException("exception encrypting data - " + e.toString());
}
return out;
}
use of javax.crypto.spec.PBEKeySpec in project yyl_example by Relucent.
the class PBEWithMD5AndDES_Encrypt method decrypt.
/**
* 将加密文本进行解密
* @param encryptText String
* @return String
*/
public String decrypt(String encryptText) throws Exception {
if (encryptText == null || encryptText.length() == 0) {
return "";
}
PBEKeySpec pbks = new PBEKeySpec((password).toCharArray());
SecretKeyFactory skf = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
SecretKey k = skf.generateSecret(pbks);
StringTokenizer st = new StringTokenizer(hex2string(encryptText), " ");
int num = 0;
byte[] salt = new byte[8];
while (st.hasMoreTokens() && (num < 8)) {
salt[num] = (byte) (Integer.parseInt(st.nextToken()));
num++;
}
int count = 0;
byte[] cbtemp = new byte[2000];
while (st.hasMoreTokens()) {
cbtemp[count] = (byte) (Integer.parseInt(st.nextToken()));
count++;
}
byte[] cb = new byte[count];
for (int i = 0; i < cb.length; i++) {
cb[i] = cbtemp[i];
}
Cipher cp = Cipher.getInstance("PBEWithMD5AndDES");
PBEParameterSpec ps = new PBEParameterSpec(salt, 1000);
cp.init(Cipher.DECRYPT_MODE, k, ps);
byte[] ptext = cp.doFinal(cb);
return new String(ptext, encoding);
}
Aggregations