Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在处理DES和AES的java加密程序中实现opmodes_Java - Fatal编程技术网

在处理DES和AES的java加密程序中实现opmodes

在处理DES和AES的java加密程序中实现opmodes,java,Java,我提供了与ECB一起工作的代码,但我还需要能够实现CBC、CFB、OFB和CTR。如果我能让这些模式工作,那么我就可以通过测试错误传播和模式保留来完成我的项目。我目前有DES版本供您查看,但理想情况下,我应该能够做一些轻微的修改,使其也支持AES 编写各种入门级项目和一些与安全相关的项目,但之前没有加密经验 import com.sun.xml.internal.messaging.saaj.packaging.mime.util.BASE64DecoderStream; impo

我提供了与ECB一起工作的代码,但我还需要能够实现CBC、CFB、OFB和CTR。如果我能让这些模式工作,那么我就可以通过测试错误传播和模式保留来完成我的项目。我目前有DES版本供您查看,但理想情况下,我应该能够做一些轻微的修改,使其也支持AES

编写各种入门级项目和一些与安全相关的项目,但之前没有加密经验

   import com.sun.xml.internal.messaging.saaj.packaging.mime.util.BASE64DecoderStream;
    import com.sun.xml.internal.messaging.saaj.packaging.mime.util.BASE64EncoderStream;
    import java.io.UnsupportedEncodingException;
    import java.security.InvalidKeyException;
    import java.security.NoSuchAlgorithmException;
    import javax.crypto.BadPaddingException;
    import javax.crypto.Cipher;
    import javax.crypto.IllegalBlockSizeException;
    import javax.crypto.KeyGenerator;
    import javax.crypto.NoSuchPaddingException;
    import javax.crypto.SecretKey;
    //import java.util.Scanner;

    public class DES {

    public static void main(String[] args) {

    String strEnc = "01 02 03 04 05 06 0a 0b 0c 0d 0e 0f";

    try {

    KeyGenerator kg = KeyGenerator.getInstance("DES");
    SecretKey myKey = kg.generateKey();
    Cipher enc, des;
    enc = Cipher.getInstance("DES/ECB/PKCS5Padding");
    des = Cipher.getInstance("DES/ECB/PKCS5Padding");

    // initialize the ciphers with the given key
    enc.init(Cipher.ENCRYPT_MODE, myKey);
    des.init(Cipher.DECRYPT_MODE, myKey);

    //Scanner in = new Scanner(System.in);
    //System.out.println("Please enter your message: ");
    byte[] messageUtf8 = strEnc.getBytes("UTF8");

    System.out.println("Message [Byte Format} : " + messageUtf8);
    //System.out.println("Message: " + new String(messageUtf8));

    byte[] encMessage = enc.doFinal(messageUtf8);
    encMessage = BASE64EncoderStream.encode(encMessage);
    String encMsg = new String(encMessage);
    System.out.println("Encrypted Message: " + encMsg);

    // Decryption

    // decode with base64 to get bytes
    byte[] dec = BASE64DecoderStream.decode(encMsg.getBytes());

    byte[] decMessageUtf8 = des.doFinal(dec);

    String decMsg = new String(decMessageUtf8);   
    System.out.println("Decrypted Message: " +decMsg );

    } catch (NoSuchAlgorithmException e) {
    e.printStackTrace();
    } catch (NoSuchPaddingException e) {
    e.printStackTrace();
    } catch (InvalidKeyException e) {

    e.printStackTrace();
    } catch (BadPaddingException e) {

    e.printStackTrace();
    } catch (IllegalBlockSizeException e) {

    e.printStackTrace();
    } catch (UnsupportedEncodingException e) {

    e.printStackTrace();
    }

    }
    }
预期结果将为每种模式提供如下输出:

Message [Byte Format} : [B@7bb11784
Encrypted Message: dk2TnVfLHZf4p9o7fa00XjVaGenbiIItrCHhhA/UbjDcvf+E2Av5pw==
Decrypted Message: 01 02 03 04 05 06 0a 0b 0c 0d 0e 0f
但是,我在尝试更改模式时收到以下消息:

java.security.InvalidKeyException: Parameters missing
    at com.sun.crypto.provider.CipherCore.init(CipherCore.java:469)
    at com.sun.crypto.provider.DESCipher.engineInit(DESCipher.java:186)
    at javax.crypto.Cipher.implInit(Cipher.java:801)
    at javax.crypto.Cipher.chooseProvider(Cipher.java:863)
    at javax.crypto.Cipher.init(Cipher.java:1248)
    at javax.crypto.Cipher.init(Cipher.java:1185)
    at DES.main(DES.java:30)