对称字符串 图形输出

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std; 
const int inf=0x3f3f3f3f;

int main(){
	int n;
	vector<char> a[25];
	a[1].push_back('A');
	for(int i=2;i<=20;i++){
		for(int j=0;j<a[i-1].size();j++)
			a[i].push_back(a[i-1][j]);
		a[i].push_back('A'+i-1);
		for(int j=a[i-1].size()-1;j>=0;j--)
			a[i].push_back(a[i-1][j]); 
	}	
	scanf("%d",&n);
	for(int i=0;i<a[n].size();i++)
		printf("%c",a[n][i]);
	printf("\n");
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用Java实现DES对称加密算法的完整源代码,包括图形用户界面和加解密功能: ```java import java.awt.BorderLayout; import java.awt.Container; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.NoSuchPaddingException; import javax.crypto.SecretKey; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextArea; import javax.swing.JTextField; public class DESGUI extends JFrame implements ActionListener { private static final long serialVersionUID = 1L; private JTextField keyTextField; private JTextArea inputTextArea; private JTextArea outputTextArea; private SecretKey key; private Cipher encryptCipher; private Cipher decryptCipher; public DESGUI() { super("DES加解密工具"); setSize(600, 400); setDefaultCloseOperation(EXIT_ON_CLOSE); Container contentPane = getContentPane(); JPanel inputPanel = new JPanel(); JPanel buttonPanel = new JPanel(); JPanel outputPanel = new JPanel(); keyTextField = new JTextField(32); inputTextArea = new JTextArea(10, 40); outputTextArea = new JTextArea(10, 40); outputTextArea.setEditable(false); JButton generateButton = new JButton("生成密钥"); JButton encryptButton = new JButton("加密"); JButton decryptButton = new JButton("解密"); generateButton.addActionListener(this); encryptButton.addActionListener(this); decryptButton.addActionListener(this); inputPanel.add(keyTextField); inputPanel.add(inputTextArea); buttonPanel.add(generateButton); buttonPanel.add(encryptButton); buttonPanel.add(decryptButton); outputPanel.add(outputTextArea); contentPane.add(inputPanel, BorderLayout.NORTH); contentPane.add(buttonPanel, BorderLayout.CENTER); contentPane.add(outputPanel, BorderLayout.SOUTH); setVisible(true); } public static void main(String[] args) { new DESGUI(); } @Override public void actionPerformed(ActionEvent e) { try { if (e.getActionCommand().equals("生成密钥")) { generateKey(); } else if (e.getActionCommand().equals("加密")) { encrypt(); } else if (e.getActionCommand().equals("解密")) { decrypt(); } } catch (Exception ex) { ex.printStackTrace(); } } private void generateKey() throws NoSuchAlgorithmException { KeyGenerator keyGenerator = KeyGenerator.getInstance("DES"); key = keyGenerator.generateKey(); keyTextField.setText(bytesToHex(key.getEncoded())); } private void encrypt() throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException { if (key == null) { generateKey(); } if (encryptCipher == null) { encryptCipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); encryptCipher.init(Cipher.ENCRYPT_MODE, key); } byte[] input = inputTextArea.getText().getBytes(); byte[] output = encryptCipher.doFinal(input); outputTextArea.setText(bytesToHex(output)); } private void decrypt() throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException { if (key == null) { generateKey(); } if (decryptCipher == null) { decryptCipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); decryptCipher.init(Cipher.DECRYPT_MODE, key); } byte[] input = hexToBytes(inputTextArea.getText()); byte[] output = decryptCipher.doFinal(input); outputTextArea.setText(new String(output)); } private String bytesToHex(byte[] bytes) { StringBuilder sb = new StringBuilder(); for (byte b : bytes) { sb.append(String.format("%02X", b)); } return sb.toString(); } private byte[] hexToBytes(String hex) { byte[] bytes = new byte[hex.length() / 2]; for (int i = 0; i < bytes.length; i++) { bytes[i] = (byte) Integer.parseInt(hex.substring(i * 2, i * 2 + 2), 16); } return bytes; } } ``` 使用说明: 1. 运行程序,会弹出一个图形用户界面。 2. 在“密钥”文本框中输入密钥,或者点击“生成密钥”按钮随机生成密钥。 3. 在“输入”文本框中输入要加密或解密的字符串。 4. 点击“加密”按钮进行加密,或者点击“解密”按钮进行解密。 5. 加密或解密后的结果会显示在“输出”文本框中,同时也会以十六进制格式显示在“输入”文本框中,方便复制粘贴。 6. 可以重复进行加密或解密操作,不需要重新输入密钥。 7. 点击窗口右上角的关闭按钮可以退出程序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值