需要在java中DES加密后传给c#/vb用,即java des加密字符串后,c#/vb解密 失败
发现即使des加密结果都不一样.怎么回事呀,如何解决.多谢
C#:其中KEY_64\IV_64均为"ABCDEFGH"
public static string Encode(string data)
{
byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64);
byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64); DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
int i = cryptoProvider.KeySize;
MemoryStream ms = new MemoryStream();
CryptoStream cst = new CryptoStream(ms,cryptoProvider.CreateEncryptor(byKey,byIV),CryptoStreamMode.Write);
   
StreamWriter sw = new StreamWriter(cst);
sw.Write(data);
sw.Flush();
cst.FlushFinalBlock();
sw.Flush();
return Convert.ToBase64String(ms.GetBuffer(),0,(int)ms.Length);
}
Encode("13988888888")后得到结果是"BHFtXIFNMTKfE+QcKrxL9w=="java改为:
import javax.crypto.*;
import java.io.*;
import javax.crypto.spec.*;
import java.security.spec.*;public class DES {
        Cipher ecipher;
        Cipher dcipher;
    
        // 8-byte Salt
        byte[] salt = {
            (byte)0x41, (byte)0x42, (byte)0x43, (byte)0x44,
            (byte)0x45, (byte)0x46, (byte)0x47, (byte)0x48
        };
    
        // Iteration count
        int iterationCount = 19;
    
        DES(String passPhrase) {
            try {
                // Create the key
                KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), salt, iterationCount);
                SecretKey key = SecretKeyFactory.getInstance(
                    "PBEWithMD5AndDES").generateSecret(keySpec);
                ecipher = Cipher.getInstance(key.getAlgorithm());
                dcipher = Cipher.getInstance(key.getAlgorithm());
    
                // Prepare the parameter to the ciphers
                AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, iterationCount);
    
                // Create the ciphers
                ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
                dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
            } catch (java.security.InvalidAlgorithmParameterException e) {
            } catch (java.security.spec.InvalidKeySpecException e) {
            } catch (javax.crypto.NoSuchPaddingException e) {
            } catch (java.security.NoSuchAlgorithmException e) {
            } catch (java.security.InvalidKeyException e) {
            }
        }
    
        public String encrypt(String str) {
            try {
                // Encode the string into bytes using utf-8
                byte[] utf8 = str.getBytes("UTF8");
    
                // Encrypt
                byte[] enc = ecipher.doFinal(utf8);
    
                // Encode bytes to base64 to get a string
                return new sun.misc.BASE64Encoder().encode(enc);
            } catch (javax.crypto.BadPaddingException e) {
            } catch (IllegalBlockSizeException e) {
            } catch (UnsupportedEncodingException e) {
            } catch (java.io.IOException e) {
            }
            return null;
        }
    
        public String decrypt(String str) {
            try {
                // Decode base64 to get bytes
                byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(str);
    
                // Decrypt
                byte[] utf8 = dcipher.doFinal(dec);
    
                // Decode using utf-8
                return new String(utf8, "UTF8");
            } catch (javax.crypto.BadPaddingException e) {
            } catch (IllegalBlockSizeException e) {
            } catch (UnsupportedEncodingException e) {
            } catch (java.io.IOException e) {
            }
            return null;
        }
    // Here is an example that uses the class
    
public static void main(String s[])
{
try {
        // Create encrypter/decrypter class
        DES encrypter = new DES("ABCDEFGH");
    
        // Encrypt
        String encrypted = encrypter.encrypt("13988888888");
System.out.println(encrypted);
        // Decrypt
        String decrypted = encrypter.decrypt(encrypted);
System.out.println(decrypted);
    } catch (Exception e) {e.printStackTrace();}}
}
加密结果是"0fqEGapnVxUtJ/SCaRqIqA=="
还是不一样呀!!不解ing......痛苦ing......

解决方案 »

  1.   

    java的加密方法等是否与c#一致
      

  2.   

    GOOGLE相关语言加密是否有这方面的差别。
      

  3.   

    当然不一样呀以下是创建一个密匙            // Create a TripleDES key
                KeyGenerator keyGenerator = KeyGenerator.getInstance ( "DESede" );
                keyGenerator.init ( 168 );   // need to initialize with the keysize
                Key key = keyGenerator.generateKey ( );可见每次创建的密匙都是不相同的,因此密文不一样!需要在java中DES加密后传给c#/vb用这很有点难度哟,不仅要传送密文还要传送密匙给c#/vb用要用到 corba 和 Java IDL 还要对注意密匙的结构问提。还是放弃吧,真的有难度特别是 密匙的结构问提
      

  4.   

    不能密匙是随机产生的,只能指定密匙的长度。你也可以实现自己的提供者加密算法如果密匙都可以指定那想想加密还有用吗?要用到 corba 和 Java IDL 还要对注意密匙的结构问提,也可以用jni来代替,不过不能实现分布式
      

  5.   

    我第一次运行java加密,得到"0fqEGapnVxUtJ/SCaRqIqA==",另写一个java测试程序解密可以正确解码,如果每次都是随机的,应该解不会来吧,而我上面提供的代码可以呀.
    各位高手帮帮忙呀
      

  6.   

    没排版, 这样可以加上密码的,看最后面的那部分.
    public class JCEDemo {
    private static final String KEY = "Some key characters for JCE Demo";
    private static final String MESSAGE =
    " This is a clear text string for JCE demo  ."; public static void main(String[] args) {
    try {
    System.out.println(" Clear Text Message :\n" + MESSAGE);
    KeyGenerator keyGen = KeyGenerator.getInstance("DES");
    keyGen.init(56); Key key = keyGen.generateKey(); Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, key); byte[] encrypted = cipher.doFinal(MESSAGE.getBytes()); System.out.println(" Encryped :\n" + new String(encrypted)); cipher.init(Cipher.DECRYPT_MODE, key);
    byte[] decrypted = cipher.doFinal(encrypted);
    System.out.println(" Decrypted :\n" + new String(decrypted));
      } catch (Exception e) {
    e.printStackTrace();
      }}
    }
    ---------------------------------------------------------------------------System.out.println(" Clear Text Message :\n" + MESSAGE);
    KeyGenerator keyGen = KeyGenerator.getInstance("DES");
    Key key = keyGen.generateKey();
    SecretKeySpec keySpec = new SecretKeySpec(key.getEncoded(), keyGen.getAlgorithm());Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");cipher.init(Cipher.ENCRYPT_MODE, keySpec);byte[] encrypted = cipher.doFinal(MESSAGE.getBytes());System.out.println(" Encryped :\n" + new String(encrypted));
    cipher.init(Cipher.DECRYPT_MODE, keySpec);byte[] decrypted = cipher.doFinal(encrypted);System.out.println(" Decrypted :\n" + new String(decrypted));
    -------------------------------------------------------------------
    DESKeySpec desKeySpec = new DESKeySpec(KEY.getBytes());
    Key key = keyGen.generateKey();
    SecretKeySpec keySpec = new SecretKeySpec(desKeySpec.getKey(), keyGen.getAlgorithm());