对称加密不起作用

时间:2014-09-10 10:55:41

标签: java encryption

我正在尝试生成密文和解密数据。我已经分配了我的incrypted值和密钥。但是当我在netbeans中运行程序时,它会在执行区中显示错误,但不会产生密文和解密数据。

Encryption.java:

package encryption;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

public class Encryption {

    private static byte[] message;

    public static void main(String[] args) throws Exception {

        byte[] message = {0, 0, 1, 2, 3, 4, 5, 6, 7, 6, 5, 4, 2, 3, 8, 9};
        byte[] key = {1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4};
        byte[] ciphertext;
        byte[] decrypted;

        Sender s = new Sender();
        s.send(message);
        Receiver r = new Receiver();
        r.receive(message);
        // TODO code application logic here
    }

}

Sender.java:

package encryption;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

public class Sender {

    private Cipher encoder;
    private SecretKeySpec myKey;

    public Sender() throws Exception {
       encoder = Cipher.getInstance("AES");
    }

    public void setKey(byte[] key) throws Exception {
      myKey = new SecretKeySpec(key, "AES");
        encoder.init(Cipher.ENCRYPT_MODE, myKey);
    }

    public byte[] send(byte[] message) throws Exception {
        return encoder.doFinal(message);
    }
}

Receiver.java:

package encryption;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

public class Receiver {
    private Cipher decoder;
    private SecretKeySpec myKey;
    public Receiver()throws Exception{

        decoder=Cipher.getInstance("AES");
    }
    public void setKey(byte[] key) throws Exception{

        myKey= new SecretKeySpec(key ,"AES");
        decoder.init(Cipher.DECRYPT_MODE, myKey);
    }
    public byte[] receive(byte[] message) throws Exception{
        return decoder.doFinal(message);
    }
}

0 个答案:

没有答案