Java Code Examples for org.keyczar.Crypter#decrypt()

The following examples show how to use org.keyczar.Crypter#decrypt() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UtilTest.java    From passopolis-server with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testCreateExportKey() throws KeyczarException {
	// create the key, export the public key; 1024 bits is the smallest size
	GenericKeyczar keyczar = Util.createKey(
	    DefaultKeyType.RSA_PRIV, KeyPurpose.DECRYPT_AND_ENCRYPT, 1024);
	KeyczarReader publicKeyReader = Util.exportPublicKeys(keyczar);
	Encrypter encrypter = new Encrypter(publicKeyReader);

	// test that it works
	String ciphertext = encrypter.encrypt(MESSAGE);
	Crypter crypter = new Crypter(Util.readerFromKeyczar(keyczar));
	String decrypted = crypter.decrypt(ciphertext);
	assertEquals(MESSAGE, decrypted);

	// test a session
	StringBuilder longMessage = new StringBuilder("hello message ");
	while (longMessage.length() < 500) {
		longMessage.append(longMessage);
	}

	ciphertext = Util.encryptWithSession(encrypter, longMessage.toString());
	assertEquals(longMessage.toString(), Util.decryptWithSession(crypter, ciphertext));
}
 
Example 2
Source File: KeyczarJsonReaderTest.java    From passopolis-server with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testSimple() throws KeyczarException {
	KeyczarReader reader = new KeyczarJsonReader(JSON_KEY);
	KeyMetadata metadata = KeyMetadata.read(reader.getMetadata());
	assertEquals(0, metadata.getPrimaryVersion().getVersionNumber());
	assertEquals(KeyPurpose.DECRYPT_AND_ENCRYPT, metadata.getPurpose());
	assertEquals("Imported AES", metadata.getName());
	assertEquals(1, metadata.getVersions().size());
	assertEquals(0, metadata.getVersions().get(0).getVersionNumber());
	assertFalse(metadata.getVersions().get(0).isExportable());

	Crypter crypter = new Crypter(reader);
	String plaintext = "hello world";
	String encrypted = crypter.encrypt(plaintext);
	assertTrue(!encrypted.equals(plaintext));

	String decrypted = crypter.decrypt(encrypted);
	assertEquals(plaintext, decrypted);

	// TODO: Add an old version of a key; test decrypting with it
}
 
Example 3
Source File: KeyczarEncryptor.java    From appinventor-extensions with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * <p>Uses local Keyczar client to decrypt the byte array.
 *
 * @throws EncryptionException if any underlying component fails
 */
@Override
public byte[] decrypt(byte[] encrypted) throws EncryptionException {
  try {
    Crypter crypter = getCrypter();
    return crypter.decrypt(encrypted);
  } catch (KeyczarException e) {
    throw new EncryptionException(e);
  }
}
 
Example 4
Source File: UtilTest.java    From passopolis-server with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testGenerateKeyczarReader() throws KeyczarException {
  KeyczarReader reader = Util.generateKeyczarReader(DefaultKeyType.AES, KeyPurpose.DECRYPT_AND_ENCRYPT);
  Crypter crypter = new Crypter(reader);

  // test that it works
  String ciphertext = crypter.encrypt(MESSAGE);
  String decrypted = crypter.decrypt(ciphertext);
  assertEquals(MESSAGE, decrypted);
}
 
Example 5
Source File: UtilTest.java    From passopolis-server with GNU General Public License v3.0 5 votes vote down vote up
protected void verifyKeyCompatibility(GenericKeyczar keyczar,
    Crypter roundtripped) throws KeyczarException {
  
  String ciphertext = roundtripped.encrypt(MESSAGE);
Crypter original = new Crypter(Util.readerFromKeyczar(keyczar));
String decrypted = original.decrypt(ciphertext);
assertEquals(MESSAGE, decrypted);

  ciphertext = original.encrypt(MESSAGE);
  decrypted = roundtripped.decrypt(ciphertext);
  assertEquals(MESSAGE, decrypted);
  
}
 
Example 6
Source File: RoundTripper.java    From passopolis-server with GNU General Public License v3.0 5 votes vote down vote up
public static String decrypt(String keyPath, String encryptedPath, String expectedMessage,
    DefaultKeyType expectedType, String keyPassword) throws KeyczarException {
  // Read the key, possibly decrypting using a password
  KeyczarReader reader = Util.readJsonFromPath(keyPath);
  if (keyPassword != null) {
    reader = new KeyczarPBEReader(reader, keyPassword);
  }

  KeyMetadata metadata = KeyMetadata.read(reader.getMetadata());
  if (metadata.getType() != expectedType) {
    throw new RuntimeException("Unexpected key type: " + metadata.getType());
  }

  Crypter key = new Crypter(reader);
  String data = Util.readFile(encryptedPath);
  String output = key.decrypt(data);

  if (expectedMessage != null) {
    if (output.equals(expectedMessage)) {
      System.out.println(encryptedPath + " decrypts successfully");
    } else {
      System.err.println("Decryption does not match?\n" + output);
      System.exit(1);
    }
  }
  return output;
}
 
Example 7
Source File: AES.java    From JavaSecurity with Apache License 2.0 4 votes vote down vote up
private static String decrypt(String ciphertext) throws KeyczarException {
    Crypter crypter = new Crypter(KEYSET_PATH);
    return crypter.decrypt(ciphertext);
}
 
Example 8
Source File: RSA.java    From JavaSecurity with Apache License 2.0 4 votes vote down vote up
private static String decrypt(String ciphertext) throws KeyczarException {
    Crypter crypter = new Crypter(KEYSET_PATH);
    return crypter.decrypt(ciphertext);
}