• Здраво и добредојдовте на форумот на IT.mk.

    Доколку сеуште не сте дел од најголемата заедница на ИТ професионалци и ентузијасти во Македонија, можете бесплатно да се - процесот нема да ви одземе повеќе од 2-3 минути, а за полесна регистрација овозможивме и регистрирање со Facebook и Steam.

Symetric crypto

  • Ја почнал/а темата
  • #1

Megatron

Intern
18 април 2007
146
4
pravam nekoja midlet app za kriptiranje/dekriptiranje na sms poraki (vo java/J2ME)...ama imama eden problem vo decryptorot...ke go postiram kodot na klasata pa ako nekoj mozi da mi pomozi ke mu bidam mn blagodaren


package javaapplication1;

import java.util.logging.Level;
import java.util.logging.Logger;
import org.bouncycastle.crypto.BlockCipher;
import org.bouncycastle.crypto.BufferedBlockCipher;
import org.bouncycastle.crypto.CryptoException;
import org.bouncycastle.crypto.engines.TwofishEngine;
import org.bouncycastle.crypto.modes.CBCBlockCipher;
import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher;
import org.bouncycastle.crypto.params.KeyParameter;



/**
*
* @author Jan
*/
public class Encryptor {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
try {
Encryptor e = new Encryptor();
String str = "kaj si be";
System.out.print(e.encrypt(str));
String temp = e.encrypt(str);
System.out.print(e.decrypt(temp));
} catch (CryptoException ex) {
Logger.getLogger(Encryptor.class.getName()).log(Level.SEVERE, null, ex);
}

}
private BufferedBlockCipher cipher;
private KeyParameter key;

public Encryptor() {

// cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new DESEngine() ) );

BlockCipher engine = new TwofishEngine();

cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(engine));
String pass = "04071987";
this.key = new KeyParameter(pass.getBytes());
}

private byte[] callCipher(byte[] data) throws CryptoException {
int size = cipher.getOutputSize(data.length);
byte[] result = new byte[size];
int processbyte = cipher.processBytes(data, 0, data.length, result, 0);
processbyte += cipher.doFinal(result, processbyte);
if (processbyte < size) {
byte[] tmp = new byte[processbyte];
System.arraycopy(result, 0, tmp, 0, processbyte);
result = tmp;
}

return result;
}

public String encrypt(String input) throws CryptoException {
byte[] data = input.getBytes();
cipher.init(true, key);
int size = cipher.getOutputSize(data.length);
byte[] result = new byte[size];
int processbyte = cipher.processBytes(data, 0, data.length, result, 0);
processbyte += cipher.doFinal(result, processbyte);

if (processbyte < size) {
byte[] tmp = new byte[processbyte];
System.arraycopy(result, 0, tmp, 0, processbyte);
result = tmp;
}
String enc = new String(result);
return enc;
}

public String decrypt(String cryptedText) throws CryptoException {
byte[] data = cryptedText.getBytes();
cipher.init(false, key);
int size = cipher.getOutputSize(data.length);
byte[] result = new byte[size];
int processbyte = cipher.processBytes(data, 0, data.length, result, 0);
try {
processbyte += cipher.doFinal(result, processbyte);
} catch (Exception e) {
e.printStackTrace();
}
if (processbyte < size) {
byte[] tmp = new byte[processbyte];
System.arraycopy(result, 0, tmp, 0, processbyte);
result = tmp;
}
String rec = new String(result);
return rec ;
}
}

problemot e vo cipher.doFinal() vo decryptor i pojma nemam so mu e...a zogra na to vo Eclipse raboti a vo NetBeans gi vadi slednite greski


Errors:
org.bouncycastle.crypto.InvalidCipherTextException: pad block corrupted
at org.bouncycastle.crypto.paddings.PKCS7Padding.padCount(Unknown Source)
at org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher.doFinal(Unknown Source)
at javaapplication1.Encryptor.decrypt(Encryptor.java:92)
at javaapplication1.Encryptor.main(Encryptor.java:34)
 
  • Ја почнал/а темата
  • #2

Megatron

Intern
18 април 2007
146
4
ok e srediv , fintata e vo dolzinata na klucot, odnosno Twofish enkripcija e128 i 256 bitna enkripcija takso sifrata mora d abidi minimum sifra.lenght()=16
 

Нови мислења

Последни Теми

Статистика

Теми
43,728
Мислења
856,161
Членови
29,737
Најнов член
M123xyz
На врв Дно