use of java.security.AlgorithmParameters in project jdk8u_jdk by JetBrains.
the class SealedObjectTest method doTest.
/*
* Run the test:
* - init a cipher with AES/GCM/NoPadding transformation
* - seal an object
* - check if we can't seal it again with the same key/IV
* - unseal the object using different methods of SealedObject class
* - check if the original and sealed objects are equal
*/
static void doTest() throws Exception {
// init a secret Key
KeyGenerator kg = KeyGenerator.getInstance(AES, PROVIDER);
kg.init(KEY_LENGTH);
SecretKey key = kg.generateKey();
// initialization
Cipher cipher = Cipher.getInstance(TRANSFORMATION, PROVIDER);
cipher.init(Cipher.ENCRYPT_MODE, key);
AlgorithmParameters params = cipher.getParameters();
// seal an object
SealedObject so = new SealedObject(key, cipher);
try {
// check if we can't seal it again with the same key/IV
so = new SealedObject(key, cipher);
throw new RuntimeException("FAILED: expected IllegalStateException hasn't " + "been thrown");
} catch (IllegalStateException ise) {
System.out.println("Expected exception when seal it again with" + " the same key/IV: " + ise);
}
// unseal the object using getObject(Cipher) and compare
cipher.init(Cipher.DECRYPT_MODE, key, params);
SecretKey unsealedKey = (SecretKey) so.getObject(cipher);
assertKeysSame(unsealedKey, key, "SealedObject.getObject(Cipher)");
// unseal the object using getObject(Key) and compare
unsealedKey = (SecretKey) so.getObject(key);
assertKeysSame(unsealedKey, key, "SealedObject.getObject(Key)");
// unseal the object using getObject(Key, String) and compare
unsealedKey = (SecretKey) so.getObject(key, PROVIDER);
assertKeysSame(unsealedKey, key, "SealedObject.getObject(Key, String)");
}
use of java.security.AlgorithmParameters in project jdk8u_jdk by JetBrains.
the class TestCipherKeyWrapperTest method wrapTest.
private void wrapTest(String transformation, String wrapAlgo, Key initKey, Key wrapKey, int keyType, boolean isPBE) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, InvalidAlgorithmParameterException {
String algo = transformation.split("/")[0];
boolean isAESBlowfish = algo.indexOf("AES") != -1 || algo.indexOf("Blowfish") != -1;
AlgorithmParameters aps = null;
AlgorithmParameterSpec pbeParams = null;
if (isPBE) {
byte[] salt = new byte[8];
int iterCnt = 1000;
new Random().nextBytes(salt);
pbeParams = new PBEParameterSpec(salt, iterCnt);
}
// Wrap & UnWrap operation
Cipher wrapCI = Cipher.getInstance(wrapAlgo);
if (isPBE && !isAESBlowfish) {
wrapCI.init(Cipher.WRAP_MODE, initKey, pbeParams);
} else if (isAESBlowfish) {
wrapCI.init(Cipher.WRAP_MODE, initKey);
aps = wrapCI.getParameters();
} else {
wrapCI.init(Cipher.WRAP_MODE, initKey);
}
out.println("keysize : " + wrapKey.getEncoded().length);
byte[] keyWrapper = wrapCI.wrap(wrapKey);
if (isPBE && !isAESBlowfish) {
wrapCI.init(Cipher.UNWRAP_MODE, initKey, pbeParams);
} else if (isAESBlowfish) {
wrapCI.init(Cipher.UNWRAP_MODE, initKey, aps);
} else {
wrapCI.init(Cipher.UNWRAP_MODE, initKey);
}
Key unwrappedKey = wrapCI.unwrap(keyWrapper, algo, keyType);
// Comparison
if (!Arrays.equals(wrapKey.getEncoded(), unwrappedKey.getEncoded())) {
throw new RuntimeException("Comparation failed testing " + transformation + ":" + wrapAlgo + ":" + keyType);
}
}
use of java.security.AlgorithmParameters in project jdk8u_jdk by JetBrains.
the class PBEParametersTest method main.
public static void main(String[] args) throws Exception {
PBEKeySpec ks = new PBEKeySpec(PASSWORD);
for (int i = 0; i < PBE_ALGOS.length; i++) {
String algo = PBE_ALGOS[i];
SecretKeyFactory skf = SecretKeyFactory.getInstance(algo);
SecretKey key = skf.generateSecret(ks);
Cipher c = Cipher.getInstance(algo, "SunJCE");
c.init(Cipher.ENCRYPT_MODE, key);
// force the generation of parameters
c.doFinal(new byte[10]);
AlgorithmParameters params = c.getParameters();
if (!params.getAlgorithm().equalsIgnoreCase(algo)) {
throw new Exception("expect: " + algo + ", but got: " + params.getAlgorithm());
}
System.out.println(algo + "...done...");
}
System.out.println("Test Passed");
}
use of java.security.AlgorithmParameters in project jdk8u_jdk by JetBrains.
the class KeyWrapper method doTest.
private static void doTest(String provider, String algo) throws Exception {
SecretKey key;
SecretKey keyToWrap;
// init a secret Key
KeyGenerator kg = KeyGenerator.getInstance(AES, PROVIDER);
kg.init(KEY_LENGTH);
key = kg.generateKey();
keyToWrap = kg.generateKey();
// initialization
Cipher cipher = Cipher.getInstance(algo, provider);
cipher.init(Cipher.WRAP_MODE, key);
AlgorithmParameters params = cipher.getParameters();
// wrap the key
byte[] keyWrapper = cipher.wrap(keyToWrap);
try {
// check if we can't wrap it again with the same key/IV
keyWrapper = cipher.wrap(keyToWrap);
throw new RuntimeException("FAILED: expected IllegalStateException hasn't " + "been thrown ");
} catch (IllegalStateException ise) {
System.out.println(ise.getMessage());
System.out.println("Expected exception");
}
// unwrap the key
cipher.init(Cipher.UNWRAP_MODE, key, params);
cipher.unwrap(keyWrapper, algo, Cipher.SECRET_KEY);
// check if we can unwrap second time
Key unwrapKey = cipher.unwrap(keyWrapper, algo, Cipher.SECRET_KEY);
if (!Arrays.equals(keyToWrap.getEncoded(), unwrapKey.getEncoded())) {
throw new RuntimeException("FAILED: original and unwrapped keys are not equal");
}
}
use of java.security.AlgorithmParameters in project jdk8u_jdk by JetBrains.
the class TextPKCS5PaddingTest method main.
public static void main(String[] args) throws Exception {
Provider provider = Security.getProvider("SunJCE");
if (provider == null) {
throw new RuntimeException("SunJCE provider not exist");
}
// generate no-padding cipher with secret key
Cipher c = Cipher.getInstance("DES/CBC/NoPadding", provider);
KeyGenerator kgen = KeyGenerator.getInstance("DES", provider);
SecretKey skey = kgen.generateKey();
// this is the improperly padded plaintext
c.init(Cipher.ENCRYPT_MODE, skey);
// encrypt plaintext
byte[] cipher = c.doFinal(PLAIN_TEXT);
AlgorithmParameters params = c.getParameters();
// generate cipher that enforces PKCS5 padding
c = Cipher.getInstance("DES/CBC/PKCS5Padding", provider);
c.init(Cipher.DECRYPT_MODE, skey, params);
try {
c.doFinal(cipher);
throw new RuntimeException("ERROR: Expected BadPaddingException not thrown");
} catch (BadPaddingException expected) {
out.println("Expected BadPaddingException thrown");
}
}
Aggregations