Java Source Code
[Link]
import import import import import import import [Link].*; [Link].*; [Link].*; [Link]; [Link]; [Link]; [Link].*;
class testDES { public static void main (String[] args) { try { FileOutputStream outFile1 = new FileOutputStream("[Link]"); // Note: PrintStream is deprecated, but still works fine in jdk1.1.7b PrintStream output1 = new PrintStream(outFile1); // convert a string to a DES key and print out the result RawSecretKey key2 = new RawSecretKey("DES",[Link]("3812A419C63BE771")); RawKey rkey = (RawKey) key2; byte[] yval = [Link](); BigInteger Bkey = new BigInteger(yval); String w = [Link](Bkey); [Link]("The Encryption Key = " + w); // use the DES key to encrypt a string Cipher des=[Link]("DES/ECB/NONE","Cryptix"); [Link](key2); byte[] ciphertext = [Link]([Link]("01010101010101010102030405060708090A0B0C0D0E0F10111 2131415161718")); // print out length and representation of ciphertext [Link]("\n"); [Link]("[Link] = " + [Link]); BigInteger Bciph = new BigInteger(ciphertext); w = [Link](Bciph); [Link]("Ciphertext for DES encryption = " + w); // decrypt ciphertext [Link](key2); ciphertext = [Link](ciphertext); [Link]("\n"); [Link]("[Link] = " + [Link]); // print out representation of decrypted ciphertext Bciph = new BigInteger(ciphertext);
w = [Link](Bciph); [Link]("Plaintext for DES encryption = " + w); [Link](" "); [Link](); } catch (Exception e) { [Link]("Caught exception " + [Link]()); } }}
Sample Program Output
[Link]
The Encryption Key = Multi-Precision Integer 62 bits long... sign: Positive magnitude: 3812A419C63BE771 [Link] = 32 Ciphertext for DES encryption = Multi-Precision Integer 254 bits long... sign: Positive magnitude: 3A2EAD12F475D82C 1FC97BB9A6D955E1 EA5541946BB4F2E6 F29555A6E8F1FB3C [Link] = 32 Plaintext for DES encryption = Multi-Precision Integer 249 bits long... sign: Positive magnitude: 0101010101010101 0102030405060708 090A0B0C0D0E0F10 1112131415161718
RSA
import import import import import import import
[Link]; [Link]; [Link]; [Link]; [Link]; [Link]; [Link];
import [Link]; public class MainClass {
public static void main(String[] args) throws Exception { [Link](new [Link] er()); byte[] input = new byte[] { (byte) 0xbe, (byte) 0xef }; Cipher cipher = [Link]("RSA/None/NoPadding", "BC"); KeyFactory keyFactory = [Link]("RSA", "BC"); RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(new BigInteger( "12345678", 16), new BigInteger("11", 16)); RSAPrivateKeySpec privKeySpec = new RSAPrivateKeySpec(new BigInteger( "12345678", 16), new BigInteger("12345678", 16)); RSAPublicKey pubKey = (RSAPublicKey) [Link](pubKeySpec ); RSAPrivateKey privKey = (RSAPrivateKey) [Link](privKe ySpec); [Link](Cipher.ENCRYPT_MODE, pubKey); byte[] cipherText = [Link](input); [Link]("cipher: " + new String(cipherText)); [Link](Cipher.DECRYPT_MODE, privKey); byte[] plainText = [Link](cipherText); [Link]("plain : " + new String(plainText)); } }
aes
import import import import
[Link].*; [Link].*; [Link].*; [Link].*;
/** * This program generates a AES key, retrieves its raw bytes, and * then reinstantiates a AES key from the key bytes. * The reinstantiated key is used to initialize a AES cipher for * encryption and decryption. */ public class AES { /** * Turns array of bytes into string * * @param buf Array of bytes to convert to hex string * @return Generated hex string */ public static String asHex (byte buf[]) { StringBuffer strbuf = new StringBuffer([Link] * 2); int i;
for (i = 0; i < [Link]; i++) { if (((int) buf[i] & 0xff) < 0x10) [Link]("0"); [Link]([Link]((int) buf[i] & 0xff, 16)); } return [Link](); } public static void main(String[] args) throws Exception { String message="This is just an example"; // Get the KeyGenerator KeyGenerator kgen = [Link]("AES"); [Link](128); // 192 and 256 bits may not be available // Generate the secret key specs. SecretKey skey = [Link](); byte[] raw = [Link](); SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); // Instantiate the cipher Cipher cipher = [Link]("AES"); [Link](Cipher.ENCRYPT_MODE, skeySpec); byte[] encrypted = [Link](([Link] == 0 ? "This is just an example" : args[0]).getBytes()); [Link]("encrypted string: " + asHex(encrypted)); [Link](Cipher.DECRYPT_MODE, skeySpec); byte[] original = [Link](encrypted); String originalString = new String(original); [Link]("Original string: " + originalString + " " + asHex(original)); } }
Sha
import import import import import [Link]; [Link]; [Link]; [Link]; [Link];
public class Ex01 { public static void main(String[] args) throws IOException { BufferedReader userInput = new BufferedReader (new InputStreamReader([Link])); [Link]("Enter string:"); String rawString = [Link](); try { [Link]("SHA1 hash of string: " + AeSimpleSHA1.SHA1(rawString)); } catch (NoSuchAlgorithmException e) { // TODO Auto-generated catch block [Link](); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block [Link](); } } }
Implementing the Diffie-Hellman key exchange
import import import import import [Link]; [Link]; [Link]; [Link]; [Link];
import [Link]; import [Link]; public class MainClass { public final static int pValue = 47; public final static int gValue = 71; public final static int XaValue = 9; public final static int XbValue = 14; public static void main(String[] args) throws Exception { BigInteger p = new BigInteger([Link](pValue)); BigInteger g = new BigInteger([Link](gValue)); BigInteger Xa = new BigInteger([Link](XaValue)); BigInteger Xb = new BigInteger([Link](XbValue));
createKey(); int bitLength = 512; // 512 bits SecureRandom rnd = new SecureRandom(); p = [Link](bitLength, rnd); g = [Link](bitLength, rnd); createSpecificKey(p, g); } public static void createKey() throws Exception { KeyPairGenerator kpg = [Link]("DiffieHellman"); [Link](512); KeyPair kp = [Link](); KeyFactory kfactory = [Link]("DiffieHellman"); DHPublicKeySpec kspec = (DHPublicKeySpec) [Link]([Link](), [Link]); } public static void createSpecificKey(BigInteger p, BigInteger g) throws Exception { KeyPairGenerator kpg = [Link]("DiffieHellman"); DHParameterSpec param = new DHParameterSpec(p, g); [Link](param); KeyPair kp = [Link](); KeyFactory kfactory = [Link]("DiffieHellman"); DHPublicKeySpec kspec = (DHPublicKeySpec) [Link]([Link](), [Link]); } }