?? desencrypt.java
字號:
package encrypt;
import java.security.Security;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
/**
*
* <p>Title: DES對稱算法</p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class DESEncrypt
{
public DESEncrypt()
{
}
public static void main(String[] args)
{
des();
}
static void des()
{
Security.addProvider( new com.sun.crypto.provider.SunJCE() );
String Algorithm = "DES"; //定義 加密算法,可用 DES,DESede,Blowfish
String myinfo = "要加密的信息";
try
{
//生成密鑰
KeyGenerator keygen = KeyGenerator.getInstance( Algorithm );
SecretKey deskey = keygen.generateKey();
//加密
// System.out.println( "加密前的二進串:" + byte2hex( myinfo.getBytes() ) );
System.out.println( "加密前的信息:" + myinfo );
Cipher c1 = Cipher.getInstance( Algorithm );
c1.init( Cipher.ENCRYPT_MODE, deskey );
byte[] cipherByte = c1.doFinal( myinfo.getBytes() );
// System.out.println( "加密后的二進串:" + byte2hex( cipherByte ) );
//解密
c1 = Cipher.getInstance( Algorithm );
c1.init( Cipher.DECRYPT_MODE, deskey );
byte[] clearByte = c1.doFinal( cipherByte );
// System.out.println( "解密后的二進串:" + byte2hex( clearByte ) );
System.out.println( "解密后的信息:" + ( new String( clearByte ) ) );
}
catch ( java.security.NoSuchAlgorithmException e1 )
{
e1.printStackTrace();
}
catch ( javax.crypto.NoSuchPaddingException e2 )
{
e2.printStackTrace();
}
catch ( java.lang.Exception e3 )
{
e3.printStackTrace();
}
}
private 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();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -