Ejemplo n.º 1
0
  protected Key engineUnwrap(byte[] wrappedKey, String wrappedKeyAlgorithm, int wrappedKeyType)
      throws InvalidKeyException, NoSuchAlgorithmException {
    byte[] encoded;
    try {
      if (wrapEngine == null) {
        encoded = engineDoFinal(wrappedKey, 0, wrappedKey.length);
      } else {
        encoded = wrapEngine.unwrap(wrappedKey, 0, wrappedKey.length);
      }
    } catch (InvalidCipherTextException e) {
      throw new InvalidKeyException(e.getMessage());
    } catch (BadPaddingException e) {
      throw new InvalidKeyException(e.getMessage());
    } catch (IllegalBlockSizeException e2) {
      throw new InvalidKeyException(e2.getMessage());
    }

    if (wrappedKeyType == Cipher.SECRET_KEY) {
      return new SecretKeySpec(encoded, wrappedKeyAlgorithm);
    } else if (wrappedKeyAlgorithm.equals("") && wrappedKeyType == Cipher.PRIVATE_KEY) {
      /*
       * The caller doesn't know the algorithm as it is part of
       * the encrypted data.
       */
      try {
        PrivateKeyInfo in = PrivateKeyInfo.getInstance(encoded);

        PrivateKey privKey = BouncyCastleProvider.getPrivateKey(in);

        if (privKey != null) {
          return privKey;
        } else {
          throw new InvalidKeyException(
              "algorithm " + in.getPrivateKeyAlgorithm().getAlgorithm() + " not supported");
        }
      } catch (Exception e) {
        throw new InvalidKeyException("Invalid key encoding.");
      }
    } else {
      try {
        KeyFactory kf =
            KeyFactory.getInstance(wrappedKeyAlgorithm, BouncyCastleProvider.PROVIDER_NAME);

        if (wrappedKeyType == Cipher.PUBLIC_KEY) {
          return kf.generatePublic(new X509EncodedKeySpec(encoded));
        } else if (wrappedKeyType == Cipher.PRIVATE_KEY) {
          return kf.generatePrivate(new PKCS8EncodedKeySpec(encoded));
        }
      } catch (NoSuchProviderException e) {
        throw new InvalidKeyException("Unknown key type " + e.getMessage());
      } catch (InvalidKeySpecException e2) {
        throw new InvalidKeyException("Unknown key type " + e2.getMessage());
      }

      throw new InvalidKeyException("Unknown key type " + wrappedKeyType);
    }
  }
 protected Key engineUnwrap(byte[] paramArrayOfByte, String paramString, int paramInt) {
   try {
     if (this.aXL == null) {
       paramArrayOfByte = engineDoFinal(paramArrayOfByte, 0, paramArrayOfByte.length);
     } else {
       paramArrayOfByte = this.aXL.ﹳ(paramArrayOfByte, 0, paramArrayOfByte.length);
     }
   } catch (InvalidCipherTextException paramArrayOfByte) {
     throw new InvalidKeyException(paramArrayOfByte.getMessage());
   } catch (BadPaddingException paramArrayOfByte) {
     throw new InvalidKeyException(paramArrayOfByte.getMessage());
   } catch (IllegalBlockSizeException paramArrayOfByte) {
     throw new InvalidKeyException(paramArrayOfByte.getMessage());
   }
   if (paramInt == 3) {
     return new SecretKeySpec(paramArrayOfByte, paramString);
   }
   if ((paramString.equals("")) && (paramInt == 2)) {
     try {
       paramArrayOfByte = PrivateKeyInfo.ﹷ(paramArrayOfByte);
       paramString = BouncyCastleProvider.ˏ(paramArrayOfByte);
       if (paramString != null) {
         return paramString;
       }
       throw new InvalidKeyException("algorithm " + paramArrayOfByte.fW().fK() + " not supported");
     } catch (Exception paramArrayOfByte) {
       throw new InvalidKeyException("Invalid key encoding.");
     }
   }
   try {
     paramString = KeyFactory.getInstance(paramString, "SC");
     if (paramInt == 1) {
       paramArrayOfByte = paramString.generatePublic(new X509EncodedKeySpec(paramArrayOfByte));
       return paramArrayOfByte;
     }
     if (paramInt == 2) {
       paramArrayOfByte = paramString.generatePrivate(new PKCS8EncodedKeySpec(paramArrayOfByte));
       return paramArrayOfByte;
     }
   } catch (NoSuchProviderException paramArrayOfByte) {
     throw new InvalidKeyException("Unknown key type " + paramArrayOfByte.getMessage());
   } catch (InvalidKeySpecException paramArrayOfByte) {
     throw new InvalidKeyException("Unknown key type " + paramArrayOfByte.getMessage());
   }
   throw new InvalidKeyException("Unknown key type " + paramInt);
 }
Ejemplo n.º 3
0
 public static void disableSpongyCastleOnLollipop() {
   if (Build.VERSION.SDK_INT == 21) {
     Security.removeProvider(spongyCastleProvider.getName());
   }
 }