密文和明文的转换

在这里插入图片描述

#include <stdio.h>
#include <math.h>
int main()
{
   int i,t,m,n;
   printf("原文转密文请按1,密文转原文请按0\n");
   scanf("%d",&t);
   if(t==1)
    {
   char c[200];
   printf("请输入明文:");
   scanf("%s",&c);
   m=strlen(c);
   for(i=0;i<m;i++)
   {
       if((c[i]>=65 && c[i]<=90) || c[i]<=118 && c[i]>=97)
        {
            c[i]=c[i]+4;
        }
        else if(c[i]>118)
        {
            c[i] = c[i]-54;
        }
   }
    printf("密文为:%s",c);
    getchar();
   }
   if(t==0)
    {
    char a[200];
    printf("请输入密文:",a);
    scanf("%s",&a);
    n=strlen(a);
    for(i=0;i<n;i++)
    {
        if(a[i]>=65 && a[i]<=68)
        {
            a[i]=a[i]+54;
        }
        else if((a[i]>=69 &&a[i]<=90 ) || (a[i]>=101 && a[i]<=122))
        {
            a[i]=a[i]-4;
        }
   else if(a[i]<=100 && a[i]>=97)
   {
       a[i]=a[i]-10;
   }
    }
    printf("明文为:%s",a);
   }
}

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,这是一个很好的编程问题。以下是一个简单的 Java 类,它可以根据用户 ID 生成密钥,将明文转换密文密文转换明文: ``` import java.security.*; import javax.crypto.*; import javax.crypto.spec.*; public class CryptoUtils { private static final String ALGORITHM = "AES"; private static final String TRANSFORMATION = "AES/ECB/PKCS5Padding"; public static SecretKey generateKey(String userId) throws Exception { KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM); SecureRandom secureRandom = new SecureRandom(userId.getBytes()); keyGenerator.init(128, secureRandom); return keyGenerator.generateKey(); } public static byte[] encrypt(SecretKey key, byte[] input) throws Exception { Cipher cipher = Cipher.getInstance(TRANSFORMATION); cipher.init(Cipher.ENCRYPT_MODE, key); return cipher.doFinal(input); } public static byte[] decrypt(SecretKey key, byte[] input) throws Exception { Cipher cipher = Cipher.getInstance(TRANSFORMATION); cipher.init(Cipher.DECRYPT_MODE, key); return cipher.doFinal(input); } } ``` 这个类使用 AES 算法进行加密和解密。它包括三个方法: - `generateKey`:根据用户 ID 生成一个密钥。 - `encrypt`:将明文转换密文。 - `decrypt`:将密文转换明文。 你可以使用这个类来加密和解密数据。例如,下面的代码演示了如何使用它: ``` String userId = "123456"; String plaintext = "Hello, world!"; SecretKey key = CryptoUtils.generateKey(userId); byte[] ciphertext = CryptoUtils.encrypt(key, plaintext.getBytes()); byte[] decrypted = CryptoUtils.decrypt(key, ciphertext); String decryptedText = new String(decrypted); System.out.println(decryptedText); ``` 这个代码片段将输出 "Hello, world!"。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值