Пример #1
0
  public void testModule() throws Exception {
    Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); // Add

    // Create the secret/symmetric key
    KeyGenerator kgen = KeyGenerator.getInstance("Blowfish");
    SecretKey skey = kgen.generateKey();
    byte[] raw = skey.getEncoded();
    SecretKeySpec skeySpec = new SecretKeySpec(raw, "Blowfish");

    // Create the cipher for encrypting
    Cipher cipher = Cipher.getInstance("Blowfish");
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec);

    // Encrypt the data
    byte[] encrypted = cipher.doFinal(plain);

    // Save the encrypted data
    FileOutputStream fos = new FileOutputStream(datafile);
    fos.write(encrypted);
    fos.close();

    // Save the cipher settings
    byte[] encodedKeySpec = skeySpec.getEncoded();
    FileOutputStream eksos = new FileOutputStream(keyfile);
    eksos.write(encodedKeySpec);
    eksos.close();

    // Read the encrypted data
    FileInputStream fis = new FileInputStream(datafile);
    byte[] temp = new byte[8192];
    int bytesRead = fis.read(temp);
    byte[] data = new byte[bytesRead];
    System.arraycopy(temp, 0, data, 0, bytesRead);

    // Read the cipher settings
    FileInputStream eksis = new FileInputStream(keyfile);
    bytesRead = eksis.read(temp);
    encodedKeySpec = new byte[bytesRead];
    System.arraycopy(temp, 0, encodedKeySpec, 0, bytesRead);

    // Recreate the secret/symmetric key
    skeySpec = new SecretKeySpec(encodedKeySpec, "Blowfish");

    // Create the cipher for encrypting
    cipher = Cipher.getInstance("Blowfish");
    cipher.init(Cipher.DECRYPT_MODE, skeySpec);

    // Decrypt the data
    byte[] decrypted = cipher.doFinal(data);

    assertTrue(Arrays.equals(decrypted, plain));
  }
Пример #2
0
 /**
  * 解密
  *
  * @param content 待解密内容
  * @param password 解密密钥
  * @return
  */
 public static byte[] decrypt(byte[] data, byte[] key) {
   CheckUtils.notEmpty(data, "data");
   CheckUtils.notEmpty(key, "key");
   if (key.length != 16) {
     throw new RuntimeException("Invalid AES key length (must be 16 bytes)");
   }
   try {
     SecretKeySpec secretKey = new SecretKeySpec(key, "AES");
     byte[] enCodeFormat = secretKey.getEncoded();
     SecretKeySpec seckey = new SecretKeySpec(enCodeFormat, "AES");
     Cipher cipher = Cipher.getInstance(ConfigureEncryptAndDecrypt.AES_ALGORITHM); // 创建密码器
     cipher.init(Cipher.DECRYPT_MODE, seckey); // 初始化
     byte[] result = cipher.doFinal(data);
     return result; // 加密
   } catch (Exception e) {
     throw new RuntimeException("decrypt fail!", e);
   }
 }
  // create jason file and put some keys into it..
  private static void createTokenFileJson() throws IOException {
    Map<String, String> map = new HashMap<String, String>();

    try {
      KeyGenerator kg = KeyGenerator.getInstance("HmacSHA1");
      for (int i = 0; i < NUM_OF_KEYS; i++) {
        SecretKeySpec key = (SecretKeySpec) kg.generateKey();
        byte[] enc_key = key.getEncoded();
        map.put("alias" + i, new String(Base64.encodeBase64(enc_key)));
      }
    } catch (NoSuchAlgorithmException e) {
      throw new IOException(e);
    }

    try {
      File p = new File(tokenFileName.getParent().toString());
      p.mkdirs();
      // convert to JSON and save to the file
      mapper.writeValue(new File(tokenFileName.toString()), map);

    } catch (Exception e) {
      System.out.println("failed with :" + e.getLocalizedMessage());
    }
  }