GNU/Linux >> LINUX-Kenntnisse >  >> Linux

Ausnahme wegen illegaler Blockgröße – CKR_ENCRYPTED_DATA_LEN_RANGE [SoftHSM]

Ich habe SoftHSM erfolgreich auf CentOS installiert und konfiguriert. Um zu überprüfen, ob ich programmgesteuert mit SoftHSM kommunizieren kann, habe ich ein Java-Programm geschrieben, das mit dem Softtoken interagiert und eine Verschlüsselung und Entschlüsselung einer Eingabezeichenfolge durchführt. Das Programm konnte die Eingabe jedoch erfolgreich verschlüsseln, kann sie jedoch nicht entschlüsseln. Die Entschlüsselungsmethode schlägt mit einem Fehler fehl – ​​Unzulässige Blockgrößenausnahme – CKR_ENCRYPTED_DATA_LEN_RANGE.

Hier ist der Teilcode.

String pkcs11ConfigData = "softhsm.cfg";
Provider pkcs11Provider = Security.getProvider("SunPKCS11");
pkcs11Provider = pkcs11Provider.configure(pkcs11ConfigData);

if (-1 == Security.addProvider(pkcs11Provider)) {
throw new RuntimeException("could not add security provider");
} else {
System.out.println("provider initialized !!!");
}

Security.addProvider(pkcs11Provider);

char[] pin = "abcdef123".toCharArray();
KeyStore keyStore;
try {
keyStore = KeyStore.getInstance("PKCS11", pkcs11Provider);
keyStore.load(null, pin);
SecretKeySpec secretKeySpec = new SecretKeySpec("0123456789ABCDEF".getBytes(), 0, 16, "AES");
Key key = new SecretKeySpec(secretKeySpec.getEncoded(), 0, 16, "AES");
keyStore.setKeyEntry("AA", key, "abcdef123".toCharArray(), null);
keyStore.store(null);
SecretKey key1 = (SecretKey) keyStore.getKey("AA", "abcdef123".toCharArray());
System.out.println("the algorithm: "+key1.getAlgorithm()+", the key: "+key1.toString()+", format: "+key1.serialVersionUID);

doCrypto(Cipher.ENCRYPT_MODE, key1, inputtext);

doCrypto(Cipher.DECRYPT_MODE, key1, encryptedtext);

} catch (KeyStoreException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (CertificateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (UnrecoverableKeyException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}

private static void doCrypto(int cipherMode, Key key, String inputtext) throws Exception {
Key secretKey = key;
Cipher cipher;
try {
cipher = Cipher.getInstance(TRANSFORMATION);

cipher.init(cipherMode, secretKey);
byte[] inputBytes = inputtext.getBytes();
byte[] outputBytes;
outputBytes = cipher.doFinal(inputBytes);
if(cipherMode == Cipher.ENCRYPT_MODE) {
encryptedtext = outputBytes.toString();
System.out.println("==========================================");
System.out.println("encrypted text: "+outputBytes.toString());
System.out.println("==========================================");
}
if(cipherMode == Cipher.DECRYPT_MODE) {
System.out.println("*****************************************");
System.out.println("decrypted text: "+outputBytes.toString());
System.out.println("*****************************************");
}

} catch (NoSuchAlgorithmException e) {
System.out.println("No such algorithm exception");
e.printStackTrace();
} catch (NoSuchPaddingException e) {
System.out.println("No such padding exception");
e.printStackTrace();
} catch (InvalidKeyException e) {
System.out.println("Invalid key exception");
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
System.out.println("Illegal block size exception");
e.printStackTrace();
} catch (BadPaddingException e) {
System.out.println("Bad padding exception");
e.printStackTrace();
} finally {

}
}

Ausgabefehler:

Also, wie habe ich es zum Laufen gebracht? Hier ist die Lösung.

So beheben Sie die Unzulässige Blockgrößenausnahme – CKR_ENCRYPTED_DATA_LEN_RANGE-Fehler beim Zugriff auf SoftHSM über Java-Code?

Da ich die Zeichenfolge mit dem AES-Algorithmus verschlüssele, ist die erwartete Rückgabe ein Array von Bytes. Das direkte Konvertieren dieser Bytes in einen String, wie unten gezeigt, verursachte das Problem.

encryptedtext = outputBytes.toString();

In der obigen Codezeile sind die outputBytes ist vom Typ bytes [] und das direkte Konvertieren in einen String war das Problem.

Um dieses Problem zu beheben, müssen wir Base64 verwenden anstatt es direkt in einen String umzuwandeln.

byte[] cipherText = cipher.doFinal(inputtext.getBytes("utf-8"));
encryptedText = java.util.Base64.getEncoder().encodeToString(cipherText);

Die obigen Zeilen wurden leicht umgeschrieben, um den cipherText zu codieren mit Base64 und speichern Sie dasselbe in encryptedText .

Ebenso müssen wir beim Entschlüsseln den Base64 konvertieren String zurück in ein Byte-Array, bevor das Byte-Array entschlüsselt wird.

byte[] deciphered = cipher.doFinal(java.util.Base64.getDecoder().decode(encryptedText));

Die obige Codezeile legt nahe, dass wir Base64 ausführen müssen entschlüsseln und als Argument an die Entschlüsselungsmethode übergeben.

Das ist es! Hier ist der vollständige Code als Referenz.

private static String performEncryption(Key secretKey, String inputtext) throws Exception {
String encryptedText = new String();
Cipher cipher;
try {
cipher = Cipher.getInstance(TRANSFORMATION);
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] cipherText = cipher.doFinal(inputtext.getBytes("utf-8"));
encryptedText = java.util.Base64.getEncoder().encodeToString(cipherText);
} catch (NoSuchAlgorithmException e) {
System.out.println("No such algorithm exception");
e.printStackTrace();
} catch (NoSuchPaddingException e) {
System.out.println("No such padding exception");
e.printStackTrace();
} catch (InvalidKeyException e) {
System.out.println("Invalid key exception");
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
System.out.println("Illegal block size exception");
e.printStackTrace();
} catch (BadPaddingException e) {
System.out.println("Bad padding exception");
e.printStackTrace();
} finally {

}
return encryptedText;
}

private static void performDecryption(Key key, String encryptedString) throws Exception {
Key secretKey = key;
Cipher cipher;
try {
cipher = Cipher.getInstance(TRANSFORMATION);
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] deciphered = cipher.doFinal(java.util.Base64.getDecoder().decode(encryptedString));
System.out.println("decrypted text: "+new String(deciphered));
} catch (NoSuchAlgorithmException e) {
System.out.println("No such algorithm exception");
e.printStackTrace();
} catch (NoSuchPaddingException e) {
System.out.println("No such padding exception");
e.printStackTrace();
} catch (InvalidKeyException e) {
System.out.println("Invalid key exception");
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
System.out.println("Illegal block size exception");
e.printStackTrace();
} catch (BadPaddingException e) {
System.out.println("Bad padding exception");
e.printStackTrace();
} finally {

}
}

Dank dieser Antwort von Stackflow, die mir geholfen hat, das Problem zu beheben.

Zum Schluss noch der Screenshot der erfolgreichen Ausgabe.


Linux
  1. Ausnahme wegen illegaler Blockgröße – CKR_ENCRYPTED_DATA_LEN_RANGE [SoftHSM]

  2. Warum hat der Server meine IP blockiert?

  3. Unterschied zwischen Blockgröße und Clustergröße?

  4. Linux – Bestimmen Sie die Größe eines Blockgeräts?

  5. Problem mit der Blockgröße von USB Startup Disk Creator?

So verwenden Sie du Command unter Linux

valgrind – Adresse ---- ist 0 Bytes nach einem zugewiesenen Block der Größe 8

Bestimmen Sie die Größe eines Blockgeräts

Wie stellen Sie die bzip2-Blockgröße ein, wenn Sie tar verwenden?

Wie bestimme ich die Blockgröße einer ext3-Partition unter Linux?

So bestimmen Sie die beste Bytegröße für den Befehl dd