Java转C#.net-使用AES和密码进行加密和解密

12 浏览
0 Comments

Java转C#.net-使用AES和密码进行加密和解密

我们有一个Java库,它使用AES密码进行加密和解密,现在我们需要将其转移到.NET平台上。

这是我的Java代码:

import java.util.*;
import java.lang.*;
import java.io.File;
import java.io.FileInputStream;
import java.math.BigInteger;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
class Rextester
{  
    public static void main(String args[])
    {
        byte[] encrypted = EncryptonToBytes("Hello");
        System.out.println("Encrypted: " + encrypted); // 结果 --> Encrypted: [B@1c53fd30
        String decrypted = DecryptionFromBytes(encrypted);
        System.out.println("Decrypted: " + decrypted);
    }
    public static final byte[] EncryptonToBytes(String str)
    {
        byte[] result = null; 
        //从常量类中提取公共密钥字符串创建SecretKey对象
        byte[] encoded = new BigInteger("728faf34b64cd55c8d1d500268026ffb", 16).toByteArray();
        SecretKey secretKey = new SecretKeySpec(encoded, "AES");
        Cipher cipher;
        try {
            //使用AES作为转换创建Cipher类对象
            cipher = Cipher.getInstance("AES");
            //与上述创建的密钥对象一起初始化加密模式的cipher
            cipher.init(Cipher.ENCRYPT_MODE, secretKey);
            //加密输入字符串
            result = cipher.doFinal(str.getBytes());
        } catch (Exception e) {
            e.printStackTrace();
        } 
        //如果有错误,返回null
        return result;
    }
    public static String DecryptionFromBytes(byte[] base64Data) 
    {       
        try {
            //从常量类中提取公共密钥字符串创建SecretKey对象
            byte[] encoded = new BigInteger("728faf34b64cd55c8d1d500268026ffb", 16).toByteArray();
            SecretKey secretKey = new SecretKeySpec(encoded, "AES");
            //使用AES作为转换创建Cipher类对象
            Cipher cipher = Cipher.getInstance("AES");
            //与上述创建的密钥对象一起初始化解密模式的cipher
            cipher.init(Cipher.DECRYPT_MODE, secretKey);
            //解密输入的字节数组
            byte[] decryptedByte = cipher.doFinal(base64Data);
            //将解密后的字节作为字符串返回
            return (new String(decryptedByte));
        } catch (Exception e) {
            e.printStackTrace();
        } 
        //如果有错误,返回空字符串
        return "";
    }
}

我参考了以下一些文章:

  1. 在C#中使用AES加密
  2. http://csharphelper.com/blog/2014/09/encrypt-or-decrypt-files-in-c/
  3. https://www.codeproject.com/Articles/769741/Csharp-AES-bits-Encryption-Library-with-Salt
  4. Cipher,Java加密,C#解密
  5. .NET中的密码加密/解密代码
  6. https://ourcodeworld.com/articles/read/471/how-to-encrypt-and-decrypt-files-using-the-aes-encryption-algorithm-in-c-sharp
  7. http://mjremijan.blogspot.com/2014/08/aes-encryption-between-java-and-c.html

但是它们都没有给我想要的输出结果。我想要的是,我的Java和.NET函数都能给出相同的结果。

在过去的一周里,我一直在尝试完成它,感到非常沮丧。非常感谢您的帮助。

0
0 Comments

Java和C#是两种不同的编程语言,它们有各自的加密和解密方法。上述代码展示了在Java中使用AES进行加密和解密的方法,并提供了将其转化为C#的代码的方法。

在给定的代码中,有两个方法:`EncryptionToBytes`和`DecryptionFromBytes`。`EncryptionToBytes`方法接受一个字符串作为输入,并使用AES算法对其进行加密。`DecryptionFromBytes`方法接受一个字节数组作为输入,并使用AES算法对其进行解密。这两个方法使用相同的密钥和模式(ECB),以及填充模式(PKCS7)。

需要注意的是,Java中的`System.out.println`方法不会直接打印字节数组,而是打印数组的类型和标识哈希码。为了在C#中模拟Java代码的行为,可以使用`Encoding.Default`。

总之,这段代码的目的是演示如何在Java和C#之间进行AES加密和解密。通过提供相应的方法,可以在两种语言之间进行转换,并实现相同的功能。

0