C#到Java DES加密

C#到Java DES加密,java,encryption,cryptography,des,encryption-symmetric,Java,Encryption,Cryptography,Des,Encryption Symmetric,尝试创建java类,该类将像下面的C#代码一样进行加密和解密 下面是我的C#代码,我需要将其转换为Java。有人能帮我怎么做吗 我已经这样做了将近2天,但还没有找到任何解决方案,如何转换它。我是加密新手 注意-似乎C#代码使用了ACME库。但是在我的java中,我不应该使用AcmeJAR文件 public static string EncryptBase64(string key, string clearText) { if (key.Length > 8)

尝试创建java类,该类将像下面的C#代码一样进行加密和解密

下面是我的C#代码,我需要将其转换为Java。有人能帮我怎么做吗

我已经这样做了将近2天,但还没有找到任何解决方案,如何转换它。我是加密新手

注意-似乎C#代码使用了ACME库。但是在我的java中,我不应该使用AcmeJAR文件

public static string EncryptBase64(string key, string clearText)
    {
        if (key.Length > 8)
            key = key.Substring(0, 8);

        byte[] keyBytes = System.Text.Encoding.ASCII.GetBytes(key);
        byte[] clearBytes = GetClearTextBytes(clearText);

        // calculate the number of legitimate bytes in the last block
        byte lastByte = (byte)(8 - (clearBytes.Length - textEncoding.GetByteCount(clearText)));

        MemoryStream ms = new MemoryStream();

        DES des = new DESCryptoServiceProvider();
        des.Padding = PaddingMode.None;
        des.GenerateIV();

        System.Security.Cryptography.ICryptoTransform ict = des.CreateEncryptor(keyBytes, des.IV);
        CryptoStream cs = new CryptoStream(ms, ict, CryptoStreamMode.Write);
        cs.Write(clearBytes, 0, clearBytes.Length);
        cs.FlushFinalBlock();
        ms.Close();

        byte[] cipherBytes = ms.ToArray();

        // create a byte output stream for Acme compatibality
        MemoryStream acmeCompatStream = new MemoryStream();

        // Acme writes the IV to the frist block
        acmeCompatStream.Write(des.IV, 0, 8);

        for (int i = 0; i < cipherBytes.Length; i = i + 8)
        {
            // write the next block
            acmeCompatStream.Write(cipherBytes, i, 8);

            // write the number of valid bytes in the block
            if (i == cipherBytes.Length - 8)
                acmeCompatStream.WriteByte(lastByte);
            else
                acmeCompatStream.WriteByte(8);
        }

        acmeCompatStream.Close();

        cipherBytes = acmeCompatStream.ToArray();

        return (System.Convert.ToBase64String(cipherBytes));
    }
package com.abc.common.encryption;

import java.io.FileInputStream;
import java.util.Properties;
import Acme.Crypto.SecurityDES;

public class ESBCryptoDES {

    public static void main(String args[]){
//      DESEncrypt("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?><SomeRequest><OrderNumber>1564578</OrderNumber></SomeRequest>"
//              ,"D:\\name\\proj\\order\\Encryption\\st.properties");
        DESDecrypt("vQ9C7ZrLzjQpHvZjtHvUb0mFCr824/aClY2jKbeciczsRVr+kEETFvDuHgdBS/aLskYV3WX3U5TANSlK3pH80r3xOyn9Q8rTjlB/yXyU7J9MgibJ66jJx0wrqeloAkmQzqj+b5+I/lXANSlK3pH80kT1D+jqWAeV"
                ,"D:\\name\\proj\\order\\Encryption\\stDecrypt.properties");
    }
    public static String DESEncrypt(String SourceStrForCrypt,String PropPath) {
        String encrypt = "";
        String decrypt = "";
        // Load the property file.
        Properties prop = new Properties();
        try {
            FileInputStream in = new FileInputStream(PropPath);
            prop.load(in);
            in.close();
        } catch (Exception e) {
            System.out.println("Exception in loading property file.. "
                    + e.getMessage());
        }

        // Encrypt the given content.
        try {
            String keypath = prop.getProperty("proj.sample.DEV");
            System.out.println("sample" + keypath);

            String SourceToEncrypt = SourceStrForCrypt; //This will provide the xml string to encrypt
            // Encryption
            encrypt = SecurityDES.DesEncryptBase64(keypath,SourceToEncrypt);
            System.out.println(encrypt);
            // Decryption
            decrypt = SecurityDES.DesDecryptBase64(keypath, encrypt);
            System.out.println(decrypt);

        } catch (Exception e) {
            System.out.println("Exception in Encryption.. " + e.getMessage());
        }
        return encrypt;
    }

    public static String DESDecrypt(String SourceStrForCrypt,String PropPath) {
        // TODO Auto-generated method stub
        String decrypt = "";

        Properties prop = new Properties();

        try {
            FileInputStream in = new FileInputStream(PropPath);
            prop.load(in);
            in.close();
        } catch (Exception e) {
            System.out.println("Exception in loading property file.. "+ e.getMessage());
        }

        try {
            String abc_keyPath = prop
                    .getProperty("proj.abc.DEV");
            System.out.println("keypath" + abc_keyPath);
            // Decryption
            decrypt = SecurityDES.DesDecryptBase64(abc_keyPath, SourceStrForCrypt);
            System.out.println("decrypted..."+decrypt);

        } catch (Exception e) {
            System.out.println("Exception in Encryption.. " + e.getMessage());
        }
        return decrypt;

    }
}
公共静态字符串EncryptBase64(字符串密钥、字符串明文)
{
如果(键长>8)
key=key.Substring(0,8);
byte[]keyBytes=System.Text.Encoding.ASCII.GetBytes(key);
字节[]clearBytes=GetClearTextBytes(clearText);
//计算最后一个块中的合法字节数
字节lastByte=(字节)(8-(clearBytes.Length-textEncoding.GetByteCount(clearText));
MemoryStream ms=新的MemoryStream();
DES DES=new DESCryptoServiceProvider();
des.Padding=PaddingMode.None;
des.GenerateIV();
System.Security.Cryptography.ICryptoTransform ict=des.CreateEncryptor(密钥字节,des.IV);
CryptoStream cs=新加密流(ms、ict、CryptoStreamMode.Write);
cs.Write(clearBytes,0,clearBytes.Length);
cs.FlushFinalBlock();
Close女士();
字节[]cipherBytes=ms.ToArray();
//为Acme兼容性创建字节输出流
MemoryStream acmeCompatStream=新的MemoryStream();
//Acme将IV写入第一个块
acmeCompatStream.Write(des.IV,0,8);
对于(int i=0;i
下面是我在java中尝试过的代码。我有两种不同的加密功能。我已经尝试了这两种加密方法。但两者都没有给出在acme中解密的预期加密字符串

    package com.abc.some.common.nativeDES;

    import java.io.ByteArrayOutputStream;
import java.security.spec.KeySpec;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import javax.xml.bind.DatatypeConverter;

public class DESEncrypt {
    public String keyValue = "123456789";
    public static void main(String[] args) {
        String text = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?><SomeRequest><OrderNumber>1564578</OrderNumber></SomeRequest>";
        String codedtext ="not encrypted";
        try{
            codedtext = new DESEncrypt().Encrypt1(text);
            //codedtext = new DESEncrypt().encrypt(text);
        }catch (Exception e) {
            System.out.println("Exception in Encryption.. " + e.getMessage());
        }

        System.out.println(codedtext);

    }
    public String Encrypt1(String CXML) {
        try {
            KeySpec myKey = new DESKeySpec(keyValue.getBytes("UTF8"));
        SecretKey key = SecretKeyFactory.getInstance("DES").generateSecret(myKey);
        Cipher ecipher = Cipher.getInstance("DES");
        ecipher.init(Cipher.ENCRYPT_MODE, key);
        byte[] data = CXML.getBytes("ASCII");

        Cipher c = Cipher.getInstance("DES/CBC/PKCS5Padding");
        c.init(Cipher.ENCRYPT_MODE, key);

        byte[] crypt = ecipher.doFinal(data);
        //String encoded = DatatypeConverter.printBase64Binary(crypt.toString().getBytes("ASCII"));
        //String encoded = DatatypeConverter.printBase64Binary(crypt.getBytes("ASCII"));

        String encoded = DatatypeConverter.printBase64Binary(crypt).toString();

        System.out.println(encoded);

        return encoded;
        } catch (Exception ex) {
        }

        return null;
    }    
}
package com.abc.some.common.nativeDES;
导入java.io.ByteArrayOutputStream;
导入java.security.spec.KeySpec;
导入javax.crypto.Cipher;
导入javax.crypto.SecretKey;
导入javax.crypto.SecretKeyFactory;
导入javax.crypto.spec.DESKeySpec;
导入javax.crypto.spec.IvParameterSpec;
导入javax.xml.bind.DatatypeConverter;
公共类去加密{
公共字符串keyValue=“123456789”;
公共静态void main(字符串[]args){
String text=“1564578”;
字符串codedtext=“未加密”;
试一试{
codedtext=new DESEncrypt().Encrypt1(文本);
//codedtext=new DESEncrypt().encrypt(文本);
}捕获(例外e){
System.out.println(“加密中的异常…”+e.getMessage());
}
System.out.println(codedtext);
}
公共字符串加密1(字符串CXML){
试一试{
keypecmykey=newdeskeyspec(keyValue.getBytes(“UTF8”);
SecretKey key=SecretKeyFactory.getInstance(“DES”).generateScret(myKey);
Cipher-ecipher=Cipher.getInstance(“DES”);
ecipher.init(Cipher.ENCRYPT_模式,密钥);
byte[]data=CXML.getBytes(“ASCII”);
Cipher c=Cipher.getInstance(“DES/CBC/PKCS5Padding”);
c、 init(Cipher.ENCRYPT_模式,密钥);
字节[]crypt=ecipher.doFinal(数据);
//字符串编码=DatatypeConverter.printBase64Binary(crypt.toString().getBytes(“ASCII”);
//字符串编码=DatatypeConverter.printBase64Binary(crypt.getBytes(“ASCII”);
字符串编码=DatatypeConverter.printBase64Binary(crypt.toString();
系统输出打印项次(编码);
返回编码;
}捕获(例外情况除外){
}
返回null;
}    
}
但是我使用了下面的java文件来加密使用AcmeJAR文件的字符串。这是工作的预期。但是根据我的项目要求,我不应该使用外部(ACME)jar文件

public static string EncryptBase64(string key, string clearText)
    {
        if (key.Length > 8)
            key = key.Substring(0, 8);

        byte[] keyBytes = System.Text.Encoding.ASCII.GetBytes(key);
        byte[] clearBytes = GetClearTextBytes(clearText);

        // calculate the number of legitimate bytes in the last block
        byte lastByte = (byte)(8 - (clearBytes.Length - textEncoding.GetByteCount(clearText)));

        MemoryStream ms = new MemoryStream();

        DES des = new DESCryptoServiceProvider();
        des.Padding = PaddingMode.None;
        des.GenerateIV();

        System.Security.Cryptography.ICryptoTransform ict = des.CreateEncryptor(keyBytes, des.IV);
        CryptoStream cs = new CryptoStream(ms, ict, CryptoStreamMode.Write);
        cs.Write(clearBytes, 0, clearBytes.Length);
        cs.FlushFinalBlock();
        ms.Close();

        byte[] cipherBytes = ms.ToArray();

        // create a byte output stream for Acme compatibality
        MemoryStream acmeCompatStream = new MemoryStream();

        // Acme writes the IV to the frist block
        acmeCompatStream.Write(des.IV, 0, 8);

        for (int i = 0; i < cipherBytes.Length; i = i + 8)
        {
            // write the next block
            acmeCompatStream.Write(cipherBytes, i, 8);

            // write the number of valid bytes in the block
            if (i == cipherBytes.Length - 8)
                acmeCompatStream.WriteByte(lastByte);
            else
                acmeCompatStream.WriteByte(8);
        }

        acmeCompatStream.Close();

        cipherBytes = acmeCompatStream.ToArray();

        return (System.Convert.ToBase64String(cipherBytes));
    }
package com.abc.common.encryption;

import java.io.FileInputStream;
import java.util.Properties;
import Acme.Crypto.SecurityDES;

public class ESBCryptoDES {

    public static void main(String args[]){
//      DESEncrypt("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?><SomeRequest><OrderNumber>1564578</OrderNumber></SomeRequest>"
//              ,"D:\\name\\proj\\order\\Encryption\\st.properties");
        DESDecrypt("vQ9C7ZrLzjQpHvZjtHvUb0mFCr824/aClY2jKbeciczsRVr+kEETFvDuHgdBS/aLskYV3WX3U5TANSlK3pH80r3xOyn9Q8rTjlB/yXyU7J9MgibJ66jJx0wrqeloAkmQzqj+b5+I/lXANSlK3pH80kT1D+jqWAeV"
                ,"D:\\name\\proj\\order\\Encryption\\stDecrypt.properties");
    }
    public static String DESEncrypt(String SourceStrForCrypt,String PropPath) {
        String encrypt = "";
        String decrypt = "";
        // Load the property file.
        Properties prop = new Properties();
        try {
            FileInputStream in = new FileInputStream(PropPath);
            prop.load(in);
            in.close();
        } catch (Exception e) {
            System.out.println("Exception in loading property file.. "
                    + e.getMessage());
        }

        // Encrypt the given content.
        try {
            String keypath = prop.getProperty("proj.sample.DEV");
            System.out.println("sample" + keypath);

            String SourceToEncrypt = SourceStrForCrypt; //This will provide the xml string to encrypt
            // Encryption
            encrypt = SecurityDES.DesEncryptBase64(keypath,SourceToEncrypt);
            System.out.println(encrypt);
            // Decryption
            decrypt = SecurityDES.DesDecryptBase64(keypath, encrypt);
            System.out.println(decrypt);

        } catch (Exception e) {
            System.out.println("Exception in Encryption.. " + e.getMessage());
        }
        return encrypt;
    }

    public static String DESDecrypt(String SourceStrForCrypt,String PropPath) {
        // TODO Auto-generated method stub
        String decrypt = "";

        Properties prop = new Properties();

        try {
            FileInputStream in = new FileInputStream(PropPath);
            prop.load(in);
            in.close();
        } catch (Exception e) {
            System.out.println("Exception in loading property file.. "+ e.getMessage());
        }

        try {
            String abc_keyPath = prop
                    .getProperty("proj.abc.DEV");
            System.out.println("keypath" + abc_keyPath);
            // Decryption
            decrypt = SecurityDES.DesDecryptBase64(abc_keyPath, SourceStrForCrypt);
            System.out.println("decrypted..."+decrypt);

        } catch (Exception e) {
            System.out.println("Exception in Encryption.. " + e.getMessage());
        }
        return decrypt;

    }
}
package com.abc.common.encryption;
导入java.io.FileInputStream;
导入java.util.Properties;
导入Acme.Crypto.SecurityDES;
公共类ESBCryptoDES{
公共静态void main(字符串参数[]){
//解除加密(“1564578”
//,“D:\\name\\proj\\order\\Encryption\\st.properties”);
解密(“VQ9C7ZrlzJQPhVzJthVu0Mfcr824/acly2jkBeciczsrvrvr+kEETFvDuHgdBS/aLskYV3WX3U5TANSlK3pH80r3xOyn9Q8rTjlB/yxyu7j9mgibj66jx0wrqeloakMqzQj+b5+I/lXANSlK3pH80kT1D+jqWAeV”
,“D:\\name\\proj\\order\\Encryption\\stDecrypt.properties”);
}
公共静态字符串DESEncrypt(字符串SourceStrForCrypt、字符串PropPath){
字符串encrypt=“”;
字符串解密=”;
//加载属性文件。
Properties prop=新属性();
试一试{
FileInputStream in=新的FileInputStream(PropPath);
道具荷载(in);
in.close();
}捕获(例外e){
System.out.println(“加载属性文件时出现异常…”
+e.getMessage());
}
//加密给定的内容。
试一试{
字符串keypath=prop.getProperty(“proj.sample.DEV”);
System.out.println(“示例”+键路径);
String SourceToEncrypt=SourceStrForCrypt;//这将提供要加密的xml字符串
//加密
encrypt=SecurityDES.DesEncryptBase64(keypath,SourceToEncrypt);
System.out.println(加密);
//解密
decrypt=SecurityDES.DesDecryptBase64(密钥路径,加密);
System.out.println(解密);
}捕获(例外e){
System.out.println(“加密中的异常…”+e.getMe