示例#1
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);
  }
示例#2
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;
 }
示例#3
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;
 }
示例#4
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();
    }
  }
示例#5
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;
  }
示例#6
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;
  }
示例#7
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;
  }
示例#8
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;
   }
 }
示例#9
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;
  }
示例#10
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;
 }
示例#11
0
  private static String encrypt(String baseKey, byte[] src)
      throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException,
          InvalidAlgorithmParameterException {

    byte[] key = SHA256(baseKey);

    if (secureKey == null) {
      secureKey = new SecretKeySpec(key, "AES");
    }

    if (encryptor == null) {
      encryptor = Cipher.getInstance("AES/CBC/PKCS5Padding");
      encryptor.init(Cipher.ENCRYPT_MODE, secureKey, new IvParameterSpec(IV.getBytes()));
    }

    byte[] encrypted = null;

    try {
      encrypted = encryptor.doFinal(src);
    } catch (IllegalBlockSizeException e) {
      e.printStackTrace();
    } catch (BadPaddingException e) {
      e.printStackTrace();
    }
    return Base64.encodeToString(encrypted, Base64.NO_WRAP);
  }
示例#12
0
  /**
   * @param src
   * @return source data
   * @throws NoSuchPaddingException
   * @throws NoSuchAlgorithmException
   * @throws InvalidAlgorithmParameterException
   * @throws InvalidKeyException
   */
  public byte[] decrypt(String baseKey, String src)
      throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException,
          InvalidAlgorithmParameterException {

    byte[] key = SHA256(baseKey);

    if (secureKey == null) {
      secureKey = new SecretKeySpec(key, "AES");
    }

    if (decryptor == null) {
      decryptor = Cipher.getInstance("AES/CBC/PKCS5Padding");
      decryptor.init(Cipher.DECRYPT_MODE, secureKey, new IvParameterSpec(IV.getBytes()));
    }

    byte[] dec = Base64.decode(src, 0);
    byte[] ret = null;

    try {
      ret = decryptor.doFinal(dec);
    } catch (IllegalBlockSizeException e) {
      e.printStackTrace();
    } catch (BadPaddingException e) {
      e.printStackTrace();
    }

    return ret;
  }
  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();
    }
  }
示例#14
0
 private byte[] encrypt(byte[] content, byte[] keyBytes) {
   byte[] encryptedText = null;
   if (!isInited) {
     init();
   }
   /**
    * 类 SecretKeySpec 可以使用此类来根据一个字节数组构造一个 SecretKey, 而无须通过一个(基于 provider 的)SecretKeyFactory。
    * 此类仅对能表示为一个字节数组并且没有任何与之相关联的钥参数的原始密钥有用 构造方法根据给定的字节数组构造一个密钥。 此构造方法不检查给定的字节数组是否指定了一个算法的密钥。
    */
   Key key = new SecretKeySpec(keyBytes, "AES");
   try {
     // 用密钥初始化此 cipher。
     cipher.init(Cipher.ENCRYPT_MODE, key);
   } catch (InvalidKeyException e) {
     e.printStackTrace();
   }
   try {
     // 按单部分操作加密或解密数据,或者结束一个多部分操作。(不知道神马意思)
     encryptedText = cipher.doFinal(content);
   } catch (IllegalBlockSizeException e) {
     e.printStackTrace();
   } catch (BadPaddingException e) {
     e.printStackTrace();
   }
   return encryptedText;
 }
示例#15
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;
 }
示例#16
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;
  }
示例#17
0
文件: DESPlus.java 项目: hebut/jxkh
 /**
  * 加密字节数组
  *
  * @param arrB 需加密的字节数组
  * @return 加密后的字节数组
  * @throws Exception
  */
 public byte[] encrypt(byte[] arrB) {
   try {
     return encryptCipher.doFinal(arrB);
   } catch (IllegalBlockSizeException e) {
     e.printStackTrace();
   } catch (BadPaddingException e) {
     e.printStackTrace();
   }
   return arrB;
 }
示例#18
0
 private static byte[] func_75885_a(int p_75885_0_, Key p_75885_1_, byte p_75885_2_[]) {
   try {
     return func_75886_a(p_75885_0_, p_75885_1_.getAlgorithm(), p_75885_1_).doFinal(p_75885_2_);
   } catch (IllegalBlockSizeException illegalblocksizeexception) {
     illegalblocksizeexception.printStackTrace();
   } catch (BadPaddingException badpaddingexception) {
     badpaddingexception.printStackTrace();
   }
   System.err.println("Cipher data failed!");
   return null;
 }
示例#19
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();
  }
示例#20
0
  private static byte[] func_75885_a(int par0, Key par1Key, byte[] par2ArrayOfByte) {
    try {
      return func_75886_a(par0, par1Key.getAlgorithm(), par1Key).doFinal(par2ArrayOfByte);
    } catch (IllegalBlockSizeException var4) {
      var4.printStackTrace();
    } catch (BadPaddingException var5) {
      var5.printStackTrace();
    }

    System.err.println("Cipher data failed!");
    return null;
  }
示例#21
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);
    }
  }
示例#22
0
 /**
  * 对 datasource 数组进行解密.
  *
  * @param datasource 要解密的数据
  * @return 返回加密后的 byte[]
  */
 public byte[] createDecryptor(byte[] datasource) {
   try {
     cipher.init(Cipher.DECRYPT_MODE, deskey);
     decryptorData = cipher.doFinal(datasource);
   } catch (java.security.InvalidKeyException ex) {
     ex.printStackTrace();
   } catch (javax.crypto.BadPaddingException ex) {
     ex.printStackTrace();
   } catch (javax.crypto.IllegalBlockSizeException ex) {
     ex.printStackTrace();
   }
   return decryptorData;
 }
示例#23
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);
  }
示例#24
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;
  }
示例#25
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();
   }
 }
 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;
 }
示例#27
0
 /**
  * 对 String 进行加密
  *
  * @param str 要加密的数据
  * @return 返回加密后的 byte 数组
  * @throws java.io.UnsupportedEncodingException
  */
 public byte[] createEncryptor(String str)
     throws UnsupportedEncodingException, InvalidAlgorithmParameterException {
   try {
     // c.init(Cipher.ENCRYPT_MODE, deskey, iv);
     return c.doFinal(str.getBytes());
     // } catch (java.security.InvalidKeyException ex) {
     //	ex.printStackTrace();
   } catch (javax.crypto.BadPaddingException ex) {
     ex.printStackTrace();
     // ex//c.
     // c.
   } catch (javax.crypto.IllegalBlockSizeException ex) {
     ex.printStackTrace();
   }
   return null;
 }
 public static byte[] encryptData(Key key, byte[] toEncrypt) {
   try {
     Cipher cipher = Cipher.getInstance(key.getAlgorithm());
     cipher.init(Cipher.ENCRYPT_MODE, key);
     return cipher.doFinal(toEncrypt);
   } catch (NoSuchAlgorithmException e) {
     e.printStackTrace();
   } catch (NoSuchPaddingException e) {
     e.printStackTrace();
   } catch (InvalidKeyException e) {
     e.printStackTrace();
   } catch (BadPaddingException e) {
     e.printStackTrace();
   } catch (IllegalBlockSizeException e) {
     e.printStackTrace();
   }
   return new byte[0];
 }
示例#29
0
 /**
  * 解密
  *
  * @param content 待解密内容
  * @param deckey 解密密钥
  * @return
  */
 public static byte[] decrypt(byte[] content, byte[] deckey) {
   try {
     SecretKeySpec key = new SecretKeySpec(deckey, "AES");
     Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding"); // 创建密码器
     cipher.init(Cipher.DECRYPT_MODE, key); // 初始化
     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;
 }
示例#30
0
  public static byte[] decrypt(byte[] content, String pwd) {
    KeyGenerator kgen = null;
    try {
      Log.i("shentanli", "in the decrypt");
      kgen = KeyGenerator.getInstance("AES");
      SecureRandom sr = SecureRandom.getInstance("SHA1PRNG", "Crypto");

      sr.setSeed(pwd.getBytes());
      kgen.init(128, sr);
      SecretKey sk = kgen.generateKey();
      byte[] ef = sk.getEncoded();
      SecretKeySpec key = new SecretKeySpec(ef, "AES");
      Cipher c = null;
      c = Cipher.getInstance("AES/ECB/NoPadding");

      c.init(Cipher.DECRYPT_MODE, key);
      //     byte[] tmp = Base64.decode(content.getBytes("UTF-8"), Base64.DEFAULT);
      //   byte[] tmp = parseHexStr2Byte(content);
      byte[] result = c.doFinal(content);
      Log.i("shentanli", "now to return");
      //  Log.i("shentanli---", Base64.encodeToString(result, Base64.DEFAULT));
      //   return Base64.encodeToString(result, Base64.DEFAULT);
      Log.i("shentanli", new String(result));
      //  return (new String(result));
      return result;
    } catch (NoSuchAlgorithmException e) {
      e.printStackTrace();
    } catch (NoSuchPaddingException e) {
      e.printStackTrace();
    } catch (BadPaddingException e) {
      e.printStackTrace();
    } catch (IllegalBlockSizeException e) {
      e.printStackTrace();
    } catch (InvalidKeyException e) {
      e.printStackTrace();
    } catch (NoSuchProviderException e) {
      e.printStackTrace();
    }

    return content;
  }