Example usage for org.bouncycastle.crypto.engines RSAEngine processBlock

List of usage examples for org.bouncycastle.crypto.engines RSAEngine processBlock

Introduction

In this page you can find the example usage for org.bouncycastle.crypto.engines RSAEngine processBlock.

Prototype

public byte[] processBlock(byte[] in, int inOff, int inLen) 

Source Link

Document

Process a single block using the basic RSA algorithm.

Usage

From source file:org.fnppl.opensdx.security.PrivateKey.java

License:Open Source License

public byte[] sign(byte[] data) throws Exception {
    RSAEngine rsae = new RSAEngine();
    rsae.init(false, priv);//from  w  ww  .j  a  va  2  s.  c  o  m
    byte[] filleddata = new byte[rsae.getInputBlockSize() - 1];
    for (int i = 0; i < filleddata.length; i++) {
        filleddata[i] = data[i % data.length];//HT 2011-03-03 better some initvectorpadddup!!!
    }

    //System.out.println("PrivKey_SIGN_PLAINBLOATED:\t"+SecurityHelper.HexDecoder.encode(filleddata, ':', 80));

    return rsae.processBlock(filleddata, 0, filleddata.length);
}

From source file:org.fnppl.opensdx.security.PublicKey.java

License:Open Source License

public byte[] encryptPKCSed7(byte[] data) throws Exception {
    //input here is a full rsa-block...
    RSAEngine rsae = new RSAEngine();
    //      org.bouncycastle.crypto.encodings.PKCS1Encoding enc = new PKCS1Encoding(rsae);
    //      enc.init(true, pub);
    //       return enc.processBlock(data, 0, data.length);

    rsae.init(true, pub);/* w ww  .ja v a 2s  . c  o m*/
    //      System.out.println("pub.encryptpkcs7.outputLength: "+rsae.getOutputBlockSize());
    //      System.out.println("pub.encryptpkcs7.inputBlockSize: "+rsae.getInputBlockSize());

    //      PKCS7Padding pad = new PKCS7Padding();
    //      byte[] kk = new byte[rsae.getInputBlockSize()];
    //      System.arraycopy(data, 0, kk, 0, data.length);
    //      pad.addPadding(kk, data.length);

    //      return rsae.processBlock(kk, 0, kk.length);

    return rsae.processBlock(data, 0, data.length);

    //      OAEPEncoding oaep = new OAEPEncoding(rsae);
    //      oaep.init(
    //            false, //fr encrypt: true
    ////            bp
    //            priv
    //         );
    //      if(data.length > rsae.getInputBlockSize()) {
    //         throw new RuntimeException("PrivateKey.encrypt::data.length("+data.length+") too long - max is: "+rsae.getInputBlockSize());
    //      }
    //      
    //      return oaep.processBlock(data, 0, data.length);
}

From source file:org.silvertunnel.netlib.layer.tor.util.Encryption.java

License:Open Source License

/**
 * checks row signature/*from w w  w . ja  v a 2 s . c  om*/
 * 
 * @param signature
 *            signature to check
 * @param signingKey
 *            public key from signing
 * @param input
 *            byte array, signature is made over
 * 
 * @return true, if the signature is correct
 * 
 */
public static boolean verifySignatureXXXX(byte[] signature, RSAPublicKeyStructure signingKey, byte[] input) {

    byte[] hash = getDigest(input);
    try {
        Signature sig = Signature.getInstance("SHA1withRSA");
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        RSAPublicKeySpec keySpec = new RSAPublicKeySpec(signingKey.getModulus(),
                signingKey.getPublicExponent());
        PublicKey pubKey = keyFactory.generatePublic(keySpec);
        sig.initVerify(pubKey);
        sig.update(input);
        log.info("");
        log.info(" HERE -> " + sig.verify(signature));

        RSAKeyParameters myRSAKeyParameters = new RSAKeyParameters(false, signingKey.getModulus(),
                signingKey.getPublicExponent());
        RSAEngine rsaAlg = new RSAEngine();
        rsaAlg.init(false, myRSAKeyParameters);
        byte[] decryptedSignature = rsaAlg.processBlock(signature, 0, signature.length);
        log.info(" inpu = " + Encoding.toHexString(input));
        log.info(" hash = " + Encoding.toHexString(hash));
        log.info("");
        log.info(" sign = " + Encoding.toHexString(signature));
        log.info(" decr = " + Encoding.toHexString(decryptedSignature));

        return Encoding.arraysEqual(hash, decryptedSignature);

    } catch (Exception e) {
        log.log(Level.WARNING, "unexpected", e);
        return false;
    }
}

From source file:TorJava.Common.Encryption.java

License:Open Source License

/**
 * checks row signature/*from www  .j  a v  a  2 s .c om*/
 * 
 * @param signature
 *            signature to check
 * @param signingKey
 *            public key from signing
 * @param input
 *            byte array, signature is made over
 * 
 * @return true, if the signature is correct
 * 
 */
public static boolean verifySignatureXXXX(byte[] signature, RSAPublicKeyStructure signingKey, byte[] input) {

    byte[] hash = getHash(input);
    try {
        Signature sig = Signature.getInstance("SHA1withRSA");
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        RSAPublicKeySpec keySpec = new RSAPublicKeySpec(signingKey.getModulus(),
                signingKey.getPublicExponent());
        PublicKey pubKey = keyFactory.generatePublic(keySpec);
        sig.initVerify(pubKey);
        sig.update(input);
        System.out.println("");
        System.out.println(" HERE -> " + sig.verify(signature));

        RSAKeyParameters myRSAKeyParameters = new RSAKeyParameters(false, signingKey.getModulus(),
                signingKey.getPublicExponent());
        RSAEngine rsa_alg = new RSAEngine();
        rsa_alg.init(false, myRSAKeyParameters);
        byte[] decrypted_signature = rsa_alg.processBlock(signature, 0, signature.length);
        System.out.println(" inpu = " + Encoding.toHexString(input));
        System.out.println(" hash = " + Encoding.toHexString(hash));
        System.out.println("");
        System.out.println(" sign = " + Encoding.toHexString(signature));
        System.out.println(" decr = " + Encoding.toHexString(decrypted_signature));

        return Encoding.arraysEqual(hash, decrypted_signature);

    } catch (Exception e) {
        e.printStackTrace();
    }

    return false;

}