½ñÌìÅöÉÏÒ»¼þÁîÎÒÍ·´óµÄÊÂÇé¡£ÎÒÃǵÄϵͳҪºÍÒ»¸öÍⲿϵͳ½øÐÐͨѶ£¬´«Ê䷽ʽÊDzÉÓà DES Ëã·¨¶ÔÏûÏ¢½øÐмÓÃÜ£¬ÔÙÓà BASE64 ±àÂë¡£²»¹ý¶Ô·½ÏµÍ³ÊÇÓà C# дµÄ¡£Æ½Ì¨²»Ò»Ñù£¬ÓÚÊÇÎҺͶÔÃæµÄÀÏÐÖÏȲâÊÔһϼÓÃܽâÃÜ¡£ÕâÒ»²âÊÔÎÊÌâ¾ÍÀ´ÁË¡£Á½±ß²ÉÓÃͬÑùµÄÃÜÔ¿£¬¶Ôͬһ¸ö×Ö·û´®¼ÓÃܳöÀ´µÄ½á¹û²»Ò»Ñù¡£Ôõô°ìÄØ£¿ÎÒÏȰÑËûÄDZߵĴúÂëÒª¹ýÀ´ÁË£¬ËûÊÇÕâÑùдµÄ(C#)£º
public static string Encrypt(string message, string key)
{
DES des = new DESCryptoServiceProvider();
des.Key = Encoding.UTF8.GetBytes(key);
des.IV = Encoding.UTF8.GetBytes(key);
byte[] bytes = Encoding.UTF8.GetBytes(message);
byte[] resultBytes = des.CreateEncryptor().TransformFinalBlock(bytes, 0, bytes.Length);
return Convert.ToBase64String(resultBytes);
}
¶øÎÒÕâ±ßÄØ£¬ÊÇÓà Java ÕâÑùдµÄ£¨½ö½ö¼ÓÃܵ½×Ö½Ú´®£¬BASE64 ±àÂëÊÇÔÚÁíÒ»¸öÀàÀ£º
public static byte[] desEncrypt(String message, String key) throws Exception {
Cipher cipher = Cipher.getInstance("DES");
DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("UTF-8"));
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
return cipher.doFinal(message.getBytes("UTF-8"));
}
Á½±ßÒ»¶Ô±ÈÎÒ¾ÍÆæ¹ÖÁË£¬ËûÄDZßÔõôÓиö IV (
initialization vector)£¿ÎÒ´ò¿ªJDKÎĵµÒ»¿´CipherÀ࣬¾¹È»Ò²ÓÐÒ»¸ögetIV()·½·¨¡£¿´À´Õâ IV ´óÓÐÀ´Í·¡£ÓÚÊÇÔÚJDKÎĵµÀïÃæµ½´¦ÕÒIV£¬ÕÒµ½ÁËÒ»¸ö½Ð¡°IvParameterSpec¡°µÄÀ࣬Ī·Ç¾ÍÊÇËü£¿ËüʵÏÖµÄÊÇ AlgorithmParameterSpec ½Ó¿Ú£¬¶øCipher.init()ÖоÍÓÐÒ»¸ö AlgorithmParameterSpec ÀàÐ͵IJÎÊý¡£ÄÇô¿´À´¾ÍÊÇËüÁË¡£ÓÚÊDzο¼ÁËIvParameterSpecµÄ¹¹Ô캯Êýºó£¬ÎÒÕâÑù°ÑËü¼Ó½øÈ¥£º
public static byte[] desEncrypt(String message, String key) throws Exception {
Cipher cipher = Cipher.getInstance("DES");
DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("UTF-8"));
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
IvParameterSpec iv = new IvParameterSpec(key.getBytes("UTF-8"));
cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);
return cipher.doFinal(message.getBytes("UTF-8"));
}
ÕâÏÂÐÐÁ˰ɣ¬½á¹ûÒ»ÔËÐб¨Òì³££º
java.security.InvalidAlgorithmParameterException: ECB mode cannot use IV
ÕâÓÖÊÇʲô£¿ ¡°ECB¡±£¿ »¹ÊÇÕÒ°É£¬ÔÚJDKÎĵµÖÐÕÒµ½ÁËÕâô¼¸¾ä¹í¶«Î÷£º
the SunJCE provider uses ECB as the default mode, and PKCS5Padding as the default padding scheme for DES, DES-EDE and Blowfish ciphers. This means that in the case of the SunJCE provider,
Cipher c1 = Cipher.getInstance("DES/ECB/PKCS5Padding");
and
Cipher c1 = Cipher.getInstance("DES");
are equivalent statements.
Ŷ£¬ÎÊÌâ¾Í³öÔÚ·½·¨ÀïµÄµÚÒ»ÐС£ÄÇô²»×¼Óà ECB£¬¸ÃÓÃÊ²Ã´ÄØ£¿ÔÚ¸ÃÎÄÍùÏÂÕÒ£¬ÕÒµ½ÁËÕâô¼¸Ï
The following names can be specified as the mode component in a transformation when requesting an instance of Cipher:
- NONE: No mode.
- CBC : Cipher Block Chaining Mode, as defined in FIPS PUB 81.
- CFB: Cipher Feedback Mode, as defined in FIPS PUB 81.
- ECB: Electronic Codebook Mode, as defined in: The National Institute of Standards and Technology (NIST) Federal Information Processing Standard (FIPS) PUB 81, "DES Modes of Operation," U.S. Department of Commerce, Dec 1980.
- OFB: Output Feedback Mode, as defined in FIPS PUB 81.
- PCBC: Propagating Cipher Block Chaining, as defined by Kerberos V4.
ÎÒÕâÑù¶¼»»×ÅÊÔһϾÍÐÐÁË¡£°¥£¬ÔËÆø²»´í£¬ÄÃ×ŵÚÒ»¸ö CBC ÊÔһϾͳɹ¦ÁË£¬¼ÓÃܺóµÄ×Ö·û´®Ò²¡°ÕýÈ·¡±ÁË¡£
ÎÒÄǸö·½·¨¾Í±ä³ÉÕâÑù£º
public static byte[] desEncrypt(String message, String key) throws Exception {
Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("UTF-8"));
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
IvParameterSpec iv = new IvParameterSpec(key.getBytes("UTF-8"));
cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);
return cipher.doFinal(message.getBytes("UTF-8"));
}
ÕâÑù£¬½â¾öÁËÕâ¸ö ECB Ö®ºó£¬Java ºÍ .net ƽ̨µÄÁ½¸öÓ¦ÓÃÖÕÓÚÄܹ»Ï໥¼ÓÃܺͽâÃÜÊý¾ÝÁË¡£Ê²Ã´£¿ÎIJ»¶ÔÌ⣿¼ÓÃܶ¼Ð´³öÀ´ÁË£¬½âÃÜ»¹²»¼òµ¥£¿×Ô¼º¿´×Űì°É¡£
Äܲ»ÄܰÑjava´úÂëÍêÕûµÄ¸øÒ»·Ý¸øÎÒ£¿ÄãÕâÀïдµÄÖ»¼ÓÃܵ½×Ö½Ú´®£¬StringÐ͵ÄÄØ£¿ÎÒµÄÓÊÏäÊÇyutan_313@163.com£¬QQÊÇ23730897£¬Ï£ÍûÄܰïÎÒһϣ¬ÎÒÏÖÔÚÒ²Óöµ½ÁËÕâÑùµÄÎÊÌ⡣лл¥Ö÷ÁË£¡