Created
April 23, 2015 12:33
-
-
Save rogermb/25326d975eed0de717ed to your computer and use it in GitHub Desktop.
Java Encryption examples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Default package for convenience | |
// package me.firedroide.einf.crypto; | |
import javax.crypto.Cipher; | |
import javax.crypto.KeyGenerator; | |
import javax.crypto.SecretKey; | |
import javax.crypto.spec.SecretKeySpec; | |
import java.nio.charset.Charset; | |
import java.util.Scanner; | |
public class AESEncryption { | |
private static final Charset UTF_8 = Charset.forName("UTF-8"); | |
public static void main(String[] args) throws Exception { | |
Scanner in = new Scanner(System.in); | |
// Klartext einlesen | |
System.out.println("Zu verschlüsselnde Nachricht:"); | |
String input = in.nextLine(); | |
byte[] inputBytes = input.getBytes(UTF_8); | |
System.out.print("Schlüssel (oder leer für zufälligen Schlüssel): "); | |
String keyString = in.nextLine(); | |
in.close(); | |
// Schlüssel einlesen oder generieren | |
SecretKey secretKey; | |
if (keyString.isEmpty()) { | |
secretKey = generateKey(); | |
System.out.println("Verwende zufälligen Schlüssel:"); | |
System.out.println(toHexString(secretKey.getEncoded())); | |
} else { | |
byte[] keyBytes = keyString.getBytes(UTF_8); | |
secretKey = new SecretKeySpec(keyBytes, "AES"); | |
} | |
// Verschlüsseln | |
byte[] encrypted = encrypt(inputBytes, secretKey); | |
System.out.println("\nVerschlüsselt:"); | |
System.out.println(toHexString(encrypted)); | |
// Entschlüsseln | |
byte[] decrypted = decrypt(encrypted, secretKey); | |
System.out.println("\nEntschlüsselt:"); | |
System.out.println(toHexString(decrypted)); | |
} | |
private static SecretKey generateKey() throws Exception { | |
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES"); | |
return keyGenerator.generateKey(); | |
} | |
private static byte[] encrypt(byte[] input, SecretKey key) throws Exception { | |
Cipher cipher = Cipher.getInstance("AES"); | |
cipher.init(Cipher.ENCRYPT_MODE, key); | |
byte[] encrypted = cipher.doFinal(input); | |
return encrypted; | |
} | |
private static byte[] decrypt(byte[] input, SecretKey key) throws Exception { | |
Cipher cipher = Cipher.getInstance("AES"); | |
cipher.init(Cipher.DECRYPT_MODE, key); | |
byte[] decrypted = cipher.doFinal(input); | |
return decrypted; | |
} | |
private static String toHexString(byte[] input) { | |
String inString = new String(input, UTF_8); | |
StringBuilder hexString = new StringBuilder(inString.length() + input.length * 2 + 4); | |
hexString.append(inString).append("\n("); | |
for (byte b : input) { | |
int val = Byte.toUnsignedInt(b); | |
String hexByte = Integer.toHexString(val); | |
hexString.append(hexByte); | |
} | |
hexString.append(")"); | |
return hexString.toString(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Default package for convenience | |
// package me.firedroide.einf.crypto; | |
import java.util.Scanner; | |
public class FenceCipher { | |
public static void main(String[] args) throws Exception { | |
Scanner in = new Scanner(System.in); | |
// Klartext einlesen | |
System.out.println("Zu verschlüsselnde Nachricht:"); | |
String input = in.nextLine(); | |
System.out.print("[V]erschlüsseln oder [E]ntschlüsseln? "); | |
String mode = in.nextLine().toLowerCase(); | |
in.close(); | |
if (mode.startsWith("v")) { | |
// Verschlüsseln | |
String encrypted = encrypt(input); | |
System.out.println("Verschlüsselt:"); | |
System.out.println(encrypted); | |
} else if (mode.startsWith("e")) { | |
// Entschlüsseln | |
String decrypted = decrypt(input); | |
System.out.println("Entschlüsselt:"); | |
System.out.println(decrypted); | |
} | |
} | |
private static String encrypt(String inString) { | |
char[] input = inString.toCharArray(); | |
int length = input.length; | |
char[] output = new char[length]; | |
int c = 0; | |
for (int i1 = 0; i1 < length; i1 += 2) { | |
output[c++] = input[i1]; | |
} | |
for (int i2 = 1; i2 < length; i2 += 2) { | |
output[c++] = input[i2]; | |
} | |
return new String(output); | |
} | |
private static String decrypt(String inString) { | |
char[] input = inString.toCharArray(); | |
int length = input.length; | |
char[] output = new char[length]; | |
int c = 0; | |
for (int i1 = 0; i1 < length; i1 += 2) { | |
output[i1] = input[c++]; | |
} | |
for (int i2 = 1; i2 < length; i2 += 2) { | |
output[i2] = input[c++]; | |
} | |
return new String(output); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Default package for convenience | |
// package me.firedroide.einf.crypto; | |
import javax.crypto.Cipher; | |
import java.nio.charset.Charset; | |
import java.security.KeyPair; | |
import java.security.KeyPairGenerator; | |
import java.security.PrivateKey; | |
import java.security.PublicKey; | |
import java.util.Scanner; | |
public class RSAEncryption { | |
private static final Charset UTF_8 = Charset.forName("UTF-8"); | |
public static void main(String[] args) throws Exception { | |
Scanner in = new Scanner(System.in); | |
// Klartext einlesen | |
System.out.println("Zu verschlüsselnde Nachricht:"); | |
String input = in.nextLine(); | |
byte[] inputBytes = input.getBytes(UTF_8); | |
in.close(); | |
// Schlüssel generieren | |
System.out.println("Generiere Schlüssel... (Könnte eine Zeit dauern!)"); | |
KeyPair keyPair = generateKeyPair(); | |
PublicKey publicKey = keyPair.getPublic(); | |
PrivateKey privateKey = keyPair.getPrivate(); | |
System.out.println("\nPrivater Schlüssel:\n" + toHexString(privateKey.getEncoded())); | |
System.out.println("\nÖffentlicher Schlüssel:\n" + toHexString(publicKey.getEncoded())); | |
// Verschlüsseln | |
byte[] encrypted = encrypt(inputBytes, publicKey); | |
System.out.println("\nVerschlüsselt:"); | |
System.out.println(toHexString(encrypted)); | |
// Entschlüsseln | |
byte[] decrypted = decrypt(encrypted, privateKey); | |
System.out.println("\nEntschlüsselt:"); | |
System.out.println(toHexString(decrypted)); | |
} | |
private static KeyPair generateKeyPair() throws Exception { | |
KeyPairGenerator keyGenerator = KeyPairGenerator.getInstance("RSA"); | |
keyGenerator.initialize(2048); | |
return keyGenerator.generateKeyPair(); | |
} | |
private static byte[] encrypt(byte[] input, PublicKey key) throws Exception { | |
Cipher rsa = Cipher.getInstance("RSA"); | |
rsa.init(Cipher.ENCRYPT_MODE, key); | |
byte[] encrypted = rsa.doFinal(input); | |
return encrypted; | |
} | |
private static byte[] decrypt(byte[] input, PrivateKey key) throws Exception { | |
Cipher rsa = Cipher.getInstance("RSA"); | |
rsa.init(Cipher.DECRYPT_MODE, key); | |
byte[] decrypted = rsa.doFinal(input); | |
return decrypted; | |
} | |
private static String toHexString(byte[] input) { | |
String inString = new String(input, UTF_8); | |
StringBuilder hexString = new StringBuilder(inString.length() + input.length * 2 + 4); | |
hexString.append(inString).append("\n("); | |
for (byte b : input) { | |
int val = Byte.toUnsignedInt(b); | |
String hexByte = Integer.toHexString(val); | |
hexString.append(hexByte); | |
} | |
hexString.append(")"); | |
return hexString.toString(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Default package for convenience | |
// package me.firedroide.einf.crypto; | |
import java.util.Scanner; | |
public class VigenèreCipher { | |
public static void main(String[] args) throws Exception { | |
Scanner in = new Scanner(System.in); | |
// Klartext einlesen | |
System.out.println("Zu verschlüsselnde Nachricht:"); | |
String input = in.nextLine(); | |
// Schlüssel einlesen | |
System.out.print("Zu verwendender Schlüssel: "); | |
String key = in.nextLine(); | |
System.out.print("[V]erschlüsseln oder [E]ntschlüsseln? "); | |
String mode = in.nextLine().toLowerCase(); | |
in.close(); | |
if (mode.startsWith("v")) { | |
// Verschlüsseln | |
String encrypted = encrypt(input, key); | |
System.out.println("Verschlüsselt:"); | |
System.out.println(encrypted); | |
} else if (mode.startsWith("e")) { | |
// Entschlüsseln | |
String decrypted = decrypt(input, key); | |
System.out.println("Entschlüsselt:"); | |
System.out.println(decrypted); | |
} | |
} | |
private static String encrypt(String inString, String keyString) { | |
int[] input = toOffsets(inString); | |
int[] key = toOffsets(keyString.toLowerCase()); | |
int[] offsets = new int[input.length]; | |
for (int c = 0; c < input.length; ++c) { | |
int k = c % key.length; | |
if (input[c] == -1) { | |
offsets[c] = -1; | |
} else { | |
offsets[c] = (input[c] + key[k]) % 26; | |
} | |
} | |
return toString(inString, offsets); | |
} | |
private static String decrypt(String inString, String keyString) { | |
int[] input = toOffsets(inString); | |
int[] key = toOffsets(keyString.toLowerCase()); | |
int[] offsets = new int[input.length]; | |
for (int c = 0; c < input.length; ++c) { | |
int k = c % key.length; | |
if (input[c] == -1) { | |
offsets[c] = -1; | |
} else { | |
offsets[c] = (input[c] - key[k] + 26) % 26; | |
} | |
} | |
return toString(inString, offsets); | |
} | |
private static int[] toOffsets(String inString) { | |
char[] chars = inString.toCharArray(); | |
int[] offsets = new int[chars.length]; | |
for (int k = 0; k < offsets.length; ++k) { | |
char c = chars[k]; | |
if (c >= 'A' && c <= 'Z') { | |
offsets[k] = c - 'A'; | |
} else if (c >= 'a' && c <= 'z') { | |
offsets[k] = c - 'a'; | |
} else { | |
offsets[k] = -1; // Do not encrypt | |
} | |
} | |
return offsets; | |
} | |
private static String toString(String originalString, int[] offsets) { | |
char[] original = originalString.toCharArray(); | |
char[] output = new char[offsets.length]; | |
for (int k = 0; k < offsets.length; ++k) { | |
char c = original[k]; | |
char low = Character.isUpperCase(c) ? 'A' : 'a'; | |
int offset = offsets[k]; | |
if (offset == -1) { | |
output[k] = original[k]; // Skip | |
} else { | |
char out = (char) (low + offsets[k]); | |
output[k] = out; | |
} | |
} | |
return new String(output); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment