Exemplo de encriptação e decriptação RSA em modo RAW.
package doxy.examples;
import java.util.Arrays;
import com.dinamonetworks.Dinamo;
import br.com.trueaccess.TacException;
import br.com.trueaccess.TacNDJavaLib;
public class EncDecRaw {
public static void main(String[] args) throws TacException {
Dinamo api = new Dinamo();
api.openSession("127.0.0.1", "master", "12345678", false);
byte clearText[] =
{
(byte)0x12, (byte)0x34, (byte)0x56, (byte)0x78,
(byte)0x12, (byte)0x34, (byte)0x56, (byte)0x78, (byte)0x12, (byte)0x34, (byte)0x56, (byte)0x78, (byte)0x12, (byte)0x34, (byte)0x56, (byte)0x78, (byte)0x12, (byte)0x34, (byte)0x56, (byte)0x78,
(byte)0x12, (byte)0x34, (byte)0x56, (byte)0x78, (byte)0x12, (byte)0x34, (byte)0x56, (byte)0x78, (byte)0x12, (byte)0x34, (byte)0x56, (byte)0x78, (byte)0x12, (byte)0x34, (byte)0x56, (byte)0x78,
} ;
String keyId = "rsa";
String pubKeyId = "pubrsa";
api.createKey(keyId, TacNDJavaLib.ALG_RSA_2048);
byte[] encBlock = api.encrypt(keyId, clearText, TacNDJavaLib.D_FORCE_ACTUAL_RSA);
byte[] publicKey = api.exportKey(keyId, TacNDJavaLib.PUBLICKEY_BLOB);
api.importKey( pubKeyId,
TacNDJavaLib.PUBLICKEY_BLOB_HSM,
TacNDJavaLib.ALG_OBJ_PUBKEY_RSA_BLOB,
publicKey,
true);
byte[] decBlock = api.decrypt(pubKeyId, encBlock, TacNDJavaLib.D_FORCE_ACTUAL_RSA);
if(Arrays.equals(clearText, decBlock))
{
System.out.println("OK!");
}
else
{
System.out.println("Invalid arrays!");
}
api.deleteKey(keyId);
api.deleteKey(pubKeyId);
api.closeSession();
}
}