Code example for SecretKeySpec

Methods: getEncoded

0
  } 
		 
  private SecretKeySpec deriveMacSecret(SecretKeySpec key) {
    try { 
      MessageDigest md = MessageDigest.getInstance("SHA-1");
      byte[] secret    = md.digest(key.getEncoded());
		 
      return new SecretKeySpec(secret, "HmacSHA1");
    } catch (NoSuchAlgorithmException e) {
      throw new IllegalArgumentException("SHA-1 Not Supported!",e);
    } 
  } 
	 
  private byte[] buildMessageFromCiphertext(byte[] cipherText) {
    Message message = new Message(localRecord.getCurrentKeyPair().getId(),
				  remoteRecord.getCurrentRemoteKey().getId(),
				  localRecord.getNextKeyPair().getPublicKey(),
				  sessionRecord.getCounter(),
				  cipherText, sessionRecord.getSessionVersion(), Message.SUPPORTED_VERSION);
		 
    return message.serialize();
  } 
		 
 	 
  private byte[] getPlaintext(byte[] cipherText, SecretKeySpec key, int counter) throws IllegalBlockSizeException, BadPaddingException {
    Cipher cipher = getCipher(Cipher.DECRYPT_MODE, key, counter);
    return cipher.doFinal(cipherText);
  } 
	 
  private byte[] getCiphertext(byte[] message, SecretKeySpec key) throws IllegalBlockSizeException, BadPaddingException {
    Cipher cipher = getCipher(Cipher.ENCRYPT_MODE, key, sessionRecord.getCounter());
    return cipher.doFinal(message);
  } 
	 
  private Cipher getCipher(int mode, SecretKeySpec key, int counter)  {
    try { 
      Cipher cipher = Cipher.getInstance("AES/CTR/NoPadding");
 
      byte[] ivBytes = new byte[16];
      Conversions.mediumToByteArray(ivBytes, 0, counter);
		 
      IvParameterSpec iv = new IvParameterSpec(ivBytes);
      cipher.init(mode, key, iv);
		 
      return cipher;
    } catch (NoSuchAlgorithmException e) {
      throw new IllegalArgumentException("AES Not Supported!");
    } catch (NoSuchPaddingException e) {
      throw new IllegalArgumentException("NoPadding Not Supported!");
    } catch (InvalidKeyException e) {
      Log.w("SessionCipher", e);
      throw new IllegalArgumentException("Invaid Key?");
    } catch (InvalidAlgorithmParameterException e) {
      Log.w("SessionCipher", e);
      throw new IllegalArgumentException("Bad IV?");
    } 
  } 
	 
  private SecretKeySpec deriveCipherSecret(int mode, BigInteger sharedSecret, int localKeyId, int remoteKeyId) throws InvalidKeyIdException {
    byte[] sharedSecretBytes = sharedSecret.toByteArray();
    byte[] derivedBytes      = deriveBytes(sharedSecretBytes, 16 * 2);
    byte[] cipherSecret      = new byte[16];
		 
    boolean isLowEnd         = isLowEnd(localKeyId, remoteKeyId);
    isLowEnd                 = (mode == Cipher.ENCRYPT_MODE ? isLowEnd : !isLowEnd);
		 
    if (isLowEnd)  {
      System.arraycopy(derivedBytes, 16, cipherSecret, 0, 16);
    } else { 
      System.arraycopy(derivedBytes, 0, cipherSecret, 0, 16);
    } 
		 
    return new SecretKeySpec(cipherSecret, "AES");
  } 
	 
  private boolean isLowEnd(int localKeyId, int remoteKeyId) throws InvalidKeyIdException {
    ECPublicKeyParameters localPublic  = (ECPublicKeyParameters)localRecord.getKeyPairForId(localKeyId).getPublicKey().getKey();
    ECPublicKeyParameters remotePublic = (ECPublicKeyParameters)remoteRecord.getKeyForId(remoteKeyId).getKey();
    BigInteger local                   = localPublic.getQ().getX().toBigInteger();
    BigInteger remote                  = remotePublic.getQ().getX().toBigInteger();
		 
    return local.compareTo(remote) < 0;
  } 
	 
  private byte[] deriveBytes(byte[] seed, int bytesNeeded) {
    MessageDigest md;
 
    try { 
      md = MessageDigest.getInstance("SHA-256");
    } catch (NoSuchAlgorithmException e) {
      Log.w("SessionCipher",e);
      throw new IllegalArgumentException("SHA-256 Not Supported!");
    } 
		 
    int rounds = bytesNeeded / md.getDigestLength();
		 
    for (int i=1;i<=rounds;i++) {
      byte[] roundBytes = Conversions.intToByteArray(i);
      md.update(roundBytes);
      md.update(seed);
    } 
		 
    return md.digest();
  } 
	 
  private SessionKey getSessionKey(int mode, int localKeyId, int remoteKeyId) throws InvalidKeyIdException {
    Log.w("SessionCipher", "Getting session key for local: " + localKeyId + " remote: " + remoteKeyId);
    SessionKey sessionKey = sessionRecord.getSessionKey(localKeyId, remoteKeyId);
    if (sessionKey != null) return sessionKey;
		 
    BigInteger sharedSecret = calculateSharedSecret(localKeyId, remoteKeyId);
    SecretKeySpec cipherKey = deriveCipherSecret(mode, sharedSecret, localKeyId, remoteKeyId);
    SecretKeySpec macKey    = deriveMacSecret(cipherKey);
 
    return new SessionKey(localKeyId, remoteKeyId, cipherKey, macKey, masterSecret);
  } 
	 
  private BigInteger calculateSharedSecret(int localKeyId, int remoteKeyId) throws InvalidKeyIdException {
    ECDHBasicAgreement agreement         = new ECDHBasicAgreement();
    AsymmetricCipherKeyPair localKeyPair = localRecord.getKeyPairForId(localKeyId).getKeyPair();
Contextual code suggestions in your IDE  Get Codota for Java