CBC模式下的DES必须被捕获或引发错误

问题描述 投票:0回答:1

我试图弄清楚为什么我的cbc模式的DES代码无法正常工作。我收到一条错误消息:错误:未报告的异常NoSuchProviderException;必须被抓住或宣布被抛出desCipher = Cipher.getInstance(“ DES / CBC / PKCS5Padding”,“ CB”);

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;

public class JEncrytion
{    
    public static void main(String[] argv) {

        try{

        KeyGenerator keygenerator = KeyGenerator.getInstance("DES");
        SecretKey myDesKey = keygenerator.generateKey();

        Cipher desCipher;

        // Create the cipher 
        desCipher = Cipher.getInstance("DES/CBC/PKCS5Padding", "CB");

        // Initialize the cipher for encryption
        desCipher.init(Cipher.ENCRYPT_MODE, myDesKey);

        //sensitive information
        byte[] text = "hello world how are you".getBytes();

        System.out.println("Text [Byte Format] : " + text);
        System.out.println("Text : " + new String(text));

        // Encrypt the text
        byte[] textEncrypted = desCipher.doFinal(text);

        System.out.println("Text Encryted : " + textEncrypted);

        // Initialize the same cipher for decryption
        desCipher.init(Cipher.DECRYPT_MODE, myDesKey);

        // Decrypt the text
        byte[] textDecrypted = desCipher.doFinal(textEncrypted);

        System.out.println("Text Decryted : " + new 
String(textDecrypted));

    }catch(NoSuchAlgorithmException e){
        e.printStackTrace();
    }catch(NoSuchPaddingException e){
        e.printStackTrace();
    }catch(InvalidKeyException e){
        e.printStackTrace();
    }catch(IllegalBlockSizeException e){
        e.printStackTrace();
    }catch(BadPaddingException e){
        e.printStackTrace();
    } 
}
}
java des cbc-mode
1个回答
0
投票

按照消息的内容-声明异常:

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

在您的情况下,一般例外就足够了。

© www.soinside.com 2019 - 2024. All rights reserved.