Package ants.p2p.query.security

Source Code of ants.p2p.query.security.PublicHeader

//******************************************************************
//******************************************************************
//**********          ANts Peer To Peer Sources        *************
//
// ANts P2P realizes a third generation P2P net. It protects your
// privacy while you are connected and makes you not trackable, hiding
// your identity (ip) and crypting everything you are sending/receiving
// from others.

// Copyright (C) 2004  Roberto Rossi

// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

package ants.p2p.query.security;

import java.io.*;
import java.math.*;
import java.security.*;
import ants.p2p.utils.*;
import javax.crypto.*;
import javax.crypto.spec.*;

import ants.p2p.utils.encoding.Base16;
import org.bouncycastle.crypto.params.RSAKeyParameters;
import org.bouncycastle.crypto.engines.RSAEngine;
import org.bouncycastle.crypto.AsymmetricBlockCipher;
import org.bouncycastle.crypto.encodings.PKCS1Encoding;

public class PublicHeader implements Serializable{
  BigInteger exp;
  BigInteger mod;
  String sessionKey;
  String encryptedSessionKey;

  public PublicHeader(BigInteger exp, BigInteger mod){
    this.exp = exp;
    this.mod = mod;
  }

  protected PublicHeader(){}

  public void encryptSessionKey() throws Exception{
    this.encryptedSessionKey = this.generate();
  }

  public void invalidateSessionKey(){
    this.sessionKey = null;
  }

  public BigInteger getExp() {
    return this.exp;
  }

  public BigInteger getMod() {
    return this.mod;
  }

  private String generate() throws Exception{
    RSAKeyParameters pubParameters = new RSAKeyParameters(false, mod, exp);
    AsymmetricBlockCipher eng = new PKCS1Encoding(new RSAEngine());
    eng.init(true, pubParameters);
    byte[] data = Base16.fromHexString(this.sessionKey);
    data = eng.processBlock(data, 0, data.length);
    return Base16.toHexString(data);
  }

  public void generateSessionKey() throws Exception{
    byte[] sessionBytes = new byte[SimmetricProvider.cipherKeySize];
    SecureRandom sr = new SecureRandom();
    sr.nextBytes(sessionBytes);
    for(int x = 0; x < sessionBytes.length; x++){
      if(sessionBytes[x] < 0)
        sessionBytes[x] = (byte)Math.abs(sessionBytes[x]);
    }
    this.sessionKey = Base16.toHexString(sessionBytes);
    this.encryptSessionKey();
  }

  public String getBase16SessionKey() {
    return this.sessionKey;
  }

  public String getEncryptedSessionKey() {
    return this.encryptedSessionKey;
  }

  public Cipher getEncCipher() throws Exception{
    SecretKeySpec skeySpec = new SecretKeySpec(Base16.fromHexString(sessionKey),
                                               SimmetricProvider.cipher);
    Cipher cipher = Cipher.getInstance(SimmetricProvider.cipher);
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
    return cipher;
  }
}
TOP

Related Classes of ants.p2p.query.security.PublicHeader

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.