【问题标题】:AES encryption troublesAES 加密问题
【发布时间】:2012-01-18 00:54:06
【问题描述】:

我正在做一个项目,我需要使用 AES 加密一个字符串。程序需要能够接收一个字符串并输出一个十六进制的加密字符串以及一个密钥,或者,使用用户指定的密钥和字符串,输出未加密的文本(也就是说,程序需要能够在不同的情况下做这两件事,即我应该能够在我的机器上输入“1234”并输出“Encrypted text: asdf Key: ghjk”;我的朋友应该能够输入“Encrypted text: asdf KEY” :ghjk”,然后退出“1234”)

以下是我目前所拥有的:

package betterencryption;   

import javax.crypto.*;
import javax.crypto.spec.*;
import java.util.Scanner;

 public class BetterEncryption {

 public static String asHex (byte buf[]) {               //asHex works just fine, it's the main that's
                                                         //giving me trouble
  StringBuffer strbuf = new StringBuffer(buf.length * 2);
  int i;

  for (i = 0; i < buf.length; i++) {
   if (((int) buf[i] & 0xff) < 0x10)
    strbuf.append("0");

   strbuf.append(Long.toString((int) buf[i] & 0xff, 16));
  }

  return strbuf.toString();
 }

 public static void main(String[] args) throws Exception {
   Scanner sc = new Scanner(System.in);
   KeyGenerator kgen = KeyGenerator.getInstance("AES");kgen.init(128); 
   SecretKey skey = kgen.generateKey();
   byte[] bytes = skey.getEncoded();
   SecretKeySpec skeySpec = new SecretKeySpec(bytes, "AES");
   Cipher cipher = Cipher.getInstance("AES");
   System.out.print("Do you want to encrypt or unencrypt?\n");/*This is a weird way of doing it,*/
   String choice = sc.next(); char cc = choice.charAt(2);     /*I know, but this part checks to see if*/       
   if(cc=='c'){                                               /*the program is to encrypt or unencrypt*/
   System.out.print("Enter a string to encrypt: ");          /* a string. The 'encrypt' function works.*/
   String message = sc.next();


   cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
   byte[] encrypted = cipher.doFinal((args.length == 0 ? message : args[0]).getBytes());
   System.out.println("Encrypted string: " + asHex(encrypted)+"\nKey: "+asHex(bytes));

   //^This^ section actually works! The code outputs an encrypted string and everything.
   //It's beautiful
   //Unfortunately getting that string back into readable text has been problematic
   //Which is where you guys come in!
   //Hopefully

 }
  if(true){
   System.out.print("\nEnter the encrypted string: "); String encryptedString = sc.next();
   System.out.print("\nEnter the key: "); String keyString = sc.next();
   int len = encryptedString.length();    /*this section converts the user-input string*/
   byte[] encrypted = new byte[len / 2];  /*into an array of bytes*/
   for (int i = 0; i < len; i += 2) {     /*I'm not sure if it works, though*/
   encrypted[i / 2] = (byte) ((Character.digit(encryptedString.charAt(i), 16) << 4)+
           Character.digit(encryptedString.charAt(i+1), 16));
   cipher.init(Cipher.DECRYPT_MODE, skeySpec); /*as you can see, I haven't even begun to implement*/ 
   byte[] original = cipher.doFinal(encrypted);/*a way to allow the user-input key to be used.*/
   String originalString = new String(original);
   System.out.println("\nOriginal string: "+originalString); //I'm really quite stuck.
      //can you guys help?
  } 

 }
   }
}

嗯,希望有人可以帮助我。

编辑:

我最大的问题是将 String encryptedString 转换为 sKeySpec 并弄清楚如何防止“unencrypt”函数给用户一个错误,指出他们输入的 String 没有正确填充。我知道这不是真的,因为我尝试加密一个字符串,然后将它的加密形式粘贴到 unencryptor 中,结果却出现错误。如果我消除所有“if”条件并让它加密一个字符串然后在同一个实例中解密它,程序就可以正常工作;我认为这是由于保存了keyGen的随机密钥

【问题讨论】:

  • 您能否编辑您的问题以具体解决您遇到的问题?从代码中的 cmets 中推断出你的问题有点困难。

标签: java arrays string encryption hex


【解决方案1】:

你的问题是这样的:

KeyGenerator kgen = KeyGenerator.getInstance("AES");kgen.init(128); 
SecretKey skey = kgen.generateKey();
byte[] bytes = skey.getEncoded();
SecretKeySpec skeySpec = new SecretKeySpec(bytes, "AES");

正如您编写的那样,您的程序每次运行时都会生成一个新的随机密钥,该密钥永远不会保存或显示在任何地方。您使用此密钥加密的任何内容实际上都无法解密。

您需要做的是想出一些从用户输入生成密钥的方案,而不是使用KeyGenerator 随机生成它。该方案将如何运作取决于您。

【讨论】:

    【解决方案2】:

    根据您使用的 AES 变体,您的密钥需要为 128、192 或 256 位长。 您可以使用 HashAlgorithm 从用户输入生成具有特定长度的密钥。

    String key;
    byte[] keydata = hashFunctionToMakeToKeytheRightSize(key);
    SecretKeySpec secretKeySpec = new SecretKeySpec(keydata, "AES");
    

    另见:java-aes-and-using-my-own-key

    【讨论】:

    • "找不到方法:.hasFunctionToMakeToKEytheRightSize"
    猜你喜欢
    • 2019-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-20
    • 2021-04-22
    • 2011-04-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多