rsa Sign byte array - Android java.security

Android examples for java.security:RSA

Description

rsa Sign byte array

Demo Code


//package com.java2s;
import java.security.KeyFactory;

import java.security.PrivateKey;

import java.security.spec.PKCS8EncodedKeySpec;

import android.util.Base64;

public class Main {
    /**//  w w  w.j a v  a2  s. c o  m
     * rsaSign
     * @param content
     * @param privateKey
     * @return
     * @throws Exception
     */
    public static byte[] rsaSign(byte[] content, byte[] privateKey)
            throws Exception {
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        byte[] encodedKey = Base64.decode(privateKey, Base64.DEFAULT);
        PrivateKey privateKeyObj = keyFactory
                .generatePrivate(new PKCS8EncodedKeySpec(encodedKey));
        java.security.Signature signature = java.security.Signature
                .getInstance("SHA1WithRSA");
        signature.initSign(privateKeyObj);
        signature.update(content);
        return signature.sign();
    }
}

Related Tutorials