无法将此文件从vb.net移植到Java AES加密

时间:2016-02-14 22:57:01

标签: java .net vb.net encryption aes

以下是在.net中编码并在Java中解码的完整工作示例,反之亦然

String myData = "kgxCSfBSw5BRxmjgc4qYhwN12dxG0dyf=";
        byte[] salt = new byte[] {0x49, 0x76, 0x61, 0x6E, 0x20, 0x4D, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76};
        String pw  = "kmjfds(#1231SDSA()#rt32geswfkjFJDSKFJDSFd";

        SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
        PBEKeySpec pbeKeySpec = new PBEKeySpec(pw.toCharArray(), salt, 1000, 384);
        Key secretKey = factory.generateSecret(pbeKeySpec);
        byte[] key = new byte[32];
        byte[] iv = new byte[16];
        System.arraycopy(secretKey.getEncoded(), 0, key, 0, 32);
        System.arraycopy(secretKey.getEncoded(), 32, iv, 0, 16);


        SecretKeySpec secretSpec = new SecretKeySpec(key, "AES");
        AlgorithmParameterSpec ivSpec = new IvParameterSpec(iv);
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        Cipher cipher1 = Cipher.getInstance("AES/CBC/PKCS5Padding");

        try {
            cipher.init(Cipher.DECRYPT_MODE,secretSpec,ivSpec);
            cipher1.init(Cipher.ENCRYPT_MODE,secretSpec,ivSpec);
        } catch (InvalidKeyException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvalidAlgorithmParameterException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        //byte[] decordedValue;
        //decordedValue = new BASE64Decoder().decodeBuffer(myData);
        //decordedValue = myData.getBytes("ISO-8859-1");
          //byte[] decValue = cipher.doFinal(myData.getBytes());
        //Base64.getMimeEncoder().encodeToString(cipher.doFinal(myData.getBytes()));
            //String decryptedValue = new String(decValue);
        byte[] decodedValue  = new Base64().decode(myData.getBytes());



          String clearText = "ljfva09876FK";


          //String encodedValue  = new Base64().encodeAsString(clearText.getBytes("UTF-16"));



          byte[] cipherBytes = cipher1.doFinal(clearText.getBytes("UTF-16LE"));
          //String cipherText = new String(cipherBytes, "UTF8");
          String encoded = Base64.encodeBase64String(cipherBytes);
          System.out.println(encoded);


            byte[] decValue =    cipher.doFinal(decodedValue);

            System.out.println(new String(decValue, StandardCharsets.UTF_16LE));

这是Java等价物。谢谢大家的帮助!确保也在Java的安全性文件夹中安装JCE策略。

import sys

readFile = open("Products.txt", "r")
#reportfile = open("report.txt", "w")
reportfile = sys.stdout

Categories_seen = []
Current_category = None
Category_sum = 0
Total_sum = 0

line = readFile.readline()

for line in readFile:
    category,product,sales2014,sales2015 = line.strip().split(',')
    sales2015 = int(sales2015)

    if category not in Categories_seen:
        if Current_category is not None: # Subroutine, if possible
            print( Current_category, 
                Category_sum, file=reportfile)
        Current_category = category
        Category_sum = sales2015
        Total_sum += sales2015
        Categories_seen.append(Current_category)
    elif Current_category == category:
        Category_sum += sales2015
        Total_sum += sales2015

if Current_category is not None:  # Subroutine, if possible
    print("Total sales for", Current_category, "in 2015:",
        Category_sum, file=reportfile)

print("----------", file=reportfile)
print("Total sales for the farmer in 2015:", Total_sum, file=reportfile)

2 个答案:

答案 0 :(得分:0)

您的迭代计数应为1000(而不是1),这是RFC中建议的最小值和(未指定)默认值Rfc2898DeriveBytes

  

对于本文档中的方法,最低限度      建议使用1000次迭代

所以这将转化为:

PBEKeySpec pbeKeySpec = new PBEKeySpec(pw.toCharArray(), salt, 1000, 384);

在Java代码中。请注意,强烈建议使用更高的迭代次数,尤其是在允许使用较弱密码的情况下。现在40K-100K是最低的。

错误命名的Unicode实际上意味着.NET中的UTF-16,因此您应该使用:

new String(decValue, StandardCharsets.UTF_16LE)

在Java代码的最后println语句中。

答案 1 :(得分:0)

感谢Maarten Bodewes的回答

String myData = "kgxCSfBSw5BRxmjgc4qYhwN12dxG0=";
    byte[] salt = new byte[] {0x49, 0x76, 0x61, 0x6E, 0x20, 0x4D, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76};
    String pw  = "kmjfds(#1231SDSA()#rt32geswfkjFJDSKFJDSFd";

    SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
    PBEKeySpec pbeKeySpec = new PBEKeySpec(pw.toCharArray(), salt, 1000, 384);
    Key secretKey = factory.generateSecret(pbeKeySpec);
    byte[] key = new byte[32];
    byte[] iv = new byte[16];
    System.arraycopy(secretKey.getEncoded(), 0, key, 0, 32);
    System.arraycopy(secretKey.getEncoded(), 32, iv, 0, 16);


    SecretKeySpec secretSpec = new SecretKeySpec(key, "AES");
    AlgorithmParameterSpec ivSpec = new IvParameterSpec(iv);
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    try {
        cipher.init(Cipher.DECRYPT_MODE,secretSpec,ivSpec);
    } catch (InvalidKeyException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InvalidAlgorithmParameterException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    //byte[] decordedValue;
    //decordedValue = new BASE64Decoder().decodeBuffer(myData);
    //decordedValue = myData.getBytes("ISO-8859-1");
      //byte[] decValue = cipher.doFinal(myData.getBytes());
    //Base64.getMimeEncoder().encodeToString(cipher.doFinal(myData.getBytes()));
        //String decryptedValue = new String(decValue);
    byte[] decodedValue  = new Base64().decode(myData.getBytes());


    byte[] decValue =    cipher.doFinal(decodedValue);

        System.out.println(new String(decValue, StandardCharsets.UTF_16LE));