/*
* This file is subject to the licence found in LICENCE.TXT in the root directory of the project.
* Copyright Jayasoft 2005 - All rights reserved
*
* #SNAPSHOT#
*/
package fr.jayasoft.crypto;
import java.io.File;
import java.io.IOException;
import java.security.KeyPair;
import junit.framework.TestCase;
import fr.jayasoft.crypto.decoder.Decoder;
import fr.jayasoft.crypto.encoder.Encoder;
import fr.jayasoft.crypto.utils.DecoderUtils;
import fr.jayasoft.crypto.utils.EncoderUtils;
public class KeyFileTest extends TestCase {
KeyPair _kp = null;
public void testCreateKeyFile() {
try {
File fPubKey = File.createTempFile("publicKey", ".key");
File fPriKey = File.createTempFile("privateKey", ".key");
fPubKey.deleteOnExit();
fPriKey.deleteOnExit();
RSAHelper.createKeyFile(fPriKey, _kp.getPrivate());
RSAHelper.createKeyFile(fPubKey, _kp.getPublic());
// we check file sizes
assertTrue(fPriKey.length()!=0);
assertTrue(fPubKey.length()!=0);
} catch (IOException e) {
assertTrue(false);
}
}
public void testEncodeDecodeUsingKeyFile() {
try {
File fPubKey = File.createTempFile("publicKey", ".key");
File fPriKey = File.createTempFile("privateKey", ".key");
fPubKey.deleteOnExit();
fPriKey.deleteOnExit();
RSAHelper.createKeyFile(fPriKey, _kp.getPrivate());
RSAHelper.createKeyFile(fPubKey, _kp.getPublic());
// we check file sizes
assertTrue(fPriKey.length()!=0);
assertTrue(fPubKey.length()!=0);
String valToEncode = "some value";
Encoder e = EncoderUtils.createRSAEncoder(fPubKey);
Decoder d = DecoderUtils.createRSADecoder(fPriKey);
byte[] decodedValue = d.decode(e.encode(valToEncode.getBytes()));
assertEquals(valToEncode, new String(decodedValue));
} catch (IOException e) {
assertTrue(false);
}
}
protected void setUp() throws Exception {
_kp = RSAHelper.generateKeyPair();
}
}