Java中的DES加密误差(非法基本64字符20)

发布于 2025-02-03 02:48:04 字数 3234 浏览 2 评论 0原文

我正在尝试为我的APCS-A最终项目制作一些加密/解密软件。我的大部分内容都使用了各种文本密码等,但是我试图使DES加密像大事一样工作。我无法在主要程序中使用它,因此我创建了自己的小东西来尝试先学习和测试。我认为我已经走了很远,但是我陷入了一个我不知道的错误。

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.spec.*;
import java.util.Base64;
public class BigETest
{
    public static void main(String[]args) throws NoSuchAlgorithmException,NoSuchPaddingException,InvalidKeyException,IllegalBlockSizeException,BadPaddingException
    {
        Scanner kbd = new Scanner(System.in);
        
        System.out.print("Enter text: ");
        String t = kbd.nextLine();
        String[] rets = en(t);
        System.out.println("Encrypted: "+ rets[0]);
        System.out.println("Key: " + rets[1]);
        String dec = de(rets[0],rets[1]);
        System.out.println("Decrypted: "+dec);
    }
    public static String[] en(String inText) throws BadPaddingException,IllegalBlockSizeException,InvalidKeyException,NoSuchPaddingException,NoSuchAlgorithmException
    {
        KeyGenerator keyGen = KeyGenerator.getInstance("DES");
        SecretKey key = keyGen.generateKey();
        
        Cipher cipher = Cipher.getInstance("DES");
        
        cipher.init(Cipher.ENCRYPT_MODE, key);
        byte[] text = Base64.getUrlDecoder().decode(inText);
        byte[] encryptedBytes = cipher.doFinal(text);
        String encrypted = new String(Base64.getUrlEncoder().encodeToString(encryptedBytes));
        
        byte[] rawKey = key.getEncoded();
        String stringKey = new String(Base64.getUrlEncoder().encodeToString(rawKey));
        
        String[] ret = {encrypted,stringKey};
        return ret;
    }
    public static String de(String inText, String inKey) throws BadPaddingException,IllegalBlockSizeException,InvalidKeyException,NoSuchPaddingException,NoSuchAlgorithmException
    {
        byte[] keyB = Base64.getUrlDecoder().decode(inKey);
        SecretKey key = new SecretKeySpec(keyB, "DES");
        Cipher cipher = Cipher.getInstance("DES");
        cipher.init(Cipher.DECRYPT_MODE, key);
        byte[] text = Base64.getUrlDecoder().decode(inText);
        byte[] fin = cipher.doFinal(text);
        String dec = new String(Base64.getUrlEncoder().encodeToString(fin));
        return dec;
    }
}

很短并且没有空间,

Enter text: test
Encrypted: BOM9tFOUfWc=
Key: bq2ooSbc1n8=
Decrypted: test

输入

Enter text: BigTest
Encrypted: jme0nIAHXyA=
Key: 90UxVwetj2g=
Decrypted: BigTess=

Enter text: bigger test

java.lang.IllegalArgumentException: Illegal base64 character 20
    at java.base/java.util.Base64$Decoder.decode0(Base64.java:746)
    at java.base/java.util.Base64$Decoder.decode(Base64.java:538)
    at java.base/java.util.Base64$Decoder.decode(Base64.java:561)
    at BigETest.en(BigETest.java:34)
    at BigETest.main(BigETest.java:20)

如果 该代码有时可以起作用就在Google上的任何地方,我可以找到一些答案,但似乎没有任何作用。任何帮助将不胜感激。

I am trying to make a little encryption/decryption software for my APCS-A final project. I have most of it done with various text ciphers and such, but I am trying to get DES encryption to work as like the big thing. I couldn't get it working in the main program so I created its own little thing to try to learn and test it first. I think I've gotten decently far, but I am getting stuck on an error that I can't figure out.

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.spec.*;
import java.util.Base64;
public class BigETest
{
    public static void main(String[]args) throws NoSuchAlgorithmException,NoSuchPaddingException,InvalidKeyException,IllegalBlockSizeException,BadPaddingException
    {
        Scanner kbd = new Scanner(System.in);
        
        System.out.print("Enter text: ");
        String t = kbd.nextLine();
        String[] rets = en(t);
        System.out.println("Encrypted: "+ rets[0]);
        System.out.println("Key: " + rets[1]);
        String dec = de(rets[0],rets[1]);
        System.out.println("Decrypted: "+dec);
    }
    public static String[] en(String inText) throws BadPaddingException,IllegalBlockSizeException,InvalidKeyException,NoSuchPaddingException,NoSuchAlgorithmException
    {
        KeyGenerator keyGen = KeyGenerator.getInstance("DES");
        SecretKey key = keyGen.generateKey();
        
        Cipher cipher = Cipher.getInstance("DES");
        
        cipher.init(Cipher.ENCRYPT_MODE, key);
        byte[] text = Base64.getUrlDecoder().decode(inText);
        byte[] encryptedBytes = cipher.doFinal(text);
        String encrypted = new String(Base64.getUrlEncoder().encodeToString(encryptedBytes));
        
        byte[] rawKey = key.getEncoded();
        String stringKey = new String(Base64.getUrlEncoder().encodeToString(rawKey));
        
        String[] ret = {encrypted,stringKey};
        return ret;
    }
    public static String de(String inText, String inKey) throws BadPaddingException,IllegalBlockSizeException,InvalidKeyException,NoSuchPaddingException,NoSuchAlgorithmException
    {
        byte[] keyB = Base64.getUrlDecoder().decode(inKey);
        SecretKey key = new SecretKeySpec(keyB, "DES");
        Cipher cipher = Cipher.getInstance("DES");
        cipher.init(Cipher.DECRYPT_MODE, key);
        byte[] text = Base64.getUrlDecoder().decode(inText);
        byte[] fin = cipher.doFinal(text);
        String dec = new String(Base64.getUrlEncoder().encodeToString(fin));
        return dec;
    }
}

The code works sometimes if the input is short and doesn't have spaces in it

Enter text: test
Encrypted: BOM9tFOUfWc=
Key: bq2ooSbc1n8=
Decrypted: test

but falls apart if the input is a bit longer

Enter text: BigTest
Encrypted: jme0nIAHXyA=
Key: 90UxVwetj2g=
Decrypted: BigTess=

And then gives me an error if there are any spaces

Enter text: bigger test

java.lang.IllegalArgumentException: Illegal base64 character 20
    at java.base/java.util.Base64$Decoder.decode0(Base64.java:746)
    at java.base/java.util.Base64$Decoder.decode(Base64.java:538)
    at java.base/java.util.Base64$Decoder.decode(Base64.java:561)
    at BigETest.en(BigETest.java:34)
    at BigETest.main(BigETest.java:20)

I have tried changing things up and looked around both on here and just around anyplace I could find on Google for some answers, but nothing seems to be working. Any help would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文