Featured image of post java生成AES密文在NET环境下解密问题

java生成AES密文在NET环境下解密问题

java 加密方法

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
    private static final String KEY_ALGORITHM = "AES";
    private static final String DEFAULT_CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";
    private static final String DEFAULT_CHAR_SET = "UTF-8";
    private static final int BIT_AES = 128;

    private static SecretKeySpec getSecretKey(final String key) throws NoSuchProviderException, UnsupportedEncodingException {
        KeyGenerator kg = null;
        try {
            kg = KeyGenerator.getInstance(KEY_ALGORITHM);
            SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
            random.setSeed(key.getBytes(DEFAULT_CHAR_SET));
            kg.init(BIT_AES, random);
            SecretKey secretKey = kg.generateKey();
            return new SecretKeySpec(secretKey.getEncoded(), KEY_ALGORITHM);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }

    private static Cipher getCipher(final String key, final int mode) {
        try {
            Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
            cipher.init(mode, getSecretKey(key));
            return cipher;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static String encrypt(final String content, final String key) {
        try {
            byte[] bytes = content.getBytes(DEFAULT_CHAR_SET);
            byte[] result = getCipher(key, Cipher.ENCRYPT_MODE).doFinal(bytes);
            return Base64.getEncoder().encodeToString(result);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

在C#下默认会解密异常

会提示等错误 由于不搞C 不深究问题原因等

1
2
3
4
Exception has occurred: CLR/System.Exception
An unhandled exception of type 'System.Exception' occurred in AESDebug.dll: 'Decryption failed'
 Inner exceptions found, see $exception in variables window for more details.
 Innermost exception      System.Security.Cryptography.CryptographicException : Padding is invalid and cannot be removed.

1
2
Exception has occurred: CLR/System.Security.Cryptography.CryptographicException
An unhandled exception of type 'System.Security.Cryptography.CryptographicException' occurred in System.Security.Cryptography.dll: 'The input data is not a complete block.'

修改C下的加解密方法

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
public static class AESUtils
{
    public static string decrypt(string decrypt,string key)
       {
           byte[] keyArray = Convert.FromBase64String(key); 
           byte[] toEncryptArray = Convert.FromBase64String(decrypt);
           RijndaelManaged rDel = new RijndaelManaged();
           rDel.Key = keyArray;
           rDel.Mode = CipherMode.ECB;        //要java一致  
           rDel.Padding = PaddingMode.PKCS7;  //和java一致 java是PKCS5
           ICryptoTransform cTransform = rDel.CreateDecryptor();
           byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
           return UTF8Encoding.UTF8.GetString(resultArray);
       }
}

重新调整给C平台的秘钥

通过java获取 其中keyid 为在java环境下的解密key

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
    public static void main(String[] args)
            throws NoSuchProviderException, UnsupportedEncodingException, NoSuchAlgorithmException {
        KeyGenerator kgen = KeyGenerator.getInstance("AES");
        java.security.SecureRandom random = java.security.SecureRandom.getInstance("SHA1PRNG");
        random.setSeed("keyid".getBytes(DEFAULT_CHAR_SET));
        kgen.init(BIT_AES, random);
        SecretKey secretKey = kgen.generateKey();
        byte[] enCodeFormat = secretKey.getEncoded();
        System.out.println(Base64.getEncoder().encodeToString(enCodeFormat));
    }

将秘钥的二进制转为base64 获取之后net平台通过输出的新key 一个base64的字符串作为解密秘钥进行解密

密文不需要处理直接传入即可解密

使用 Hugo 构建
主题 StackJimmy 设计