EncDecRaw.java

Exemplo de encriptação e decriptação RSA em modo RAW.

Veja Nota sobre os exemplos.
package doxy.examples;
import java.util.Arrays;
public class EncDecRaw {
public static void main(String[] args) throws TacException {
Dinamo api = new Dinamo();
api.openSession("10.0.62.10", "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.deleteKey(keyId);
api.deleteKey(pubKeyId);
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.closeSession();
}
}