use of java.security.spec.KeySpec in project robovm by robovm.
the class KeyFactoryTest method testGetKeySpec.
@SuppressWarnings("unchecked")
public void testGetKeySpec() {
KeyFactory factory = null;
try {
factory = KeyFactory.getInstance(TEST_KEYFACTORY_NAME);
} catch (NoSuchAlgorithmException e) {
fail("unexpected exception: " + e);
}
assertNotNull(factory);
{
Key[] keys = { new TestPrivateKey(), new TestPublicKey(), new TestPrivateKey(new byte[] { 42, 41, 40 }), new TestPublicKey(new byte[] { 40, 41, 42 }) };
Class[] keySpecs = { TestPrivateKeySpec.class, TestPublicKeySpec.class, TestPrivateKeySpec.class, TestPublicKeySpec.class };
for (int i = 0; i < keys.length; i++) {
Key key = keys[i];
Class keySpec = keySpecs[i];
String message = "getKeySpec(" + key.toString() + ", " + keySpec.toString() + ")";
try {
KeySpec spec = factory.getKeySpec(key, keySpec);
assertNotNull(spec);
assertTrue(spec.getClass() == keySpec);
} catch (InvalidKeySpecException e) {
fail("unexpected exception: " + e);
}
}
}
{
Key[] keys = { new AnotherKey(), null, new TestPrivateKey(), null };
Class[] keySpecs = { KeySpec.class, TestPrivateKeySpec.class, null, null };
Class[] exceptions = { InvalidKeySpecException.class, NullPointerException.class, InvalidKeySpecException.class, NullPointerException.class };
for (int i = 0; i < keys.length; i++) {
Key key = keys[i];
Class keySpec = keySpecs[i];
exceptionThrown = false;
String message = "getKeySpec(" + (key == null ? "null" : key.toString()) + ", " + (keySpec == null ? "null" : keySpec.toString()) + ")";
try {
factory.getKeySpec(key, keySpec);
} catch (Exception e) {
checkException(message, e, exceptions[i]);
} finally {
checkException(message, null, exceptions[i]);
}
}
}
}
use of java.security.spec.KeySpec in project robovm by robovm.
the class DSAPrivateKeySpecTest method testDSAPrivateKeySpec.
/**
* Test for constructor
*/
public final void testDSAPrivateKeySpec() {
KeySpec ks = new DSAPrivateKeySpec(new BigInteger("1"), new BigInteger("2"), new BigInteger("3"), new BigInteger("4"));
assertTrue(ks instanceof DSAPrivateKeySpec);
}
use of java.security.spec.KeySpec in project robovm by robovm.
the class BcKeyStoreSpi method decodeKey.
private Key decodeKey(DataInputStream dIn) throws IOException {
int keyType = dIn.read();
String format = dIn.readUTF();
String algorithm = dIn.readUTF();
byte[] enc = new byte[dIn.readInt()];
KeySpec spec;
dIn.readFully(enc);
if (format.equals("PKCS#8") || format.equals("PKCS8")) {
spec = new PKCS8EncodedKeySpec(enc);
} else if (format.equals("X.509") || format.equals("X509")) {
spec = new X509EncodedKeySpec(enc);
} else if (format.equals("RAW")) {
return new SecretKeySpec(enc, algorithm);
} else {
throw new IOException("Key format " + format + " not recognised!");
}
try {
switch(keyType) {
case KEY_PRIVATE:
return KeyFactory.getInstance(algorithm, BouncyCastleProvider.PROVIDER_NAME).generatePrivate(spec);
case KEY_PUBLIC:
return KeyFactory.getInstance(algorithm, BouncyCastleProvider.PROVIDER_NAME).generatePublic(spec);
case KEY_SECRET:
return SecretKeyFactory.getInstance(algorithm, BouncyCastleProvider.PROVIDER_NAME).generateSecret(spec);
default:
throw new IOException("Key type " + keyType + " not recognised!");
}
} catch (Exception e) {
throw new IOException("Exception creating key: " + e.toString());
}
}
use of java.security.spec.KeySpec in project robovm by robovm.
the class BaseSecretKeyFactory method engineGetKeySpec.
protected KeySpec engineGetKeySpec(SecretKey key, Class keySpec) throws InvalidKeySpecException {
if (keySpec == null) {
throw new InvalidKeySpecException("keySpec parameter is null");
}
if (key == null) {
throw new InvalidKeySpecException("key parameter is null");
}
if (SecretKeySpec.class.isAssignableFrom(keySpec)) {
return new SecretKeySpec(key.getEncoded(), algName);
}
try {
Class[] parameters = { byte[].class };
Constructor c = keySpec.getConstructor(parameters);
Object[] p = new Object[1];
p[0] = key.getEncoded();
return (KeySpec) c.newInstance(p);
} catch (Exception e) {
throw new InvalidKeySpecException(e.toString());
}
}
use of java.security.spec.KeySpec in project cloudstack by apache.
the class VncClient method encodePassword.
/**
* Encode password using DES encryption with given challenge.
*
* @param challenge
* a random set of bytes.
* @param password
* a password
* @return DES hash of password and challenge
*/
public byte[] encodePassword(byte[] challenge, String password) throws Exception {
// VNC password consist of up to eight ASCII characters.
// Padding
byte[] key = { 0, 0, 0, 0, 0, 0, 0, 0 };
byte[] passwordAsciiBytes = password.getBytes(RfbConstants.CHARSET);
System.arraycopy(passwordAsciiBytes, 0, key, 0, Math.min(password.length(), 8));
// Flip bytes (reverse bits) in key
for (int i = 0; i < key.length; i++) {
key[i] = flipByte(key[i]);
}
KeySpec desKeySpec = new DESKeySpec(key);
SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = secretKeyFactory.generateSecret(desKeySpec);
Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] response = cipher.doFinal(challenge);
return response;
}
Aggregations