示例#1
0
 public static String decrypt4AES(String content, String key) {
   try {
     byte[] decryptFrom = Base64.decodeBase64(content);
     IvParameterSpec zeroIv = new IvParameterSpec(key.getBytes("utf-8"));
     SecretKeySpec key1 = new SecretKeySpec(key.getBytes("utf-8"), "AES");
     Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
     cipher.init(Cipher.DECRYPT_MODE, key1, zeroIv);
     byte decryptedData[] = cipher.doFinal(decryptFrom);
     return new String(decryptedData); // 加密
   } catch (NoSuchAlgorithmException e) {
     e.printStackTrace();
   } catch (NoSuchPaddingException e) {
     e.printStackTrace();
   } catch (InvalidKeyException e) {
     e.printStackTrace();
   } catch (UnsupportedEncodingException e) {
     e.printStackTrace();
   } catch (IllegalBlockSizeException e) {
     e.printStackTrace();
   } catch (BadPaddingException e) {
     e.printStackTrace();
   } catch (Exception e) {
     e.printStackTrace();
   }
   return null;
 }
示例#2
0
  public Crypto(String passPhrase) throws Exception {

    set = new HashSet<Point>();
    random = new Random();
    for (int i = 0; i < 10; i++) {
      point = new Point(random.nextInt(1000), random.nextInt(2000));
      set.add(point);
    }
    last = random.nextInt(5000);
    try {
      // Create Key
      // byte key[] = passPhrase.getBytes();
      ois = new ObjectInputStream(new FileInputStream("keyfile"));
      DESKeySpec desKeySpec = new DESKeySpec((byte[]) ois.readObject());
      SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
      SecretKey secretKey = keyFactory.generateSecret(desKeySpec);

      // Create Cipher
      eCipher = Cipher.getInstance("DES/CFB8/NoPadding");
      dCipher = Cipher.getInstance("DES/CFB8/NoPadding");

      // Create the ciphers
      eCipher.init(Cipher.ENCRYPT_MODE, secretKey);
      dCipher.init(Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec((byte[]) ois.readObject()));

    } catch (javax.crypto.NoSuchPaddingException e) {
      e.printStackTrace();
    } catch (java.security.NoSuchAlgorithmException e) {
      e.printStackTrace();
    } catch (java.security.InvalidKeyException e) {
      e.printStackTrace();
    }
  }
示例#3
0
  private void cambiarBtnActionPerformed(
      java.awt.event.ActionEvent evt) { // GEN-FIRST:event_cambiarBtnActionPerformed
    char[] claveActualChar = claveActualTF.getPassword();
    char[] claveNuevaChar = claveNuevaTF.getPassword();

    String claveActual = new String(claveActualChar);
    String claveNueva = new String(claveNuevaChar);

    try {
      jp.cambiarClave("Administrador", claveActual, claveNueva);
      JOptionPane.showMessageDialog(
          rootPane, "Contraseña cambiada exitosamente.", "Mensaje", JOptionPane.OK_OPTION);
      this.setVisible(false);
    } catch (NoSuchAlgorithmException ex) {
      JOptionPane.showMessageDialog(rootPane, ex.getMessage(), "Error", JOptionPane.OK_OPTION);
    } catch (NoSuchPaddingException ex) {
      JOptionPane.showMessageDialog(rootPane, ex.getMessage(), "Error", JOptionPane.OK_OPTION);
    } catch (IOException ex) {
      JOptionPane.showMessageDialog(rootPane, ex.getMessage(), "Error", JOptionPane.OK_OPTION);
    } catch (ClassNotFoundException ex) {
      JOptionPane.showMessageDialog(rootPane, ex.getMessage(), "Error", JOptionPane.OK_OPTION);
    } catch (InvalidKeyException ex) {
      JOptionPane.showMessageDialog(rootPane, ex.getMessage(), "Error", JOptionPane.OK_OPTION);
    } catch (IllegalBlockSizeException ex) {
      JOptionPane.showMessageDialog(rootPane, ex.getMessage(), "Error", JOptionPane.OK_OPTION);
    } catch (BadPaddingException ex) {
      JOptionPane.showMessageDialog(rootPane, ex.getMessage(), "Error", JOptionPane.OK_OPTION);
    } catch (ClaveNoValidaException ex) {
      JOptionPane.showMessageDialog(rootPane, ex.getMessage(), "Error", JOptionPane.OK_OPTION);
    } catch (IllegalArgumentException ex) {
      JOptionPane.showMessageDialog(rootPane, ex.getMessage(), "Error", JOptionPane.OK_OPTION);
    }
  } // GEN-LAST:event_cambiarBtnActionPerformed
示例#4
0
  private static byte[] doAes(int mode, byte[] input, String password) {
    try {
      KeyGenerator keygen = KeyGenerator.getInstance(AES_ALGORITHM_NAME);
      keygen.init(128, new SecureRandom(password.getBytes()));
      SecretKey secretKey = keygen.generateKey();

      byte[] enCodeFormat = secretKey.getEncoded();
      SecretKeySpec key = new SecretKeySpec(enCodeFormat, AES_ALGORITHM_NAME);

      Cipher cipher = Cipher.getInstance(AES_ALGORITHM_NAME);
      cipher.init(Cipher.DECRYPT_MODE, key);

      return cipher.doFinal(input);
    } catch (NoSuchAlgorithmException e) {
      e.printStackTrace();
    } catch (NoSuchPaddingException e) {
      e.printStackTrace();
    } catch (InvalidKeyException e) {
      e.printStackTrace();
    } catch (IllegalBlockSizeException e) {
      e.printStackTrace();
    } catch (BadPaddingException e) {
      e.printStackTrace();
    }
    return null;
  }
  public void downloadFile(
      String fileUrl, File directory, String rString, File directoryFolder, String cifrado) {
    try {
      directorio = directoryFolder;
      URL url = new URL(fileUrl);
      HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
      // urlConnection.setRequestMethod("GET");
      // urlConnection.setDoOutput(true);
      urlConnection.connect();
      InputStream inputStream = urlConnection.getInputStream();
      if (cifrado.equals("AES")) {
        aesCypher(rString, inputStream, directory);
      } else {
        rsaCypher(rString, inputStream, directory);
      }

    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (MalformedURLException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } catch (InvalidKeyException e) {
      e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
      e.printStackTrace();
    } catch (NoSuchPaddingException e) {
      e.printStackTrace();
    } catch (IllegalBlockSizeException e) {
      e.printStackTrace();
    } catch (BadPaddingException e) {
      e.printStackTrace();
    }
  }
 public CipherSpi(OAEPParameterSpec pSpec) {
   try {
     initFromSpec(pSpec);
   } catch (NoSuchPaddingException e) {
     throw new IllegalArgumentException(e.getMessage());
   }
 }
示例#7
0
 private void init() {
   try {
     /**
      * 为指定算法生成一个 KeyGenerator 对象。 此类提供(对称)密钥生成器的功能。 密钥生成器是使用此类的某个 getInstance 类方法构造的。 KeyGenerator
      * 对象可重复使用,也就是说,在生成密钥后, 可以重复使用同一 KeyGenerator 对象来生成进一步的密钥。 生成密钥的方式有两种:与算法无关的方式,以及特定于算法的方式。
      * 两者之间的惟一不同是对象的初始化: 与算法无关的初始化 所有密钥生成器都具有密钥长度 和随机源 的概念。 此 KeyGenerator 类中有一个 init
      * 方法,它可采用这两个通用概念的参数。 还有一个只带 keysize 参数的 init 方法, 它使用具有最高优先级的提供程序的 SecureRandom 实现作为随机源
      * (如果安装的提供程序都不提供 SecureRandom 实现,则使用系统提供的随机源)。 此 KeyGenerator 类还提供一个只带随机源参数的 inti 方法。
      * 因为调用上述与算法无关的 init 方法时未指定其他参数, 所以由提供程序决定如何处理将与每个密钥相关的特定于算法的参数(如果有)。 特定于算法的初始化
      * 在已经存在特定于算法的参数集的情况下, 有两个具有 AlgorithmParameterSpec 参数的 init 方法。 其中一个方法还有一个 SecureRandom 参数,
      * 而另一个方法将已安装的高优先级提供程序的 SecureRandom 实现用作随机源 (或者作为系统提供的随机源,如果安装的提供程序都不提供 SecureRandom 实现)。
      * 如果客户端没有显式地初始化 KeyGenerator(通过调用 init 方法), 每个提供程序必须提供(和记录)默认初始化。
      */
     keyGen = KeyGenerator.getInstance("AES");
   } catch (NoSuchAlgorithmException e) {
     e.printStackTrace();
   }
   // 初始化此密钥生成器,使其具有确定的密钥长度。
   keyGen.init(128); // 128位的AES加密
   try {
     // 生成一个实现指定转换的 Cipher 对象。
     cipher = Cipher.getInstance(algorithmStr);
   } catch (NoSuchAlgorithmException e) {
     e.printStackTrace();
   } catch (NoSuchPaddingException e) {
     e.printStackTrace();
   }
   // 标识已经初始化过了的字段
   isInited = true;
 }
示例#8
0
 public String desencripta(byte[] message) throws EncriptaException {
   try {
     final MessageDigest md = MessageDigest.getInstance("md5");
     final byte[] digestOfPassword = md.digest(clave.getBytes("utf-8"));
     final byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
     for (int j = 0, k = 16; j < 8; ) {
       keyBytes[k++] = keyBytes[j++];
     }
     final SecretKey key = new SecretKeySpec(keyBytes, "DESede");
     final IvParameterSpec iv = new IvParameterSpec(new byte[8]);
     final Cipher decipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
     decipher.init(Cipher.DECRYPT_MODE, key, iv);
     final byte[] plainText = decipher.doFinal(message);
     return new String(plainText, "UTF-8");
   } catch (NoSuchAlgorithmException ex) {
     throw new EncriptaException(ex.getMessage());
   } catch (UnsupportedEncodingException ex) {
     throw new EncriptaException(ex.getMessage());
   } catch (NoSuchPaddingException ex) {
     throw new EncriptaException(ex.getMessage());
   } catch (InvalidKeyException ex) {
     throw new EncriptaException(ex.getMessage());
   } catch (InvalidAlgorithmParameterException ex) {
     throw new EncriptaException(ex.getMessage());
   } catch (IllegalBlockSizeException ex) {
     throw new EncriptaException(ex.getMessage());
   } catch (BadPaddingException ex) {
     throw new EncriptaException(ex.getMessage());
   }
 }
示例#9
0
文件: AES.java 项目: zengboming/JAVA
 protected String encrypt(String content, String key) {
   try {
     KeyGenerator kgen = KeyGenerator.getInstance("AES");
     kgen.init(128, new SecureRandom(key.getBytes()));
     SecretKey secretKey = kgen.generateKey();
     byte[] enCodeFormat = secretKey.getEncoded();
     SecretKeySpec secretKeySpec = new SecretKeySpec(enCodeFormat, "AES");
     Cipher cipher = Cipher.getInstance("AES");
     byte[] byteContent = content.getBytes("utf-8");
     cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
     byte[] byteRresult = cipher.doFinal(byteContent);
     StringBuffer sb = new StringBuffer();
     for (int i = 0; i < byteRresult.length; i++) {
       String hex = Integer.toHexString(byteRresult[i] & 0xFF);
       if (hex.length() == 1) {
         hex = '0' + hex;
       }
       sb.append(hex.toUpperCase());
     }
     return sb.toString();
   } catch (NoSuchAlgorithmException e) {
     e.printStackTrace();
   } catch (NoSuchPaddingException e) {
     e.printStackTrace();
   } catch (InvalidKeyException e) {
     e.printStackTrace();
   } catch (UnsupportedEncodingException e) {
     e.printStackTrace();
   } catch (IllegalBlockSizeException e) {
     e.printStackTrace();
   } catch (BadPaddingException e) {
     e.printStackTrace();
   }
   return null;
 }
示例#10
0
  /**
   * 使用口令加密私钥(Base64编码)
   *
   * @return
   */
  public String encryptPrivateKey(String passphrase) {
    byte[] seeds = null;
    byte[] raw = mMyPrivKey.getEncoded();
    byte[] encrypted = null;

    Cipher cipher;
    try {
      seeds = getRawKey(passphrase.getBytes());
      SecretKeySpec skeySpec = new SecretKeySpec(seeds, PASS_ALGO);
      cipher = Cipher.getInstance(PASS_ALGO);
      cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
      encrypted = cipher.doFinal(raw);
    } catch (NoSuchAlgorithmException e) {
      e.printStackTrace();
    } catch (NoSuchPaddingException e) {
      e.printStackTrace();
    } catch (InvalidKeyException e) {
      e.printStackTrace();
    } catch (IllegalBlockSizeException e) {
      e.printStackTrace();
    } catch (BadPaddingException e) {
      e.printStackTrace();
    }

    String result = Base64.encodeToString(encrypted, Base64.DEFAULT);
    return result;
  }
示例#11
0
  /**
   * @param strKey key
   * @param strIvKey 矢量key
   * @param strContent 要解密的内容
   * @return 加密后的字符串,失败返回null
   */
  public static String encode(String strKey, String strIvKey, String strContent) {

    IvParameterSpec zeroIv = new IvParameterSpec(strIvKey.getBytes());
    SecretKeySpec key = new SecretKeySpec(strKey.getBytes(), "AES");
    Cipher cipher;
    byte[] encryptedData = null;

    try {
      cipher = Cipher.getInstance(TRANSFORMATION);
      cipher.init(Cipher.ENCRYPT_MODE, key, zeroIv);
      encryptedData = cipher.doFinal(strContent.getBytes(CHARSET));
    } catch (NoSuchAlgorithmException e) {
      e.printStackTrace();
    } catch (NoSuchPaddingException e) {
      e.printStackTrace();
    } catch (InvalidAlgorithmParameterException e) {
      e.printStackTrace();
      Log.e(TAG, "无效的矢量key");
    } catch (InvalidKeyException e) {
      e.printStackTrace();
      Log.e(TAG, "无效的key");
    } catch (BadPaddingException e) {
      e.printStackTrace();
    } catch (IllegalBlockSizeException e) {
      e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    }

    if (encryptedData == null) {
      return null;
    }
    return Base64Util.encode(encryptedData);
  }
示例#12
0
  private static byte[] decrypt(byte[] content, String password)
      throws InvalidAlgorithmParameterException {
    try {
      //            System.out.println("before decode.content.length = " + content.length);
      content = pad16Byte(content);
      byte[] keyStr = getKey(password);
      SecretKeySpec key = new SecretKeySpec(keyStr, "AES");
      byte[] ivbuf = new byte[16];
      for (int i = 0; i < ivbuf.length; i++) {
        ivbuf[i] = 0;
      }
      IvParameterSpec iv = new IvParameterSpec(ivbuf);

      Cipher cipher = Cipher.getInstance(algorithmStr);
      cipher.init(Cipher.DECRYPT_MODE, key, iv); //   ʼ
      //            System.out.println("after decode.content.length = " + content.length);
      byte[] result = cipher.doFinal(content);
      return result; //
    } catch (NoSuchAlgorithmException e) {
      e.printStackTrace();
    } catch (NoSuchPaddingException e) {
      e.printStackTrace();
    } catch (InvalidKeyException e) {
      e.printStackTrace();
    } catch (IllegalBlockSizeException e) {
      e.printStackTrace();
    } catch (BadPaddingException e) {
      e.printStackTrace();
    }
    return null;
  }
示例#13
0
 /**
  * 加密
  *
  * @param content 需要加密的内容
  * @param password 加密密码
  * @return
  */
 public static byte[] encryptAES(String content, String password) {
   try {
     KeyGenerator kgen = KeyGenerator.getInstance("AES");
     kgen.init(128, new SecureRandom(password.getBytes()));
     SecretKey secretKey = kgen.generateKey();
     byte[] enCodeFormat = secretKey.getEncoded();
     SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
     Cipher cipher = Cipher.getInstance("AES"); // 创建密码器
     byte[] byteContent = content.getBytes("utf-8");
     cipher.init(Cipher.ENCRYPT_MODE, key); // 初始化
     byte[] result = cipher.doFinal(byteContent);
     return result; // 加密
   } catch (NoSuchAlgorithmException e) {
     e.printStackTrace();
   } catch (NoSuchPaddingException e) {
     e.printStackTrace();
   } catch (InvalidKeyException e) {
     e.printStackTrace();
   } catch (UnsupportedEncodingException e) {
     e.printStackTrace();
   } catch (IllegalBlockSizeException e) {
     e.printStackTrace();
   } catch (BadPaddingException e) {
     e.printStackTrace();
   }
   return null;
 }
示例#14
0
 private static byte[] encrypt(String content, String password)
     throws InvalidAlgorithmParameterException {
   try {
     byte[] keyStr = getKey(password);
     SecretKeySpec key = new SecretKeySpec(keyStr, "AES");
     //            Cipher cipher = Cipher.getInstance(algorithmStr);//algorithmStr
     byte[] ivbuf = new byte[16];
     for (int i = 0; i < ivbuf.length; i++) {
       ivbuf[i] = 0;
     }
     IvParameterSpec iv = new IvParameterSpec(ivbuf);
     Cipher cipher = Cipher.getInstance(algorithmStr);
     byte[] byteContent = content.getBytes("utf-8");
     cipher.init(Cipher.ENCRYPT_MODE, key, iv); //   ʼ
     byte[] result = cipher.doFinal(pad16Byte(byteContent));
     return result; //
   } catch (NoSuchAlgorithmException e) {
     e.printStackTrace();
   } catch (NoSuchPaddingException e) {
     e.printStackTrace();
   } catch (InvalidKeyException e) {
     e.printStackTrace();
   } catch (UnsupportedEncodingException e) {
     e.printStackTrace();
   } catch (IllegalBlockSizeException e) {
     e.printStackTrace();
   } catch (BadPaddingException e) {
     e.printStackTrace();
   }
   return null;
 }
示例#15
0
  /** This encryption/decryption code doesn't match outputs with the Python code. It needs fixed. */
  public byte[] encrypt(String input) {
    byte[] encryptedBytes;
    try {
      SecretKeySpec key = generatekey();
      // Cipher encryptCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
      Cipher encryptCipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
      encryptCipher.init(Cipher.ENCRYPT_MODE, key);
      iv = encryptCipher.getIV();
      // Encrypt
      ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
      CipherOutputStream cipherOutputStream = new CipherOutputStream(outputStream, encryptCipher);
      cipherOutputStream.write(input.getBytes());
      cipherOutputStream.flush();
      cipherOutputStream.close();
      encryptedBytes = outputStream.toByteArray();
      outputStream.close();
    } catch (IOException e) {
      e.printStackTrace();
      return null;
    } catch (NoSuchPaddingException e) {
      e.printStackTrace();
      return null;
    } catch (NoSuchAlgorithmException e) {
      e.printStackTrace();
      return null;
    } catch (InvalidKeyException e) {
      e.printStackTrace();
      return null;
    }

    return encryptedBytes;
  }
示例#16
0
  public static String decryptIt(String value) {
    try {
      DESKeySpec keySpec = new DESKeySpec(cryptoPass.getBytes("UTF8"));
      SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
      SecretKey key = keyFactory.generateSecret(keySpec);

      byte[] encrypedPwdBytes = Base64.decode(value, Base64.DEFAULT);
      // cipher is not thread safe
      Cipher cipher = Cipher.getInstance("DES");
      cipher.init(Cipher.DECRYPT_MODE, key);
      byte[] decrypedValueBytes = (cipher.doFinal(encrypedPwdBytes));

      String decrypedValue = new String(decrypedValueBytes);
      Log.d(TAG, "Decrypted: " + value + " -> " + decrypedValue);
      return decrypedValue;

    } catch (InvalidKeyException e) {
      e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    } catch (InvalidKeySpecException e) {
      e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
      e.printStackTrace();
    } catch (BadPaddingException e) {
      e.printStackTrace();
    } catch (NoSuchPaddingException e) {
      e.printStackTrace();
    } catch (IllegalBlockSizeException e) {
      e.printStackTrace();
    }
    return value;
  }
示例#17
0
  public static void main(String[] args) {
    // TODO Auto-generated method stub

    try {
      RSACryptoProvider rsa = new RSACryptoProvider();
      byte[] data = rsa.Encrypt("Michael");
      System.out.println(new String(data));
      System.out.println(rsa.Decrypt(data));

    } catch (InvalidKeyException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (NoSuchPaddingException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IllegalBlockSizeException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (BadPaddingException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (InvalidKeySpecException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (FileNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
示例#18
0
文件: AES.java 项目: zengboming/JAVA
  protected String decrypt(String content, String key) {

    if (content.length() < 1) return null;
    byte[] byteRresult = new byte[content.length() / 2];
    for (int i = 0; i < content.length() / 2; i++) {
      int high = Integer.parseInt(content.substring(i * 2, i * 2 + 1), 16);
      int low = Integer.parseInt(content.substring(i * 2 + 1, i * 2 + 2), 16);
      byteRresult[i] = (byte) (high * 16 + low);
    }
    try {
      KeyGenerator kgen = KeyGenerator.getInstance("AES");
      kgen.init(128, new SecureRandom(key.getBytes()));
      SecretKey secretKey = kgen.generateKey();
      byte[] enCodeFormat = secretKey.getEncoded();
      SecretKeySpec secretKeySpec = new SecretKeySpec(enCodeFormat, "AES");
      Cipher cipher = Cipher.getInstance("AES");
      cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
      byte[] result = cipher.doFinal(byteRresult);
      return new String(result);
    } catch (NoSuchAlgorithmException e) {
      e.printStackTrace();
    } catch (NoSuchPaddingException e) {
      e.printStackTrace();
    } catch (InvalidKeyException e) {
      e.printStackTrace();
    } catch (IllegalBlockSizeException e) {
      e.printStackTrace();
    } catch (BadPaddingException e) {
      e.printStackTrace();
    }
    return null;
  }
示例#19
0
 /**
  * Do the encryption
  *
  * @param plaintext
  * @param inputLen
  * @param key
  * @param iv
  * @return
  */
 private static byte[] seafileEncrypt(
     @NonNull byte[] plaintext, int inputLen, @NonNull SecretKey key, @NonNull byte[] iv) {
   try {
     Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
     IvParameterSpec ivParams = new IvParameterSpec(iv);
     cipher.init(Cipher.ENCRYPT_MODE, key, ivParams);
     return cipher.doFinal(plaintext, 0, inputLen);
   } catch (NoSuchAlgorithmException e) {
     e.printStackTrace();
     Log.e(TAG, "NoSuchAlgorithmException " + e.getMessage());
     return null;
   } catch (InvalidKeyException e) {
     e.printStackTrace();
     Log.e(TAG, "InvalidKeyException " + e.getMessage());
     return null;
   } catch (InvalidAlgorithmParameterException e) {
     e.printStackTrace();
     Log.e(TAG, "InvalidAlgorithmParameterException " + e.getMessage());
     return null;
   } catch (NoSuchPaddingException e) {
     e.printStackTrace();
     Log.e(TAG, "NoSuchPaddingException " + e.getMessage());
     return null;
   } catch (IllegalBlockSizeException e) {
     e.printStackTrace();
     Log.e(TAG, "IllegalBlockSizeException " + e.getMessage());
     return null;
   } catch (BadPaddingException e) {
     e.printStackTrace();
     Log.e(TAG, "seafileEncrypt BadPaddingException " + e.getMessage());
     return null;
   }
 }
 public void deleteFSSFProfileDomainByUNameIdx(
     CFSecurityAuthorization Authorization,
     long argTenantId,
     long argFSSFProfileId,
     String argName) {
   final String S_ProcName = "deleteFSSFProfileDomainByUNameIdx";
   String rqst =
       CFFreeSwitchXMsgSchemaMessageFormatter.formatRqstXmlPreamble()
           + "\n"
           + "\t"
           + CFFreeSwitchXMsgFSSFProfileDomainMessageFormatter
               .formatFSSFProfileDomainRqstDeleteByUNameIdx(
                   "\n\t\t\t", argTenantId, argFSSFProfileId, argName)
           + "\n"
           + CFFreeSwitchXMsgSchemaMessageFormatter.formatRqstXmlPostamble();
   try {
     schema.getCFTipClientHandler().issueAppRequest(rqst);
   } catch (BadPaddingException e) {
     throw CFLib.getDefaultExceptionFactory()
         .newRuntimeException(
             getClass(), S_ProcName, "Caught BadPaddingException - " + e.getMessage(), e);
   } catch (IllegalBlockSizeException e) {
     throw CFLib.getDefaultExceptionFactory()
         .newRuntimeException(
             getClass(), S_ProcName, "Caught IllegalBlockSizeException - " + e.getMessage(), e);
   } catch (InvalidKeyException e) {
     throw CFLib.getDefaultExceptionFactory()
         .newRuntimeException(
             getClass(), S_ProcName, "Caught InvalidKeyException - " + e.getMessage(), e);
   } catch (NoSuchAlgorithmException e) {
     throw CFLib.getDefaultExceptionFactory()
         .newRuntimeException(
             getClass(), S_ProcName, "Caught NoSuchAlgorithmException - " + e.getMessage(), e);
   } catch (InvalidAlgorithmParameterException e) {
     throw CFLib.getDefaultExceptionFactory()
         .newRuntimeException(
             getClass(),
             S_ProcName,
             "Caught InvalidAlgorithmParameterException - " + e.getMessage(),
             e);
   } catch (NoSuchPaddingException e) {
     throw CFLib.getDefaultExceptionFactory()
         .newRuntimeException(
             getClass(), S_ProcName, "Caught NoSuchPaddingException - " + e.getMessage(), e);
   }
   ICFTipResponseHandler responseHandler = schema.getCFTipClientHandler().getResponseHandler();
   CFLibRuntimeException exceptionRaised = responseHandler.getExceptionRaised();
   if (exceptionRaised != null) {
     throw exceptionRaised;
   }
   boolean deleted = responseHandler.getDeleted();
   if (!deleted) {
     throw CFLib.getDefaultExceptionFactory()
         .newRuntimeException(
             getClass(), S_ProcName, "Server did not respond with a Deleted message");
   }
 }
示例#21
0
 static {
   try {
     cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
   } catch (NoSuchAlgorithmException e) {
     e.printStackTrace();
   } catch (NoSuchPaddingException e) {
     e.printStackTrace();
   }
 }
示例#22
0
 Cipher createCipher(String cipherName) throws PGPException {
   try {
     return helper.createCipher(cipherName);
   } catch (NoSuchAlgorithmException e) {
     throw new PGPException("cannot create cipher: " + e.getMessage(), e);
   } catch (NoSuchPaddingException e) {
     throw new PGPException("cannot create cipher: " + e.getMessage(), e);
   } catch (NoSuchProviderException e) {
     throw new PGPException("cannot create cipher: " + e.getMessage(), e);
   }
 }
示例#23
0
  public Crypt() {
    ivspec = new IvParameterSpec(Config.MCRYPT_IV.getBytes());
    keyspec = new SecretKeySpec(Config.MCRYPT_KEY.getBytes(), "AES");

    try {
      cipher = Cipher.getInstance("AES/CBC/NoPadding");
    } catch (NoSuchAlgorithmException e) {
      e.printStackTrace();
    } catch (NoSuchPaddingException e) {
      e.printStackTrace();
    }
  }
示例#24
0
  public static byte[] passwordEncrypt(char[] password, byte[] plaintext) {
    byte[] salt = new byte[8];
    Random random = new Random();
    random.nextBytes(salt);

    PBEKeySpec keySpec = new PBEKeySpec(password);
    SecretKeyFactory keyFactory = null;
    try {
      keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
    } catch (NoSuchAlgorithmException e) {
      e.printStackTrace();
    }
    SecretKey key = null;
    try {
      key = keyFactory.generateSecret(keySpec);
    } catch (InvalidKeySpecException e) {
      e.printStackTrace();
    }
    PBEParameterSpec paramSpec = new PBEParameterSpec(salt, ITERATIONS);
    Cipher cipher = null;
    try {
      cipher = Cipher.getInstance("PBEWithMD5AndDES");
    } catch (NoSuchAlgorithmException e) {
      e.printStackTrace();
    } catch (NoSuchPaddingException e) {
      e.printStackTrace();
    }
    try {
      cipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
    } catch (InvalidKeyException e) {
      e.printStackTrace();
    } catch (InvalidAlgorithmParameterException e) {
      e.printStackTrace();
    }

    byte[] ciphertext = new byte[0];
    try {
      ciphertext = cipher.doFinal(plaintext);
    } catch (IllegalBlockSizeException e) {
      e.printStackTrace();
    } catch (BadPaddingException e) {
      e.printStackTrace();
    }

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
      baos.write(salt);
      baos.write(ciphertext);
    } catch (IOException e) {
      e.printStackTrace();
    }
    return baos.toByteArray();
  }
示例#25
0
  public String[] encrypt(String value) throws Exception {
    try {
      KeyGenerator kgen = KeyGenerator.getInstance("AES");
      SecureRandom sran = new SecureRandom();

      kgen.init(128, sran);

      SecretKey skey = kgen.generateKey();
      byte[] raw = skey.getEncoded();

      String key = asHex(raw);

      Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");

      IvParameterSpec ivSpec = createCtrIvForAES(1, sran);

      cipher.init(Cipher.ENCRYPT_MODE, skey, ivSpec);

      byte[] encrypted = cipher.doFinal(padWithZeros(value.getBytes()));

      String vector = asHex(cipher.getIV());

      String encryptedValue = asHex(encrypted);

      return new String[] {encryptedValue, key, vector};

    } catch (NoSuchAlgorithmException e) {
      e.printStackTrace();

      throw new Exception("Encrypt error!", e);
    } catch (NoSuchPaddingException e) {
      e.printStackTrace();

      throw new Exception("Encrypt error!", e);
    } catch (InvalidKeyException e) {
      e.printStackTrace();

      throw new Exception("Encrypt error!", e);
    } catch (IllegalBlockSizeException e) {
      e.printStackTrace();

      throw new Exception("Encrypt error!", e);
    } catch (BadPaddingException e) {
      e.printStackTrace();

      throw new Exception("Encrypt error!", e);
    } catch (InvalidAlgorithmParameterException e) {
      e.printStackTrace();

      throw new Exception("Encrypt error!", e);
    }
  }
 public void deleteRealProject(
     CFSecurityAuthorization Authorization, CFInternetRealProjectBuff Buff) {
   final String S_ProcName = "deleteRealProject";
   String rqst =
       CFAccXMsgSchemaMessageFormatter.formatRqstXmlPreamble()
           + "\n"
           + "\t"
           + CFAccXMsgRealProjectMessageFormatter.formatRealProjectRqstDelete("\n\t\t\t", Buff)
           + "\n"
           + CFAccXMsgSchemaMessageFormatter.formatRqstXmlPostamble();
   try {
     schema.getCFTipClientHandler().issueAppRequest(rqst);
   } catch (BadPaddingException e) {
     throw CFLib.getDefaultExceptionFactory()
         .newRuntimeException(
             getClass(), S_ProcName, "Caught BadPaddingException - " + e.getMessage(), e);
   } catch (IllegalBlockSizeException e) {
     throw CFLib.getDefaultExceptionFactory()
         .newRuntimeException(
             getClass(), S_ProcName, "Caught IllegalBlockSizeException - " + e.getMessage(), e);
   } catch (InvalidKeyException e) {
     throw CFLib.getDefaultExceptionFactory()
         .newRuntimeException(
             getClass(), S_ProcName, "Caught InvalidKeyException - " + e.getMessage(), e);
   } catch (NoSuchAlgorithmException e) {
     throw CFLib.getDefaultExceptionFactory()
         .newRuntimeException(
             getClass(), S_ProcName, "Caught NoSuchAlgorithmException - " + e.getMessage(), e);
   } catch (InvalidAlgorithmParameterException e) {
     throw CFLib.getDefaultExceptionFactory()
         .newRuntimeException(
             getClass(),
             S_ProcName,
             "Caught InvalidAlgorithmParameterException - " + e.getMessage(),
             e);
   } catch (NoSuchPaddingException e) {
     throw CFLib.getDefaultExceptionFactory()
         .newRuntimeException(
             getClass(), S_ProcName, "Caught NoSuchPaddingException - " + e.getMessage(), e);
   }
   ICFTipResponseHandler responseHandler = schema.getCFTipClientHandler().getResponseHandler();
   CFLibRuntimeException exceptionRaised = responseHandler.getExceptionRaised();
   if (exceptionRaised != null) {
     throw exceptionRaised;
   }
   boolean deleted = responseHandler.getDeleted();
   if (!deleted) {
     throw CFLib.getDefaultExceptionFactory()
         .newRuntimeException(
             getClass(), S_ProcName, "Server did not respond with a Deleted message");
   }
 }
示例#27
0
  /**
   * Endorse the proof
   *
   * @param aStampContext current context
   * @param aProof proof
   * @return endorsed proof
   * @throws NoSuchAlgorithmException
   * @throws BadPaddingException
   * @throws IllegalBlockSizeException
   * @throws NoSuchPaddingException
   * @throws InvalidKeyException
   */
  private static byte[] endorseP(WitnessContext aStampContext, byte aProof[]) {

    // Sign on the proof first
    byte[] sig = {};
    try {
      sig = CryptoUtil.signDSA(aStampContext.getPriDSASelf(), aProof);
    } catch (InvalidKeyException e) {
      e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
      e.printStackTrace();
    } catch (SignatureException e) {
      e.printStackTrace();
    }

    // Include own ID in EP
    byte[] wID = aStampContext.getPubDSASelf().getEncoded();

    ArrayList<byte[]> array = new ArrayList<byte[]>();
    array.add(wID);
    array.add(aProof);
    array.add(sig);

    byte[] epContent = MessageUtil.compileMessages(array);

    // Encrypt epContent with an AES key first
    SecretKey aesKey = null;
    byte[] epEncrypted = {};
    byte[] keyEncrypted = {};
    try {
      aesKey = CryptoUtil.generateAESKey(128);
      epEncrypted = CryptoUtil.encryptAES(aesKey, epContent);
      keyEncrypted = CryptoUtil.encryptRSA(aStampContext.getPubRSACA(), aesKey.getEncoded());
    } catch (NoSuchAlgorithmException e1) {
      e1.printStackTrace();
    } catch (InvalidKeyException e) {
      e.printStackTrace();
    } catch (NoSuchPaddingException e) {
      e.printStackTrace();
    } catch (IllegalBlockSizeException e) {
      e.printStackTrace();
    } catch (BadPaddingException e) {
      e.printStackTrace();
    }

    ArrayList<byte[]> arrayOut = new ArrayList<byte[]>();
    arrayOut.add(epEncrypted);
    arrayOut.add(keyEncrypted);

    return MessageUtil.compileMessages(arrayOut);
  }
示例#28
0
  public static byte[] passwordDecrypt(char[] password, byte[] ciphertext) {
    byte[] salt = new byte[8];
    ByteArrayInputStream bais = new ByteArrayInputStream(ciphertext);
    bais.read(salt, 0, 8);

    byte[] remainingCiphertext = new byte[ciphertext.length - 8];
    bais.read(remainingCiphertext, 0, ciphertext.length - 8);

    PBEKeySpec keySpec = new PBEKeySpec(password);
    SecretKeyFactory keyFactory = null;
    try {
      keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
    } catch (NoSuchAlgorithmException e) {
      e.printStackTrace();
    }
    SecretKey secretKey = null;
    try {
      secretKey = keyFactory.generateSecret(keySpec);
    } catch (InvalidKeySpecException e) {
      e.printStackTrace();
    }

    PBEParameterSpec paramSpec = new PBEParameterSpec(salt, ITERATIONS);

    Cipher cipher = null;
    try {
      cipher = Cipher.getInstance("PBEWithMD5AndDES");
    } catch (NoSuchAlgorithmException e) {
      e.printStackTrace();
    } catch (NoSuchPaddingException e) {
      e.printStackTrace();
    }
    try {
      cipher.init(Cipher.DECRYPT_MODE, secretKey, paramSpec);
    } catch (InvalidKeyException e) {
      e.printStackTrace();
    } catch (InvalidAlgorithmParameterException e) {
      e.printStackTrace();
    }

    try {
      return cipher.doFinal(remainingCiphertext);
    } catch (IllegalBlockSizeException e) {
      e.printStackTrace();
    } catch (BadPaddingException e) {
      e.printStackTrace();
    }
    return null;
  }
 public static String decrypt(String property) {
   SecretKeyFactory keyFactory = null;
   String retValue = null;
   try {
     keyFactory = SecretKeyFactory.getInstance(encrypteAlgorithm);
   } catch (NoSuchAlgorithmException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
   }
   SecretKey key = null;
   try {
     key = keyFactory.generateSecret(new PBEKeySpec(PASSWORD));
   } catch (InvalidKeySpecException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
   }
   Cipher pbeCipher = null;
   try {
     pbeCipher = Cipher.getInstance(encrypteAlgorithm);
   } catch (NoSuchAlgorithmException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
   } catch (NoSuchPaddingException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
   }
   try {
     pbeCipher.init(Cipher.DECRYPT_MODE, key, new PBEParameterSpec(SALT, 20));
   } catch (InvalidKeyException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
   } catch (InvalidAlgorithmParameterException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
   }
   try {
     retValue = new String(pbeCipher.doFinal(base64Decode(property)));
   } catch (IllegalBlockSizeException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
   } catch (BadPaddingException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
   } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
   }
   return retValue;
 }
示例#30
0
 public static void main(String[] args) {
   byte[] password = "******".getBytes();
   try {
     InputStream fin = new FileInputStream("testEn.jar");
     OutputStream fos = new FileOutputStream("testDe4java.jar");
     SecretKeySpec key = new SecretKeySpec(password, "AES");
     Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
     byte[] ivByte = new byte[16];
     new Random().nextBytes(ivByte);
     System.out.printf("ivbytes:%x %x %x\n", ivByte[0], ivByte[1], ivByte[15]);
     IvParameterSpec iv = new IvParameterSpec(ivByte);
     cipher.init(2, key, iv);
     int totalLen = fin.available();
     byte[] byteContent = new byte[totalLen];
     int len = 0;
     len = fin.read(byteContent);
     if (len != totalLen) System.out.println("read is not equal");
     byte[] result = cipher.doFinal(byteContent);
     // 前四个字节是文件长度
     int fileLen =
         ((result[16] & 0xff) << 24)
             + ((result[17] & 0xff) << 16)
             + ((result[18] & 0xff) << 8)
             + (result[19] & 0xff);
     if ((fileLen + 20) > len) fos.write(result, 20, len - 20);
     else fos.write(result, 20, fileLen);
     fin.close();
     fos.close();
   } catch (NoSuchAlgorithmException e) {
     e.printStackTrace();
   } catch (NoSuchPaddingException e) {
     e.printStackTrace();
   } catch (InvalidKeyException e) {
     e.printStackTrace();
   } catch (IllegalBlockSizeException e) {
     e.printStackTrace();
   } catch (BadPaddingException e) {
     e.printStackTrace();
   } catch (FileNotFoundException e1) {
     // TODO Auto-generated catch block
     e1.printStackTrace();
   } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
   } catch (InvalidAlgorithmParameterException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
   }
 }