コード例 #1
0
ファイル: hmac.java プロジェクト: xingkongtianyu/Encryption
  /**
   * HMAC加密
   *
   * @param data
   * @param key
   * @return
   * @throws Exception
   */
  public static byte[] encryptHMAC(byte[] data, String key, String method) throws Exception {

    // SecretKey secretKey = new SecretKeySpec(decryptBASE64(key), "HmacMD5");
    SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), method); // 直接用给定的字符串进行
    Mac mac = Mac.getInstance(secretKey.getAlgorithm());
    mac.init(secretKey);

    return mac.doFinal(data);
  }
コード例 #2
0
ファイル: TestSAT.java プロジェクト: jsigle/elexis-base
  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));
  }
コード例 #3
0
  /**
   * @param username Sauce Username
   * @param accessKey Sauce Access key
   * @param jobId job id
   * @return HMAC token
   * @throws NoSuchAlgorithmException thrown if an error occurs generating the key
   * @throws InvalidKeyException thrown if an error occurs generating the key
   * @throws UnsupportedEncodingException thrown if an error occurs generating the key
   */
  public String calcHMAC(String username, String accessKey, String jobId)
      throws NoSuchAlgorithmException, InvalidKeyException, UnsupportedEncodingException {
    Calendar calendar = Calendar.getInstance();

    SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT);
    format.setTimeZone(TimeZone.getTimeZone("UTC"));
    String key = username + ":" + accessKey + ":" + format.format(calendar.getTime());
    byte[] keyBytes = key.getBytes();
    SecretKeySpec sks = new SecretKeySpec(keyBytes, HMAC_KEY);
    Mac mac = Mac.getInstance(sks.getAlgorithm());
    mac.init(sks);
    byte[] hmacBytes = mac.doFinal(jobId.getBytes());
    byte[] hexBytes = new Hex().encode(hmacBytes);
    return new String(hexBytes, "ISO-8859-1");
  }
コード例 #4
0
ファイル: OAuthToken.java プロジェクト: gavin01du/fantalk
 @Override
 public int hashCode() {
   int result = token.hashCode();
   result = 31 * result + tokenSecret.hashCode();
   result = 31 * result + (secretKeySpec != null ? secretKeySpec.hashCode() : 0);
   return result;
 }
コード例 #5
0
ファイル: AES.java プロジェクト: mlq6789/myproject
 /**
  * 解密
  *
  * @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);
   }
 }
コード例 #6
0
  // 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());
    }
  }
コード例 #7
0
ファイル: OAuthToken.java プロジェクト: gavin01du/fantalk
  @Override
  public boolean equals(Object o) {
    if (this == o) return true;
    if (!(o instanceof OAuthToken)) return false;

    OAuthToken that = (OAuthToken) o;

    if (secretKeySpec != null
        ? !secretKeySpec.equals(that.secretKeySpec)
        : that.secretKeySpec != null) return false;
    if (!token.equals(that.token)) return false;
    if (!tokenSecret.equals(that.tokenSecret)) return false;

    return true;
  }
コード例 #8
0
ファイル: JCEHandlerTest.java プロジェクト: evilgod528/jPOS
 @Test
 public void testFormDESKey1() throws Throwable {
   byte[] clearKeyBytes = new byte[1];
   SecretKeySpec result = (SecretKeySpec) jCEHandler.formDESKey((short) 192, clearKeyBytes);
   assertEquals("result.getAlgorithm()", "DESede", result.getAlgorithm());
 }