这些方法有什么区别?

时间:2017-04-24 07:01:36

标签: java encryption cryptography

我正在学习java加密,并使用了这两个教程This from Code2learn

This from howtodoinjava.com

当我尝试从这些示例代码加密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);
}

我添加了代码,其中一条评论说每个人都无法访问外部链接。

请记住,在我的代码中,secretkeys和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)));
    }

}

以上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 TheBestSecretKey和hello作为要加密的字符串

代码1 UlQiIs / K0EwcSKbWMjcT1g ==

代码2 hHDBo1dJYj5RcjdFA6BBfw ==

0 个答案:

没有答案