[JAVA] SpringSecurity TextEncryptor: Allgemeine Schlüsselverschlüsselung / -entschlüsselung

Einführung

Verschlüsselung und Entschlüsselung werden häufig verwendet. Spring Security verfügt über eine praktische Benutzeroberfläche und ist einfach zu verwenden. Erstellen wir also ein Beispiel.

Verschlüsselungs- / Entschlüsselungsschnittstelle

Für Text: org.springframework.security.crypto.encrypt.TextEncryptor Für das Byte-Array: org.springframework.security.crypto.encrypt.BytesEncryptor

Schnittstelle zur zufälligen Generierung

Für Text: org.springframework.security.crypto.keygen.StringKeyGenerator Für das Byte-Array: org.springframework.security.crypto.keygen.BytesKeyGenerator

Stichprobe

package sample.security;

import java.util.Base64;

import org.apache.commons.codec.binary.Hex;
import org.springframework.security.crypto.encrypt.BytesEncryptor;
import org.springframework.security.crypto.encrypt.Encryptors;
import org.springframework.security.crypto.encrypt.TextEncryptor;
import org.springframework.security.crypto.keygen.KeyGenerators;

public class Test {

	public static void main(String[] args) {

		//Klartext
		String orginalText = "Ich mag ◯◯.";

		//Gemeinsamer Schlüssel
		String encryptSecrerKey = "GOV2dkHGQcE1ZcX8";

		//Salz: Zufällige Generation
		byte[] bytes = KeyGenerators.secureRandom(16).generateKey();
		String salt = new String(Hex.encodeHexString(bytes));

		//CBC-Methode
		TextEncryptor cbcTextEncryptor = Encryptors.text(orginalText, salt);
		//Verschlüsselung
		String encryptedCbcData = cbcTextEncryptor.encrypt(orginalText);
		System.out.println("Verschlüsseltes Ergebnis (AES-CBC): " + encryptedCbcData);
		//Entschlüsselung
		String decryptedCbcData = cbcTextEncryptor.decrypt(encryptedCbcData);
		System.out.println("Entschlüsseltes Ergebnis (AES-CBC): " + decryptedCbcData);

		//GCM-Methode
		TextEncryptor aesGcmTextEncryptor = Encryptors.delux(encryptSecrerKey, salt);
		//Verschlüsselung
		String encryptedData = aesGcmTextEncryptor.encrypt(orginalText);
		System.out.println("Verschlüsseltes Ergebnis (AES-GCM): " + encryptedData);
		//Entschlüsselung
		String decryptedData = aesGcmTextEncryptor.decrypt(encryptedData);
		System.out.println("Entschlüsseltes Ergebnis (AES-GCM): " + decryptedData);

		//binär(CBC)
		BytesEncryptor binaryEncryptor = Encryptors.standard(encryptSecrerKey, salt);
		//Verschlüsselung
		String encryptedBinaryData = Base64.getEncoder()
				.encodeToString(binaryEncryptor.encrypt(orginalText.getBytes()));
		System.out.println("Verschlüsseltes Ergebnis (binäre CBC): " + encryptedBinaryData);
		//Entschlüsselung
		byte[] decryptedBinaryData = binaryEncryptor.decrypt(Base64.getDecoder().decode(encryptedBinaryData));
		System.out.println("Entschlüsseltes Ergebnis (binäre CBC): " + new String(decryptedBinaryData));

		//binär(GCM)
		BytesEncryptor aesGcmBinaryEncryptor = Encryptors.stronger(encryptSecrerKey, salt);
		//Verschlüsselung
		String gcmEncryptedBinaryData = Base64.getEncoder()
				.encodeToString(aesGcmBinaryEncryptor.encrypt(orginalText.getBytes()));
		System.out.println("Verschlüsseltes Ergebnis (binäres GCM): " + gcmEncryptedBinaryData);
		//Entschlüsselung
		byte[] gcmDecryptedBinaryData = binaryEncryptor.decrypt(Base64.getDecoder().decode(encryptedBinaryData));
		System.out.println("Entschlüsseltes Ergebnis (binäres GCM): " + new String(gcmDecryptedBinaryData));
	}
}


Beispiel für ein Ausführungsergebnis:

Verschlüsseltes Ergebnis (AES-CBC): a34e20774314b85322de3eaf6c10f6990befe353d382cc449039524ffb7042b442f2a7a00beeddc94c8ad1495f6a0fba Entschlüsseltes Ergebnis (AES-CBC): Ich mag ◯◯. Verschlüsseltes Ergebnis (AES-GCM): 1f5c89aeabf9f05a3cb286d1eca6e06ecd3259e9e52cea96d80e41d60614e436657317b7e69ad5b3e8672adaadd2434af69cbd36b516462908bef26d Entschlüsseltes Ergebnis (AES-GCM): Ich mag ◯◯. Verschlüsseltes Ergebnis (binäres CBC): zQg1LujXz6lpMwVoqNvG + ETtDbEC / LE0kq1LKJ + 4d8ntbPDHDsAC0e5w6Eym7Hps Entschlüsseltes Ergebnis (binäres CBC): Ich mag ◯◯. Verschlüsseltes Ergebnis (binäres GCM): gSFtQJCV0OSkjdg8sf + WHxYAFIdZrgb6MFWsPTmzau67lzFVW + ZJP36bWPQQ0p8 / QZ9FIgQ7isZm4fIYt1g = Entschlüsseltes Ergebnis (binäres GCM): Ich mag ◯◯.

Referenz: https://docs.spring.io/spring-security/site/docs/5.2.1.RELEASE/reference/htmlsingle/#spring-security-crypto-encryption-text

das ist alles

Recommended Posts

SpringSecurity TextEncryptor: Allgemeine Schlüsselverschlüsselung / -entschlüsselung
Beispiel für die Erstellung / Verschlüsselung / Entschlüsselung von RSA-Schlüsselpaaren (JAVA)
Java-Ver- und Entschlüsselung PDF
RSA-Verschlüsselung / Entschlüsselung mit Java 8