在这里如何解密邮件加密? [英] How do I decrypt message encrypt in here?

查看:118
本文介绍了在这里如何解密邮件加密?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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){

}

I也有解密代码,但是我想使用cipherText的输出解密消息。

I also have the decrypting code, but I want to decrypt the message using the output of cipherText.

eg;我的密文输出是 uSG1OxJPywzU4JylpqgS6SoB9t21GZ4iN3bY2M6Qf10 =

eg; my ciphertext output is uSG1OxJPywzU4JylpqgS6SoB9t21GZ4iN3bY2M6Qf10=.

我想解密这个: uSG1OxJPywzU4JylpqgS6SoB9t21GZ4iN3bY2M6Qf10 = / code>

I want to decrypt this: uSG1OxJPywzU4JylpqgS6SoB9t21GZ4iN3bY2M6Qf10=

提前感谢

推荐答案

尝试这个代码,它在我的pc.Good运气很好!

Try this code,it works fine on my pc.Good luck!

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);
    }

}






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


If you want to use a customer key,try the following code,just remember the key length is 128 bit. By the way ,I prefer to store my key in keystore file!

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

import sun.misc.BASE64Encoder;
import sun.misc.BASE64Decoder;

public class AESExample
{

    public static void main(String[] args)
    {
        try
        {
            byte[]key={-4, -14, 106, -75, -9, 65, -95, 77, -52, 73, -87, -101, 80, 94, -59, -66};
            String plainData = "my name is laksahan", cipherText, decryptedText;
            System.out.println(key.length);
            cipherText = encrypt(plainData, key);
            System.out.println(cipherText);
            decryptedText = decrypt(cipherText, key);
            System.out.println(decryptedText);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }

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

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

}

这篇关于在这里如何解密邮件加密?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆