【问题标题】:Encryption using AES使用 AES 加密
【发布时间】:2016-03-11 19:41:36
【问题描述】:

我正在使用 AES 加密字符串,但它没有将“1234567812345678”字符从加密字符串解密回纯文本。对于其他文本(如“Hello World”),它工作正常。还希望保持加密代码只接受 UTF-8 字符。我正在使用以下代码

import java.security.*;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
public class StrongAES 
{

    public static void main(String[] args) 
    {
         //listing all available cryptographic algorithms 

        /* for (Provider provider: Security.getProviders()) {
              System.out.println(provider.getName());
              for (String key: provider.stringPropertyNames())
                System.out.println("\t" + key + "\t" + provider.getProperty(key));
            }*/
        StrongAES saes = new StrongAES();
        String encrypt = saes.encrypt(new String("Bar12346Bar12346"),new String("1234567812345678"));
        //String encrypt = saes.encrypt(new String("Bar12346Bar12346"),new String("Hello world"));
        System.out.println(encrypt);
        String decrypt = saes.decrypt(new String("Bar12346Bar12346"), new String(encrypt));
        System.out.println(decrypt);
    }


    String encrypt(String key, String text) 
    {
        String encryptedText="";
        try{
         // Create key and cipher
        Key aesKey = new SecretKeySpec(key.getBytes(), "AES");
        Cipher cipher = Cipher.getInstance("AES");


        // encrypt the text
        cipher.init(Cipher.ENCRYPT_MODE, aesKey);
        byte[] encrypted = cipher.doFinal(text.getBytes());
        encryptedText = new String(encrypted);
       // System.out.println(encryptedText);
        }
        catch(Exception e) 
        {
            e.printStackTrace();
        }
        return encryptedText;

    }

    String decrypt(String key, String encryptedText)
    {
        String decryptedText="";
        try{
         // Create key and cipher
        Key aesKey = new SecretKeySpec(key.getBytes(), "AES");
        Cipher cipher = Cipher.getInstance("AES");

        // decrypt the text
        cipher.init(Cipher.DECRYPT_MODE, aesKey);
        decryptedText = new String(cipher.doFinal(encryptedText.getBytes()));
       // System.out.println("Decrypted   "+decryptedText);

        }
        catch(Exception e) 
        {
            e.printStackTrace();
        }
        return decryptedText;

    }

}

【问题讨论】:

标签: java aes


【解决方案1】:

您必须保留字节,而不是转换为字符串。

这篇文章的解释: Problems converting byte array to string and back to byte array

如果你想保留一个字符串,考虑转换为 B64

String my_string=DatatypeConverter.printBase64Binary(byte_array);

byte [] byteArray=DatatypeConverter.parseBase64Binary(my_string);

您也可以转换为 Hexa(更大)

【讨论】:

    【解决方案2】:

    我已将代码更改为以下内容。在解密时使用 BASE64Encoder 将加密的字节转换为与 BASE64Decoder 相同的字符串。每个 getByte 都使用 UTF-8。

    import java.security.*;
    
    import javax.crypto.BadPaddingException;
    import javax.crypto.Cipher;
    import javax.crypto.IllegalBlockSizeException;
    import javax.crypto.NoSuchPaddingException;
    import javax.crypto.spec.SecretKeySpec;
    
    import sun.misc.BASE64Decoder;
    import sun.misc.BASE64Encoder;
    public class StrongAES 
    {
    
        public static void main(String[] args) 
        {
            StrongAES saes = new StrongAES();
            String encrypt = saes.encrypt(new String("Bar12346Bar12346"),new String("1234567812345678"));
            System.out.println(encrypt);
            String decrypt = saes.decrypt(new String("Bar12346Bar12346"), new String(encrypt));
            System.out.println(decrypt);
        }
    
    
        String encrypt(String key, String text) 
        {
            String encryptedText="";
            try{
             // Create key and cipher
            Key aesKey = new SecretKeySpec(key.getBytes("utf-8"), "AES");
            Cipher cipher = Cipher.getInstance("AES");
            // encrypt the text
            cipher.init(Cipher.ENCRYPT_MODE, aesKey);
            byte[] encrypted = cipher.doFinal(text.getBytes("utf-8"));
            BASE64Encoder encoder = new BASE64Encoder();
            encryptedText = encoder.encodeBuffer(encrypted);
            }
            catch(Exception e) 
            {
                e.printStackTrace();
            }
            return encryptedText;
    
        }
    
        String decrypt(String key, String encryptedText)
        {
            String decryptedText="";
            try{
             // Create key and cipher
            Key aesKey = new SecretKeySpec(key.getBytes("utf-8"), "AES");
            Cipher cipher = Cipher.getInstance("AES");
            // decrypt the text
            cipher.init(Cipher.DECRYPT_MODE, aesKey);
            BASE64Decoder decoder = new BASE64Decoder();
            decryptedText = new String(cipher.doFinal(decoder.decodeBuffer(encryptedText)));
            }
            catch(Exception e) 
            {
                e.printStackTrace();
            }
            return decryptedText;
    
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-08-12
      • 2013-08-11
      • 2014-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多