1 package net.sourceforge.jcetaglib.test; 2 3 import junit.framework.Assert; 4 import junit.framework.TestCase; 5 import net.sourceforge.jcetaglib.lib.Crypt; 6 import net.sourceforge.jcetaglib.lib.DHKeyAgreement; 7 8 import java.security.Key ; 9 import java.security.KeyPair ; 10 import java.security.PublicKey ; 11 12 public class DHKeyAgreementTest extends TestCase { 13 private StringBuffer ciphertext = null; 14 private StringBuffer plaintext = null; 15 16 public void testAgreement() throws Exception { 17 KeyPair alicekeypair = DHKeyAgreement.generateDHKeyPair(); 19 KeyPair bobkeypair = DHKeyAgreement.generateDHKeyPair(); 20 21 byte[] bytealicekey = DHKeyAgreement.publicKeyToBytes(alicekeypair.getPublic()); byte[] bytebobkey = DHKeyAgreement.publicKeyToBytes(bobkeypair.getPublic()); 25 PublicKey alicepub = DHKeyAgreement.bytesToPublicKey(bytealicekey); 27 PublicKey bobpub = DHKeyAgreement.bytesToPublicKey(bytebobkey); 28 29 Key alicekey = DHKeyAgreement.generateSessionKey(bobpub, alicekeypair.getPrivate(), "AES", 256); 31 Key bobkey = DHKeyAgreement.generateSessionKey(alicepub, bobkeypair.getPrivate(), "AES", 256); 32 33 Assert.assertEquals(alicekey, bobkey); 34 35 for (int i = 0; i < RunTest.text.length; i++) { 36 ciphertext = Crypt.encrypt(RunTest.text[i] 38 , alicekey 39 , "AES" 40 , "CBC" 41 , "PKCS7Padding" 42 , null); 43 44 plaintext = Crypt.decrypt(ciphertext 46 , bobkey 47 , "AES" 48 , "CBC" 49 , "PKCS7Padding"); 50 51 Assert.assertEquals(plaintext.toString(), RunTest.text[i].toString()); 52 } 53 } 54 } 55 | Popular Tags |