以下兩個類可以很方便的完成字元串的加密和解密
加密 CryptHelper encrypt(password)
解密 CrypHelper decrypt(password)
代碼如下
CryptUtils java
[java]
package gdie lab crypt;
import java io IOException;
import javax crypto Cipher;
import javax crypto KeyGenerator;
import javax crypto SecretKey;
import apache xerces internal impl dv util Base ;
public class CryptUtils {
private static String Algorithm = DES ;
private static byte[] DEFAULT_KEY=new byte[] { };
private static String VALUE_ENCODING= UTF ;
/**
* 生成密鑰
*
* @return byte[] 返回生成的密鑰
* @throws exception
* 扔出異常
*/
public static byte[] getSecretKey() throws Exception {
KeyGenerator keygen = KeyGenerator getInstance(Algorithm)
SecretKey deskey = keygen generateKey()
// if (debug ) System out println ( 生成密鑰 +byte hex (deskey getEncoded
// ()))
return deskey getEncoded()
}
/**
* 將指定的數據根據提供的密鑰進行加密
*
* @param input
* 需要加密的數據
* @param key
* 密鑰
* @return byte[] 加密後的數據
* @throws Exception
*/
public static byte[] encryptData(byte[] input byte[] key) throws Exception {
SecretKey deskey = new javax crypto spec SecretKeySpec(key Algorithm)
// if (debug )
// {
// System out println ( 加密前的二進串 +byte hex (input ))
// System out println ( 加密前的字元串 +new String (input ))
//
// }
Cipher c = Cipher getInstance(Algorithm)
c init(Cipher ENCRYPT_MODE deskey)
byte[] cipherByte = c doFinal(input)
// if (debug ) System out println ( 加密後的二進串 +byte hex (cipherByte ))
return cipherByte;
}
public static byte[] encryptData(byte[] input) throws Exception {
return encryptData(input DEFAULT_KEY)
}
/**
* 將給定的已加密的數據通過指定的密鑰進行解密
*
* @param input
* 待解密的數據
* @param key
* 密鑰
* @return byte[] 解密後的數據
* @throws Exception
*/
public static byte[] decryptData(byte[] input byte[] key) throws Exception {
SecretKey deskey = new javax crypto spec SecretKeySpec(key Algorithm)
// if (debug ) System out println ( 解密前的信息 +byte hex (input ))
Cipher c = Cipher getInstance(Algorithm)
c init(Cipher DECRYPT_MODE deskey)
byte[] clearByte = c doFinal(input)
// if (debug )
// {
// System out println ( 解密後的二進串 +byte hex (clearByte ))
// System out println ( 解密後的字元串 +(new String (clearByte )))
//
// }
return clearByte;
}
public static byte[] decryptData(byte[] input) throws Exception {
return decryptData(input DEFAULT_KEY)
}
/**
* 位元組碼轉換成 進制字元串
*
* @param byte[] b 輸入要轉換的位元組碼
* @return String 返回轉換後的 進制字元串
*/
public static String byte hex(byte[] bytes) {
StringBuilder hs = new StringBuilder()
for(byte b : bytes)
hs append(String format( % $ X b))
return hs toString()
}
public static byte[] hex byte(String content) {
int l=content length()》 ;
byte[] result=new byte[l];
for(int i= ;i<l;i++) {
int j=i《 ;
String s=content substring(j j+ )
result[i]=Integer valueOf(s ) byteValue()
}
return result;
}
/**
* 將位元組數組轉換為base 編碼字元串
* @param buffer
* @return
*/
public static String bytesToBase (byte[] buffer) {
//BASE Encoder en=new BASE Encoder()
return Base encode(buffer)
// return encoder encode(buffer)
}
/**
* 將base 編碼的字元串解碼為位元組數組
* @param value
* @return
* @throws IOException
*/
public static byte[] base ToBytes(String value) throws IOException {
//return Base decodeToByteArray(value)
// System out println(decoder decodeBuffer(value))
// return decoder decodeBuffer(value)
return Base decode(value)
}
/**
* 加密給定的字元串
* @param value
* @return 加密後的base 字元串
*/
public static String encryptString(String value) {
return encryptString(value DEFAULT_KEY)
}
/**
* 根據給定的密鑰加密字元串
* @param value 待加密的字元串
* @param key 以BASE 形式存在的密鑰
* @return 加密後的base 字元串
* @throws IOException
*/
public static String encryptString(String value String key) throws IOException {
return encryptString(value base ToBytes(key))
}
/**
* 根據給定的密鑰加密字元串
* @param value 待加密的字元串
* @param key 位元組數組形式的密鑰
* @return 加密後的base 字元串
*/
public static String encryptString(String value byte[] key) {
try {
byte[] data=value getBytes(VALUE_ENCODING)
data=CryptUtils encryptData(data key)
return bytesToBase (data)
} catch (Exception e) {
// TODO Auto generated catch block
e printStackTrace()
return null;
}
}
/**
* 解密字元串
* @param value base 形式存在的密文
* @return 明文
*/
public static String decryptString(String value) {
return decryptString(value DEFAULT_KEY)
}
/**
* 解密字元串
* @param value base 形式存在的密文
* @param key base 形式存在的密鑰
* @return 明文
* @throws IOException
*/
public static String decryptString(String value String key) throws IOException {
String s=decryptString(value base ToBytes(key))
return s;
}
/**
* 解密字元串
* @param value base 形式存在的密文
* @param key 位元組數據形式存在的密鑰
* @return 明文
*/
public static String decryptString(String value byte[] key) {
try {
byte[] data=base ToBytes(value)
data=CryptUtils decryptData(data key)
return new String(data VALUE_ENCODING)
}catch(Exception e) {
e printStackTrace()
return null;
}
}
}
package gdie lab crypt;
import java io IOException;
import javax crypto Cipher;
import javax crypto KeyGenerator;
import javax crypto SecretKey;
import apache xerces internal impl dv util Base ;
public class CryptUtils {
private static String Algorithm = DES ;
private static byte[] DEFAULT_KEY=new byte[] { };
private static String VALUE_ENCODING= UTF ;
/**
* 生成密鑰
*
* @return byte[] 返回生成的密鑰
* @throws exception
* 扔出異常
*/
public static byte[] getSecretKey() throws Exception {
KeyGenerator keygen = KeyGenerator getInstance(Algorithm)
SecretKey deskey = keygen generateKey()
// if (debug ) System out println ( 生成密鑰 +byte hex (deskey getEncoded
// ()))
return deskey getEncoded()
}
/**
* 將指定的數據根據提供的密鑰進行加密
*
* @param input
* 需要加密的數據
* @param key
* 密鑰
* @return byte[] 加密後的數據
* @throws Exception
*/
public static byte[] encryptData(byte[] input byte[] key) throws Exception {
SecretKey deskey = new javax crypto spec SecretKeySpec(key Algorithm)
// if (debug )
// {
// System out println ( 加密前的二進串 +byte hex (input ))
// System out println ( 加密前的字元串 +new String (input ))
//
// }
Cipher c = Cipher getInstance(Algorithm)
c init(Cipher ENCRYPT_MODE deskey)
byte[] cipherByte = c doFinal(input)
// if (debug ) System out println ( 加密後的二進串 +byte hex (cipherByte ))
return cipherByte;
}
public static byte[] encryptData(byte[] input) throws Exception {
return encryptData(input DEFAULT_KEY)
}
/**
* 將給定的已加密的數據通過指定的密鑰進行解密
*
* @param input
* 待解密的數據
* @param key
* 密鑰
* @return byte[] 解密後的數據
* @throws Exception
*/
public static byte[] decryptData(byte[] input byte[] key) throws Exception {
SecretKey deskey = new javax crypto spec SecretKeySpec(key Algorithm)
// if (debug ) System out println ( 解密前的信息 +byte hex (input ))
Cipher c = Cipher getInstance(Algorithm)
c init(Cipher DECRYPT_MODE deskey)
byte[] clearByte = c doFinal(input)
// if (debug )
// {
// System out println ( 解密後的二進串 +byte hex (clearByte ))
// System out println ( 解密後的字元串 +(new String (clearByte )))
//
// }
return clearByte;
}
public static byte[] decryptData(byte[] input) throws Exception {
return decryptData(input DEFAULT_KEY)
}
/**
* 位元組碼轉換成 進制字元串
*
* @param byte[] b 輸入要轉換的位元組碼
* @return String 返回轉換後的 進制字元串
*/
public static String byte hex(byte[] bytes) {
StringBuilder hs = new StringBuilder()
for(byte b : bytes)
hs append(String format( % $ X b))
return hs toString()
}
public static byte[] hex byte(String content) {
int l=content length()》 ;
byte[] result=new byte[l];
for(int i= ;i<l;i++) {
int j=i《 ;
String s=content substring(j j+ )
result[i]=Integer valueOf(s ) byteValue()
}
return result;
}
/**
* 將位元組數組轉換為base 編碼字元串
* @param buffer
* @return
*/
public static String bytesToBase (byte[] buffer) {
//BASE Encoder en=new BASE Encoder()
return Base encode(buffer)
// return encoder encode(buffer)
}
/**
* 將base 編碼的字元串解碼為位元組數組
* @param value
* @return
* @throws IOException
*/
public static byte[] base ToBytes(String value) throws IOException {
//return Base decodeToByteArray(value)
// System out println(decoder decodeBuffer(value))
// return decoder decodeBuffer(value)
return Base decode(value)
}
/**
* 加密給定的字元串
* @param value
* @return 加密後的base 字元串
*/
public static String encryptString(String value) {
return encryptString(value DEFAULT_KEY)
}
/**
* 根據給定的密鑰加密字元串
* @param value 待加密的字元串
* @param key 以BASE 形式存在的密鑰
* @return 加密後的base 字元串
* @throws IOException
*/
public static String encryptString(String value String key) throws IOException {
return encryptString(value base ToBytes(key))
}
/**
* 根據給定的密鑰加密字元串
* @param value 待加密的字元串
* @param key 位元組數組形式的密鑰
* @return 加密後的base 字元串
*/
public static String encryptString(String value byte[] key) {
try {
byte[] data=value getBytes(VALUE_ENCODING)
data=CryptUtils encryptData(data key)
return bytesToBase (data)
} catch (Exception e) {
// TODO Auto generated catch block
e printStackTrace()
return null;
}
}
/**
* 解密字元串
* @param value base 形式存在的密文
* @return 明文
*/
public static String decryptString(String value) {
return decryptString(value DEFAULT_KEY)
}
/**
* 解密字元串
* @param value base 形式存在的密文
* @param key base 形式存在的密鑰
* @return 明文
* @throws IOException
*/
public static String decryptString(String value String key) throws IOException {
String s=decryptString(value base ToBytes(key))
return s;
}
/**
* 解密字元串
* @param value base 形式存在的密文
* @param key 位元組數據形式存在的密鑰
* @return 明文
*/
public static String decryptString(String value byte[] key) {
try {
byte[] data=base ToBytes(value)
data=CryptUtils decryptData(data key)
return new String(data VALUE_ENCODING)
}catch(Exception e) {
e printStackTrace()
return null;
}
}
}
CryptHelper java
[java]
package gdie lab crypt;
import javax crypto Cipher;
import javax crypto SecretKey;
import javax crypto SecretKeyFactory;
import javax crypto spec DESKeySpec;
import javax crypto spec IvParameterSpec;
import springframework util DigestUtils;
public class CryptHelper{
private static String CRYPT_KEY = zhongqian ;
//加密
private static Cipher ecip;
//解密
private static Cipher dcip;
static {
try {
String KEY = DigestUtils md DigestAsHex(CRYPT_KEY getBytes()) toUpperCase()
KEY = KEY substring( )
byte[] bytes = KEY getBytes()
DESKeySpec ks = new DESKeySpec(bytes)
SecretKeyFactory skf = SecretKeyFactory getInstance( DES )
SecretKey sk = skf generateSecret(ks)
IvParameterSpec iv = new IvParameterSpec(bytes)
ecip = Cipher getInstance( DES/CBC/PKCS Padding )
ecip init(Cipher ENCRYPT_MODE sk iv )
dcip = Cipher getInstance( DES/CBC/PKCS Padding )
dcip init(Cipher DECRYPT_MODE sk iv )
}catch(Exception ex) {
ex printStackTrace()
}
}
public static String encrypt(String content) throws Exception {
byte[] bytes = ecip doFinal(content getBytes( ascii ))
return CryptUtils byte hex(bytes)
}
public static String decrypt(String content) throws Exception {
byte[] bytes = CryptUtils hex byte(content)
bytes = dcip doFinal(bytes)
return new String(bytes ascii )
}
//test
public static void main(String[] args) throws Exception {
String password = gly ;
String en = encrypt(password)
System out println(en)
System out println(decrypt(en))
}
}
package gdie lab crypt;
import javax crypto Cipher;
import javax crypto SecretKey;
import javax crypto SecretKeyFactory;
import javax crypto spec DESKeySpec;
import javax crypto spec IvParameterSpec;
import springframework util DigestUtils;
public class CryptHelper{
private static String CRYPT_KEY = zhongqian ;
//加密
private static Cipher ecip;
//解密
private static Cipher dcip;
static {
try {
String KEY = DigestUtils md DigestAsHex(CRYPT_KEY getBytes()) toUpperCase()
KEY = KEY substring( )
byte[] bytes = KEY getBytes()
DESKeySpec ks = new DESKeySpec(bytes)
SecretKeyFactory skf = SecretKeyFactory getInstance( DES )
SecretKey sk = skf generateSecret(ks)
IvParameterSpec iv = new IvParameterSpec(bytes)
ecip = Cipher getInstance( DES/CBC/PKCS Padding )
ecip init(Cipher ENCRYPT_MODE sk iv )
dcip = Cipher getInstance( DES/CBC/PKCS Padding )
dcip init(Cipher DECRYPT_MODE sk iv )
}catch(Exception ex) {
ex printStackTrace()
}
}
public static String encrypt(String content) throws Exception {
byte[] bytes = ecip doFinal(content getBytes( ascii ))
return CryptUtils byte hex(bytes)
}
public static String decrypt(String content) throws Exception {
byte[] bytes = CryptUtils hex byte(content)
bytes = dcip doFinal(bytes)
return new String(bytes ascii )
}
//test
public static void main(String[] args) throws Exception {
String password = gly ;
String en = encrypt(password)
System out println(en)
System out println(decrypt(en))
}
lishixin/Article/program/Java/hx/201311/26449
『貳』 javaDES加密 C 語言DES解密
再用java寫一段解密演算法就好了。本來數據類型就不一致。或者加密解密就用c寫,還是很方便
『叄』 java生成rsa密鑰,c++可以直接使用密鑰解密嗎
可以的,RSA加密解密有一套規則的,不同的語言都會遵循,只是實現的方式不一樣。
用對應的的公鑰就可以進行解密
『肆』 如何用Java進行3DES加密解密
這里是例子,直接拿來用就可以了。
package com.nnff.des;
import java.security.Security;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
/*字元孝姿耐串冊談 DESede(3DES) 加密
* ECB模式/使用PKCS7方式填充不足位,目前給的密鑰是192位
* 3DES(即Triple DES)是DES向AES過渡的加密演算法(1999年,NIST將3-DES指定為過渡的
* 加密標准),是DES的一個更安全的巧春變形。它以DES為基本模塊,通過組合分組方法設計出分組加
* 密演算法,其具體實現如下:設Ek()和Dk()代表DES演算法的加密和解密過程,K代表DES演算法使用的
* 密鑰,P代表明文,C代表密表,這樣,
* 3DES加密過程為:C=Ek3(Dk2(Ek1(P)))
* 3DES解密過程為:P=Dk1((EK2(Dk3(C)))
* */
public class ThreeDes {
/**
* @param args在java中調用sun公司提供的3DES加密解密演算法時,需要使
* 用到$JAVA_HOME/jre/lib/目錄下如下的4個jar包:
*jce.jar
*security/US_export_policy.jar
*security/local_policy.jar
*ext/sunjce_provider.jar
*/
private static final String Algorithm = "DESede"; //定義加密演算法,可用 DES,DESede,Blowfish
//keybyte為加密密鑰,長度為24位元組
//src為被加密的數據緩沖區(源)
public static byte[] encryptMode(byte[] keybyte,byte[] src){
try {
//生成密鑰
SecretKey deskey = new SecretKeySpec(keybyte, Algorithm);
//加密
Cipher c1 = Cipher.getInstance(Algorithm);
c1.init(Cipher.ENCRYPT_MODE, deskey);
return c1.doFinal(src);//在單一方面的加密或解密
} catch (java.security.NoSuchAlgorithmException e1) {
// TODO: handle exception
e1.printStackTrace();
}catch(javax.crypto.NoSuchPaddingException e2){
e2.printStackTrace();
}catch(java.lang.Exception e3){
e3.printStackTrace();
}
return null;
}
//keybyte為加密密鑰,長度為24位元組
//src為加密後的緩沖區
public static byte[] decryptMode(byte[] keybyte,byte[] src){
try {
//生成密鑰
SecretKey deskey = new SecretKeySpec(keybyte, Algorithm);
//解密
Cipher c1 = Cipher.getInstance(Algorithm);
c1.init(Cipher.DECRYPT_MODE, deskey);
return c1.doFinal(src);
} catch (java.security.NoSuchAlgorithmException e1) {
// TODO: handle exception
e1.printStackTrace();
}catch(javax.crypto.NoSuchPaddingException e2){
e2.printStackTrace();
}catch(java.lang.Exception e3){
e3.printStackTrace();
}
return null;
}
//轉換成十六進制字元串
public static String byte2Hex(byte[] b){
String hs="";
String stmp="";
for(int n=0; n<b.length; n++){
stmp = (java.lang.Integer.toHexString(b[n]& 0XFF));
if(stmp.length()==1){
hs = hs + "0" + stmp;
}else{
hs = hs + stmp;
}
if(n<b.length-1)hs=hs+":";
}
return hs.toUpperCase();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
//添加新安全演算法,如果用JCE就要把它添加進去
Security.addProvider(new com.sun.crypto.provider.SunJCE());
final byte[] keyBytes = {0x11, 0x22, 0x4F, 0x58,
(byte)0x88, 0x10, 0x40, 0x38, 0x28, 0x25, 0x79, 0x51,
(byte)0xCB,
(byte)0xDD, 0x55, 0x66, 0x77, 0x29, 0x74,
(byte)0x98, 0x30, 0x40, 0x36,
(byte)0xE2
}; //24位元組的密鑰
String szSrc = "This is a 3DES test. 測試";
System.out.println("加密前的字元串:" + szSrc);
byte[] encoded = encryptMode(keyBytes,szSrc.getBytes());
System.out.println("加密後的字元串:" + new String(encoded));
byte[] srcBytes = decryptMode(keyBytes,encoded);
System.out.println("解密後的字元串:" + (new String(srcBytes)));
}
}
『伍』 javarsa加密c#解密失敗
系統bug。當軟體javarsa的系統出現系統bug時,就會導致該軟體在解密c井的程序的時候出現解密失敗的情況,只需要將該軟體卸載後重新安裝該軟體即可。
『陸』 des解密演算法,利用C語言解密JAVA語言加密的密碼。。密鑰為12345678,加密後的密文為:26d086be3a3a62fc
// C 語言 DES用的是 ECB模式, 沒有填充
// 因此Java端要對應, 你的明文是 liubiao 嗎?
// 另外 DES已經不安全了, 如果可以改為 3DES或者 AES吧。
public class LearnDes {
public static void main(String[] args) {
try {
System.out.println(encrypt("liubiao", "12345678"));
System.out.println(decrypt("26d086be3a3a62fc", "12345678"));
} catch (Exception e) {
e.printStackTrace();
}
}
public static String encrypt(String message, String key) throws Exception {
//Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
Cipher cipher = Cipher.getInstance("DES/ECB/NOPADDING");
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);
cipher.init(Cipher.ENCRYPT_MODE, secretKey );
return toHexString(cipher.doFinal(message.getBytes("UTF-8")));
}
public static String decrypt(String message, String key) throws Exception {
byte[] bytesrc = convertHexString(message);
//Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
Cipher cipher = Cipher.getInstance("DES/ECB/NOPADDING");
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.DECRYPT_MODE, secretKey, iv);
cipher.init(Cipher.DECRYPT_MODE, secretKey );
byte[] retByte = cipher.doFinal(bytesrc);
return new String(retByte);
}
public static byte[] convertHexString(String ss) {
byte digest[] = new byte[ss.length() / 2];
for (int i = 0; i < digest.length; i++) {
String byteString = ss.substring(2 * i, 2 * i + 2);
int byteValue = Integer.parseInt(byteString, 16);
digest[i] = (byte) byteValue;
}
return digest;
}
public static String toHexString(byte b[]) {
StringBuffer hexString = new StringBuffer();
for (int i = 0; i < b.length; i++) {
String plainText = Integer.toHexString(0xff & b[i]);
if (plainText.length() < 2)
plainText = "0" + plainText;
hexString.append(plainText);
}
return hexString.toString();
}
}