首页
学习
活动
专区
圈层
工具
发布

加解密
EN

Stack Overflow用户
提问于 2015-02-11 02:54:47
回答 1查看 41关注 0票数 0

我正在尝试创建一个循环,所以这个程序将继续,直到用户要求退出,但我知道我需要使用do-while循环,但由于某些原因,它不能工作,我如何能够修复这个问题。我希望用户按下像E(退出)和程序将停止。

代码语言:javascript
复制
import java.util.Scanner;
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 javax.crypto.SecretKey;
/**

 */
public class ENCRY
{
    public static void main(String[] args) 
    {

        try{
            do{
                Scanner input = new Scanner(System.in);
                System.out.println("Please select an Option");
                System.out.println("Encrypt the Sentence");
                int File =input.nextInt();
                System.out.println("--------------------");
                System.out.print("Enter the sentence:");
                String s = input.next();

                KeyGenerator keygenerator = KeyGenerator.getInstance("DES");
                SecretKey myDesKey = keygenerator.generateKey();
                Cipher desCipher;
                desCipher = Cipher.getInstance("DES");
                desCipher.init(Cipher.ENCRYPT_MODE, myDesKey);
                byte[] text = s.getBytes();
                System.out.println("" + new String(text));


                byte[] textEncrypted = desCipher.doFinal(text);
                System.out.println("sentence Encryted : " + textEncrypted);
                desCipher.init(Cipher.DECRYPT_MODE, myDesKey);

                byte[] textDecrypted = desCipher.doFinal(textEncrypted);
                System.out.println("sentence Decryted : " +  new String(textDecrypted));



while()



        }catch(NoSuchAlgorithmException S)
        {
            S.printStackTrace();
        }catch(NoSuchPaddingException S)
        {
            S.printStackTrace();
        }catch(InvalidKeyException S)
        {
            S.printStackTrace();
        }catch(IllegalBlockSizeException S)
        {
            S.printStackTrace();
        }catch(BadPaddingException S)
        {
            S.printStackTrace();
        } 

    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-02-11 03:06:18

您可以做的事情如下所示

代码语言:javascript
复制
do{ 
 //code you want to run
}while(!stop);

在关键事件时,停止改为真(如。用户按下"e“键)或可能按JButton。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28445792

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档