Всем добрый вечер. Попытался сделать реализацию алгоритма RSA. Написал небольшой тест. Проблема в том что тест срабатывает "через раз". Не могу понять в чем ошибка. Прошу помочь. RSAKeyGenerator.javaКод | package org.jim.crypto.impl.rsa;
import org.jim.crypto.Key; import org.jim.crypto.KeyGenerator;
import java.math.BigInteger; import java.util.Random;
/** * Author: SemenovAn * Date: Oct 29, 2011 */ public class RSAKeyGenerator implements KeyGenerator { public Object createKey(Object value) { BigInteger[] mas = (BigInteger[]) value; return new Key[]{new RSAKey(mas[0], mas[1]) , new RSAKey(mas[2], mas[3])}; }
/* * Return array[2] with keys : * array[0] - public key * array[1] - private key * */ public Object generateKeys() { Random rnd = new Random(); BigInteger p = BigInteger.probablePrime(512, rnd); BigInteger q = BigInteger.probablePrime(512, rnd); BigInteger n = p.multiply(q); BigInteger fi = p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE));
BigInteger e; while(true){ e = BigInteger.probablePrime(512, rnd); if ((n .compareTo(e) == 1) && (fi.gcd(e).equals(BigInteger.ONE))) break; }
BigInteger d = e.modInverse(fi); return new Key[] {new RSAKey(e, n), new RSAKey(d, n)}; } }
|
RSAChipher.javaКод | package org.jim.crypto.impl.rsa;
import org.jim.crypto.Cipher; import org.jim.crypto.Key;
import java.math.BigInteger;
/** * Author: SemenovAn * Date: Oct 29, 2011 */ public class RSACipher implements Cipher { public byte[] encrypt(byte[] source, Key key) { BigInteger e = ((PairOfNumbers)key.getKey()).getNumb1(); BigInteger n = ((PairOfNumbers)key.getKey()).getNumb2(); BigInteger val = new BigInteger(source); val=val.modPow(e, n); return val.toByteArray(); }
public byte[] decrypt(byte[] source, Key key) { BigInteger d = ((PairOfNumbers)key.getKey()).getNumb1(); BigInteger n = ((PairOfNumbers)key.getKey()).getNumb2(); BigInteger val = new BigInteger(source); val=val.modPow(d, n); return val.toByteArray(); } }
|
CryptographerTest ошибка в тесте testRSAAndBlowfishEncDec() Код | package org.jim.crypto;
import org.jim.crypto.impl.rsa.RSACipher; import org.junit.Test;
import java.io.IOException; import java.math.BigInteger;
import static junit.framework.Assert.assertEquals;
/** * Author: SemenovAn * Date: Oct 29, 2011 */ public class CryptographerTest { private RSACipher rsaCrypto = new RSACipher();
@Test public void testRSAEncryptDecrypt() throws IOException { BigInteger msg = new BigInteger("123456789"); CryptoFacade facade = new CryptoFacade(CryptoFacade.RSA); Key[] keys = (Key[]) facade.getKey(); Key publicKey = keys[0]; Key privateKey = keys[1];
byte[] etalonBytes = msg.toByteArray();
byte[] encrypt = facade.encode(etalonBytes, publicKey);
byte[] decrypt = facade.decode(encrypt, privateKey);
assertBiteArrays(etalonBytes, decrypt); assertEquals(msg, new BigInteger(decrypt)); }
@Test public void testBlowfishEncryptDecrypt() throws IOException { // String helloWorld = "Hellow world"; BigInteger test = new BigInteger("123456789123456789"); CryptoFacade facade = new CryptoFacade(CryptoFacade.BLOWFISH); Key key = (Key) facade.getKey();
byte[] etalonBytes =test.toByteArray();
byte[] encrypt = facade.encode(etalonBytes, key);
byte[] decrypt = facade.decode(encrypt, key);
assertBiteArrays(etalonBytes, decrypt); }
@Test public void testRSAAndBlowfishEncDec() throws IOException, ClassNotFoundException { // message A -> B BigInteger test = new BigInteger("123456789123456789"); CryptoFacade rsa = new CryptoFacade("RSA"); CryptoFacade blowfish = new CryptoFacade("BLOWFISH");
Key[] keysA = (Key[]) rsa.getKey(); Key[] keysB = (Key[]) rsa.getKey(); Key openKeyB = keysB[0]; Key privateKeyB = keysB[1];
Key bfKeyA = (Key) blowfish.getKey();
byte[] blowFishKeyForBAsBytes = ((BigInteger)bfKeyA.getKey()).toByteArray();
byte[] encryptBlowFishKeyForB = rsa.encode(blowFishKeyForBAsBytes, openKeyB);
byte[] decodeBlowfishKeyOnB = rsa.decode(encryptBlowFishKeyForB, privateKeyB);
assertBiteArrays(blowFishKeyForBAsBytes, decodeBlowfishKeyOnB); // assertEquals(bfKeyA.getKey(), new BigInteger(decodeBlowfishKeyOnB));
Key bfKeyAOnBSide = (Key) blowfish.createKey(decodeBlowfishKeyOnB);
byte[] msg = test.toByteArray();
byte[] encryptMsgWithBlowFishForB = blowfish.encode(msg, bfKeyA);
byte[] decryptMsgOnBSideUsingDecryptKeyFromA = blowfish.decode(encryptMsgWithBlowFishForB, bfKeyAOnBSide);
assertBiteArrays(msg, decryptMsgOnBSideUsingDecryptKeyFromA);
assertEquals(test, new BigInteger(decryptMsgOnBSideUsingDecryptKeyFromA)); }
private void assertBiteArrays(byte[] expected, byte[] actual) { assertEquals("Length not equals ", expected.length, actual.length); for (int i =0 ; i<expected.length; i++) assertEquals(expected[i], actual[i]); } }
|
Ошибка летит следущая: Код | junit.framework.AssertionFailedError: Length not equals expected:<129> but was:<128> at org.jim.crypto.CryptographerTest.assertBiteArrays(CryptographerTest.java:96) at org.jim.crypto.CryptographerTest.testRSAAndBlowfishEncDec(CryptographerTest.java:77) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
|
Т.е. дешифрованное сообщение и зашифрованное отличаются по длине, почему такое происходит ? Подозреваю , проблема в разрядности, но вроде пытаюсь генерить ключи длины 512. Это сообщение отредактировал(а) rang3r - 8.11.2011, 21:25
|