Пример #1
0
  private void sealedObjectTest() throws Exception {
    KeyPairGenerator kpg = KeyPairGenerator.getInstance("ECIES");
    kpg.initialize(new ECGenParameterSpec("secp256r1"));
    KeyPair keyPair = kpg.generateKeyPair();

    Cipher cipher = Cipher.getInstance("ECIES");
    cipher.init(Cipher.ENCRYPT_MODE, keyPair.getPublic());

    String toEncrypt = "Hello";

    // Check that cipher works ok
    cipher.doFinal(toEncrypt.getBytes());

    // Using a SealedObject to encrypt the same string fails with a NullPointerException
    SealedObject sealedObject = new SealedObject(toEncrypt, cipher);

    cipher.init(Cipher.DECRYPT_MODE, keyPair.getPrivate());

    String result = (String) sealedObject.getObject(cipher);

    isTrue("result wrong", result.equals(toEncrypt));

    result = (String) sealedObject.getObject(keyPair.getPrivate());

    isTrue("result wrong", result.equals(toEncrypt));
  }
Пример #2
0
    /**
     * Decrypt and extract a message from SealedObject
     *
     * @param encryptedMsgObj - encrypted SealedObject
     * @return message
     */
    public String decryptMsg(SealedObject encryptedMsgObj) {

      String plainText = null;

      try {
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, sessionKey);
        plainText = (String) encryptedMsgObj.getObject(cipher);
      } catch (NoSuchAlgorithmException e) {
        System.out.println("Error: No algorithm entered exists.");
        System.exit(1);
      } catch (NoSuchPaddingException e) {
        System.out.println("Error: transformation contains a padding scheme is not available.");
        System.exit(1);
      } catch (InvalidKeyException e) {
        System.out.println("Error: the session key is invalid.");
        System.exit(1);
      } catch (ClassNotFoundException e) {
        System.out.println("Error: cannot typecast to byte[].");
        System.exit(1);
      } catch (IllegalBlockSizeException e) {
        System.out.println("Error: the block size is invalid.");
        System.exit(1);
      } catch (BadPaddingException e) {
        System.out.println("Error: decrypted data is not bounded by the valid padding bytes.");
        System.exit(1);
      } catch (IOException e) {
        System.out.println("Error: cannot decrypt message.");
      }

      return plainText;
    }
Пример #3
0
  public static Keyring load(File keyringFile, char[] passphrase) throws IOException {

    ObjectInputStream is = null;

    try {
      is = new ObjectInputStream(new FileInputStream(keyringFile));

      try {
        byte[] salt = (byte[]) is.readObject();

        Cipher cipher = getCipher(passphrase, salt, Cipher.DECRYPT_MODE);

        SealedObject skr = (SealedObject) is.readObject();
        return (Keyring) skr.getObject(cipher);
      } catch (GeneralSecurityException e) {
        throw new RuntimeException(e);
      } catch (ClassNotFoundException e) {
        throw new RuntimeException(e);
      }
    } finally {
      if (is != null) is.close();
    }
  }