Пример #1
0
 public static byte[] decryptRSA(byte[] encrypted, PublicKey privateKey)
 {
     if (encrypted == null)
     {
         throw new Exception("Data is empty");
     }
     if (encrypted.length == 0)
     {
         throw new Exception("Data length is zero");
     }
     if (privateKey == null)
     {
         throw new Exception("Certificate Private Key is empty");
     }
     if (privateKey.getAlgorithm().compareTo("RSA") != 0)
     {
         throw new Exception("Certificate Algorithm is not RSA");
     }
     try {
         byte[]     encryptionByte = null;
         CipherData cipher         = Cipher.getInstance("RSA/ECB/PKCS1Padding");
         cipher.init(2, privateKey);
         byte[] someDecrypted = cipher.update(encrypted);
         byte[] moreDecrypted = cipher.doFinal();
         byte[] decrypted     = new byte[someDecrypted.length + moreDecrypted.length];
         System.arraycopy(someDecrypted, 0, decrypted, 0, someDecrypted.length);
         System.arraycopy(moreDecrypted, 0, decrypted, someDecrypted.length, moreDecrypted.length);
         return(decrypted);
     }
     catch (Exception ex) {
         throw ex;
     }
 }