Java 下面这些方法的区别是什么?

Java 下面这些方法的区别是什么?,java,encryption,cryptography,Java,Encryption,Cryptography,我正在学习java中的加密,并使用了这两个教程 及 当我尝试从这些示例代码中加密hello时,两个示例代码都给出了不同的加密字符串,我已经尝试了很多,以发现这些示例之间的主要区别是什么,导致了相同密码的不同加密字符串,因为这两个示例都使用AES 代码2中的代码学习 package nomad; import java.security.*; import java.security.spec.InvalidKeySpecException; import javax.crypto.*; imp

我正在学习java中的加密,并使用了这两个教程

当我尝试从这些示例代码中加密hello时,两个示例代码都给出了不同的加密字符串,我已经尝试了很多,以发现这些示例之间的主要区别是什么,导致了相同密码的不同加密字符串,因为这两个示例都使用AES

代码2中的代码学习

package nomad;

import java.security.*;
import java.security.spec.InvalidKeySpecException;
import javax.crypto.*;
import sun.misc.*;

public class AESencrp {

     private static final String ALGO = "AES";
    private static final byte[] keyValue = 
        new byte[] { 'T', 'h', 'e', 'B', 'e', 's', 't',
'S', 'e', 'c', 'r','e', 't', 'K', 'e', 'y' };


public static String encrypt(String Data) throws Exception {
        Key key = generateKey();
        Cipher c = Cipher.getInstance(ALGO);
        c.init(Cipher.ENCRYPT_MODE, key);
        byte[] encVal = c.doFinal(Data.getBytes());
        String encryptedValue = new BASE64Encoder().encode(encVal);
        return encryptedValue;
    }

    public static String decrypt(String encryptedData) throws Exception {
        Key key = generateKey();
        Cipher c = Cipher.getInstance(ALGO);
        c.init(Cipher.DECRYPT_MODE, key);
        byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedData);
        byte[] decValue = c.doFinal(decordedValue);
        String decryptedValue = new String(decValue);
        return decryptedValue;
    }
    private static Key generateKey() throws Exception {
        Key key = new SecretKeySpec(keyValue, ALGO);
        return key;
}

}


package nomad;

public class Checker {

    public static void main(String[] args) throws Exception {

        String password = "mypassword";
        String passwordEnc = AESencrp.encrypt(password);
        String passwordDec = AESencrp.decrypt(passwordEnc);

        System.out.println("Plain Text : " + password);
        System.out.println("Encrypted Text : " + passwordEnc);
        System.out.println("Decrypted Text : " + passwordDec);
    }
}
在java中如何操作的代码

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Base64;

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

public class AES {

    private static SecretKeySpec secretKey;
    private static byte[] key;

    public static void setKey(String myKey) 
    {
        MessageDigest sha = null;
        try {
            key = myKey.getBytes("UTF-8");
            sha = MessageDigest.getInstance("SHA-1");
            key = sha.digest(key);
            key = Arrays.copyOf(key, 16); 
            secretKey = new SecretKeySpec(key, "AES");
        } 
        catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } 
        catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    public static String encrypt(String strToEncrypt, String secret) 
    {
        try
        {
            setKey(secret);
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, secretKey);
            return Base64.getEncoder().encodeToString(cipher.doFinal(strToEncrypt.getBytes("UTF-8")));
        } 
        catch (Exception e) 
        {
            System.out.println("Error while encrypting: " + e.toString());
        }
        return null;
    }

    public static String decrypt(String strToDecrypt, String secret) 
    {
        try
        {
            setKey(secret);
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
            cipher.init(Cipher.DECRYPT_MODE, secretKey);
            return new String(cipher.doFinal(Base64.getDecoder().decode(strToDecrypt)));
        } 
        catch (Exception e) 
        {
            System.out.println("Error while decrypting: " + e.toString());
        }
        return null;
    }
}


public static void main(String[] args) 
{
    final String secretKey = "ssshhhhhhhhhhh!!!!";

    String originalString = "howtodoinjava.com";
    String encryptedString = AES.encrypt(originalString, secretKey) ;
    String decryptedString = AES.decrypt(encryptedString, secretKey) ;

    System.out.println(originalString);
    System.out.println(encryptedString);
    System.out.println(decryptedString);
}
我添加了代码,因为其中一条评论说,外部链接不是每个人都可以访问的

请记住,secretkey和passowrd在我的代码中是相同的

添加我自己的代码

将其命名为代码1

AES类

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.security.MessageDigest;
import java.util.Arrays;
import java.util.Base64;

public class AES {


    public static SecretKeySpec secretKeySpec;
    private static byte[] key;


    public static String setKey(String myKey) throws Exception {


        MessageDigest sha=null;

        key =myKey.getBytes("UTF-8");
        sha=MessageDigest.getInstance("SHA-1");
        key=sha.digest(key);
        key= Arrays.copyOf(key,16);

        secretKeySpec=new SecretKeySpec(key,"AES");
        return myKey;
    }

    public static String encrypt(String strToencrypt, String secret) throws Exception {

        String s=  setKey(secret);
        System.out.println(s);

        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE,secretKeySpec);
        return Base64.getEncoder().encodeToString(cipher.doFinal(strToencrypt.getBytes("UTF-8")));
    }


    public static  String decryt(String strToDec , String secret) throws Exception {
        setKey(secret);
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE,secretKeySpec);
        return  new String(cipher.doFinal(Base64.getDecoder().decode(strToDec)));
    }

}
public class Main {





    public static void main(String[] args) throws Exception {


        AES aes = new AES();

        String ss=null;
        String sd=null;
        ss=aes.encrypt("hello","TheBestSecretKey");
        sd=aes.decryt(ss,"TheBestSecretKey");

        System.out.println("The Encrypted == " + ss);
        System.out.println("The Decrypted == " + sd);



    }


}
以上AES类的主类

public class Main {





    public static void main(String[] args) throws Exception {


        AES aes = new AES();

        String ss=null;
        String sd=null;
        ss=aes.encrypt("hello","TheBestSecretKey");
        sd=aes.decryt(ss,"TheBestSecretKey");

        System.out.println("The Encrypted == " + ss);
        System.out.println("The Decrypted == " + sd);



    }


}
另一个代码

将其命名为代码2

package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Base64;

public class Main  {


    private static final String ALGO ="AES";
    private static String string  = "TheBestSecretKey";
    private static  byte[] key ;
    private static SecretKeySpec secretKeySpec;



    public static String  aesEncrypt(String en) throws Exception {

        Key key = new SecretKeySpec(string.getBytes(),ALGO);
        Cipher c = Cipher.getInstance("AES/ECB/PKCS5Padding");
        c.init(Cipher.ENCRYPT_MODE,key);
        byte[] encValue =c.doFinal(en.getBytes("UTF-8"));
        String encryptedValue= new BASE64Encoder().encode(encValue);
return encryptedValue;

    }

    public static String aesDecrypt(String De) throws Exception{

        Key key = new SecretKeySpec(string.getBytes(),"AES");

        Cipher c = Cipher.getInstance("AES/ECB/PKCS5Padding");
        c.init(Cipher.DECRYPT_MODE,key);
       // return  new String(c.doFinal(Base64.getDecoder().decode(De)));

        byte[]decodedVlue=new BASE64Decoder().decodeBuffer(De);
        byte[] decValue = c.doFinal(decodedVlue);
        String deccryptedValue = new String(decValue);
        return deccryptedValue;

    }

    public static void main(String[] args) throws Exception {

        String password = "hello";
        String passEnc= Main.aesEncrypt(password);
        System.out.println(passEnc);
        String passDec = Main.aesDecrypt(passEnc);
        System.out.println(passDec);

    }
}
这是密码 现在我也发布加密字符串

使用SecretSpecKey、bestsecretkey和hello作为要加密的字符串

代码1 UlQiIs/K0EwcSKbWMjcT1g==


代码2 hHDBo1dJYj5RcjdFA6BBfw==

请在此处发布相关代码,而不是隐藏在链接后面。尽量让你的问题更具体。。。。“请帮帮我”太含糊了。你确定你对这两个密码都使用了相同的密钥吗?请发布一个你的问题。外部链接不是每个人(包括我)都可以访问的。他们不是都使用相同的“密码”,不管你是指密钥还是要加密的内容。顶部代码使用“BestSecretKey”加密“mypassword”;底部代码使用“ssshhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh。所以,他们会给出不同的答案,因为他们有不同的输入。“代码”。但这远远不是一个最小的、完整的和可验证的示例,而且它甚至看起来不像是您的代码,因为您刚刚评论了代码中的内容是如何不同的。如果你不发布自己的代码,我们该如何帮助你解决这个问题?在某种程度上,我们可以测试它来重现错误?