用java解码c#生成的aes iv和密钥

ajsxfq5m  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(545)

在c语言中,他们使用下面的代码生成密钥。

using System;
using System.Text;
using System.Security.Cryptography;

public class Program
{
    public static void Main()
    {            
                var aesEncryption = new RijndaelManaged
{
    KeySize = 128,
    BlockSize = 128,
    Mode = CipherMode.CBC,
    Padding = PaddingMode.PKCS7

};
    aesEncryption.GenerateIV();
    string ivStr = Convert.ToBase64String(aesEncryption.IV);
    aesEncryption.GenerateKey();
    string keyStr = Convert.ToBase64String(aesEncryption.Key);
    string completeKey = Convert.ToBase64String(ASCIIEncoding.UTF8.GetBytes(ivStr + "," + keyStr));
    // Console.WriteLine(completeKey);
                }
}

样品完成键:sentukr2qjz1zjbyv0o2axnccnrqqt09ldjpdtzhmupym0rbtq2njl6zg83z2c9pq==
在java中,我是如何解码completekey来获得iv和key的。我使用了下面的代码,并且key和iv不是128位的,我的加密失败了。

import java.util.Base64;

public class HelloWorld{

     public static void main(String []args){
        String originalInput = "SENTUkR2QjZ1ZjBYV0o2aXNCcnRqQT09LDJpdTZHMUpWYm0rbTQ2Njl6ZG83Z2c9PQ==";

String decodedString = new String(Base64.getDecoder().decode(originalInput.getBytes()));
        String[] arrOfStr = decodedString.split(",", 2); 
        String iv=arrOfStr[0];
        String key=arrOfStr[1];
        System.out.println(decodedString);//HCSRDvB6uf0XWJ6isBrtjA==,2iu6G1JVbm+m4669zdo7gg==
        System.out.println(iv);//HCSRDvB6uf0XWJ6isBrtjA==
        System.out.println(key);//2iu6G1JVbm+m4669zdo7gg==

     }
}
bt1cpqcv

bt1cpqcv1#

您的iv和key首先被编码为base64字符串,然后以“,”作为分隔符连接,结果字符串被转换为字节数组,最后被base64编码。
下面的程序正在反向运行,最后您会发现一个长度(正确)为16字节的iv和一个长度为16字节=128位的aes密钥—这两个密钥都适用于cbc模式下的aes加密。
安全警告:这种传输密钥的方式在大多数情况下都是不安全的,因此以下代码仅用于教育目的。
结果:

Decode C# generated AES IV and KEY in Java
ivStr: k7ivGm6OoMHRlnk4tZCAMA==
ivExp: k7ivGm6OoMHRlnk4tZCAMA==
keyStr: Lq5wkaGWFknsEKUxoAY46w==
keyExp: Lq5wkaGWFknsEKUxoAY46w==
iv  length: 16 data: 93b8af1a6e8ea0c1d1967938b5908030
key length: 16 data: 2eae7091a1961649ec10a531a00638eb

代码:

import java.util.Base64;

public class Main {
    public static void main(String[] args) {
        System.out.println("Decode C# generated AES IV and KEY in Java");
        // https://stackoverflow.com/questions/63503753/decode-c-sharp-generated-aes-iv-and-key-in-java
        /* dataset
        iv:k7ivGm6OoMHRlnk4tZCAMA==
        key: Lq5wkaGWFknsEKUxoAY46w==
        azdpdkdtNk9vTUhSbG5rNHRaQ0FNQT09LExxNXdrYUdXRmtuc0VLVXhvQVk0Nnc9PQ==
         */
        String originalInput = "azdpdkdtNk9vTUhSbG5rNHRaQ0FNQT09LExxNXdrYUdXRmtuc0VLVXhvQVk0Nnc9PQ==";
        byte[] originalInputByte = Base64.getDecoder().decode(originalInput);
        String decodedString = new String(originalInputByte);
        String[] arrOfStr = decodedString.split(",", 2);
        String ivStr = arrOfStr[0];
        String keyStr = arrOfStr[1];
        System.out.println("ivStr: " + ivStr);
        System.out.println("ivExp: " + "k7ivGm6OoMHRlnk4tZCAMA==");
        System.out.println("keyStr: " + keyStr);
        System.out.println("keyExp: " + "Lq5wkaGWFknsEKUxoAY46w==");
        // final conversion to byte[]
        byte[] iv = Base64.getDecoder().decode(ivStr);
        byte[] key = Base64.getDecoder().decode(keyStr);
        System.out.println("iv  length: " + iv.length + " data: " + bytesToHex(iv));
        System.out.println("key length: " + key.length + " data: " + bytesToHex(key));
    }
    private static String bytesToHex(byte[] bytes) {
        StringBuffer result = new StringBuffer();
        for (byte b : bytes) result.append(Integer.toString((b & 0xff) + 0x100, 16).substring(1));
        return result.toString();
    }
}

相关问题