Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/377.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在这里如何解密消息加密?_Java_Encryption - Fatal编程技术网

Java 在这里如何解密消息加密?

Java 在这里如何解密消息加密?,java,encryption,Java,Encryption,我也有解密代码,但我想用密文的输出对消息进行解密 eg;我的密文输出是uSG1OxJPywzU4JylpqgS6SoB9t21GZ4iN3bY2M6Qf10= 我想解密这个:uSG1OxJPywzU4JylpqgS6SoB9t21GZ4iN3bY2M6Qf10= 提前谢谢。试试这个 try{ String plainData="my name is laksahan",cipherText,decryptedText; KeyGenerator keyGen = KeyGener

我也有解密代码,但我想用密文的输出对消息进行解密

eg;我的密文输出是uSG1OxJPywzU4JylpqgS6SoB9t21GZ4iN3bY2M6Qf10=

我想解密这个:
uSG1OxJPywzU4JylpqgS6SoB9t21GZ4iN3bY2M6Qf10=

提前谢谢。

试试这个

try{
    String plainData="my name is laksahan",cipherText,decryptedText;
    KeyGenerator keyGen = KeyGenerator.getInstance("AES");
    keyGen.init(128);
    SecretKey secretKey = keyGen.generateKey();
    Cipher aesCipher = Cipher.getInstance("AES");
    aesCipher.init(Cipher.ENCRYPT_MODE,secretKey);
    byte[] byteDataToEncrypt = plainData.getBytes();
    byte[] byteCipherText = aesCipher.doFinal(byteDataToEncrypt);
    cipherText = new BASE64Encoder().encode(byteCipherText);
System.out.println(cipherText);
}catch(Exception e){

}
试试这个

try{
    String plainData="my name is laksahan",cipherText,decryptedText;
    KeyGenerator keyGen = KeyGenerator.getInstance("AES");
    keyGen.init(128);
    SecretKey secretKey = keyGen.generateKey();
    Cipher aesCipher = Cipher.getInstance("AES");
    aesCipher.init(Cipher.ENCRYPT_MODE,secretKey);
    byte[] byteDataToEncrypt = plainData.getBytes();
    byte[] byteCipherText = aesCipher.doFinal(byteDataToEncrypt);
    cipherText = new BASE64Encoder().encode(byteCipherText);
System.out.println(cipherText);
}catch(Exception e){

}

试试这个代码,它在我的电脑上运行良好。祝你好运

 byte[] data = new BASE64Decoder().decodeBuffer(cipherData);
        Cipher aesCipher = Cipher.getInstance("AES");
        aesCipher.init(Cipher.DECRYPT_MODE, secretKeyUsed while encrypting);
        byte[] plainData = aesCipher.doFinal(data);
        return new String(plainData);

如果要使用客户密钥,请尝试以下代码,只需记住密钥长度为128位。 顺便说一下,我更喜欢将密钥存储在密钥库文件中

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import sun.misc.BASE64Encoder;
import sun.misc.BASE64Decoder;

public class AESExample
{
    public static void main(String[] args)
    {
        try
        {
            String plainData = "my name is laksahan", cipherText, decryptedText;
            KeyGenerator keyGen = KeyGenerator.getInstance("AES");
            keyGen.init(128);
            SecretKey secretKey = keyGen.generateKey();
            cipherText = encrypt(plainData, secretKey);
            System.out.println(cipherText);
            decryptedText = decrypt(cipherText, secretKey);
            System.out.println(decryptedText);
        } catch (Exception e)
        {
            e.printStackTrace();
        }

    }

    public static String encrypt(String plainData, SecretKey secretKey) throws Exception
    {
        Cipher aesCipher = Cipher.getInstance("AES");
        aesCipher.init(Cipher.ENCRYPT_MODE, secretKey);
        byte[] byteDataToEncrypt = plainData.getBytes();
        byte[] byteCipherText = aesCipher.doFinal(byteDataToEncrypt);
        return new BASE64Encoder().encode(byteCipherText);
    }

    public static String decrypt(String cipherData, SecretKey secretKey) throws Exception
    {
        byte[] data = new BASE64Decoder().decodeBuffer(cipherData);
        Cipher aesCipher = Cipher.getInstance("AES");
        aesCipher.init(Cipher.DECRYPT_MODE, secretKey);
        byte[] plainData = aesCipher.doFinal(data);
        return new String(plainData);
    }

}

试试这个代码,它在我的电脑上运行良好。祝你好运

 byte[] data = new BASE64Decoder().decodeBuffer(cipherData);
        Cipher aesCipher = Cipher.getInstance("AES");
        aesCipher.init(Cipher.DECRYPT_MODE, secretKeyUsed while encrypting);
        byte[] plainData = aesCipher.doFinal(data);
        return new String(plainData);

如果要使用客户密钥,请尝试以下代码,只需记住密钥长度为128位。 顺便说一下,我更喜欢将密钥存储在密钥库文件中

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import sun.misc.BASE64Encoder;
import sun.misc.BASE64Decoder;

public class AESExample
{
    public static void main(String[] args)
    {
        try
        {
            String plainData = "my name is laksahan", cipherText, decryptedText;
            KeyGenerator keyGen = KeyGenerator.getInstance("AES");
            keyGen.init(128);
            SecretKey secretKey = keyGen.generateKey();
            cipherText = encrypt(plainData, secretKey);
            System.out.println(cipherText);
            decryptedText = decrypt(cipherText, secretKey);
            System.out.println(decryptedText);
        } catch (Exception e)
        {
            e.printStackTrace();
        }

    }

    public static String encrypt(String plainData, SecretKey secretKey) throws Exception
    {
        Cipher aesCipher = Cipher.getInstance("AES");
        aesCipher.init(Cipher.ENCRYPT_MODE, secretKey);
        byte[] byteDataToEncrypt = plainData.getBytes();
        byte[] byteCipherText = aesCipher.doFinal(byteDataToEncrypt);
        return new BASE64Encoder().encode(byteCipherText);
    }

    public static String decrypt(String cipherData, SecretKey secretKey) throws Exception
    {
        byte[] data = new BASE64Decoder().decodeBuffer(cipherData);
        Cipher aesCipher = Cipher.getInstance("AES");
        aesCipher.init(Cipher.DECRYPT_MODE, secretKey);
        byte[] plainData = aesCipher.doFinal(data);
        return new String(plainData);
    }

}

好的-现在我可以阅读代码了(请学习如何使用代码格式)。对于初学者,将
catch(异常e){}
更改为
catch(异常e){e.printStckTrace();}
OK-现在我可以阅读代码了(请学习如何使用代码格式)。对于初学者,将
catch(异常e){}
更改为
catch(异常e){e.printStckTrace();}
代替encryptedText放置加密密码找不到符号secretKey、cipherText、, encBytes@user2136160您需要在此处指定您的值实际上我们使用的是Base64,但您使用的是Base64编码器。u Base64代替encryptedText是否可以放置加密密码找不到符号secretKey、cipherText、, encBytes@user2136160您需要在此处指定您的值实际上我们使用的是Base64,但您使用的是Base64编码器。u Base64可以吗?这很有用,但我想提供加密密码并对其解密。如果您能提供一个带有示例的代码,那么me@user2136160你说的“提供加密密码并解密”是什么意思?你说的是将密码存储在某个地方并使用密码加密或解密数据吗?我用代码更改了答案这很有帮助,但我想提供我的加密密码并解密它。如果您能提供一个带有示例的代码,那么me@user2136160你说的“提供加密密码并解密”是什么意思?你说的是将密码存储在某个地方并使用密码加密或解密数据吗?我用密码改变了答案