import java.security.*;
創(chuàng)新互聯(lián)于2013年開始,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè)網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢想脫穎而出為使命,1280元岱山做網(wǎng)站,已為上家服務(wù),為岱山各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:18982081108
import javax.crypto.*;
/**
* 本例解釋如何利用DES私鑰加密算法加解密
*
* @author Devon
* @version 1.0 04/03/10
*/
public class SingleKeyExample {
public static void main(String[] args) {
try {
String algorithm = "DES"; //定義加密算法,可用 DES,DESede,Blowfish
String message = "Hello World. 這是待加密的信息";
// 生成個(gè)DES密鑰
KeyGenerator keyGenerator = KeyGenerator.getInstance(algorithm);
keyGenerator.init(56); //選擇DES算法,密鑰長度必須為56位
Key key = keyGenerator.generateKey(); //生成密鑰
// 生成Cipher對(duì)象
Cipher cipher = Cipher.getInstance("DES");
//用密鑰加密明文(message),生成密文(cipherText)
cipher.init(Cipher.ENCRYPT_MODE, key); //操作模式為加密(Cipher.ENCRYPT_MODE),key為密鑰
byte[] cipherText = cipher.doFinal(message.getBytes()); //得到加密后的字節(jié)數(shù)組
System.out.println("加密后的信息: " + new String(cipherText));
//用密鑰加密明文(plainText),生成密文(cipherByte)
cipher.init(Cipher.DECRYPT_MODE, key); //操作模式為解密,key為密鑰
byte[] sourceText = cipher.doFinal(cipherText); //獲得解密后字節(jié)數(shù)組
System.out.println("解密后的信息: " + new String(sourceText));
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
/**
* @author Devon
*/
import java.security.*;
import java.security.spec.*;
import javax.crypto.*;
public class PairKeyExample {
public static void main(String argv[]) {
try {
String algorithm = "RSA"; //定義加密算法,可用 DES,DESede,Blowfish
String message = "張三,你好,我是李四";
//產(chǎn)生張三的密鑰對(duì)(keyPairZhang)
KeyPairGenerator keyGeneratorZhang =
KeyPairGenerator.getInstance(algorithm); //指定采用的算法
keyGeneratorZhang.initialize(1024); //指定密鑰長度為1024位
KeyPair keyPairZhang = keyGeneratorZhang.generateKeyPair(); //產(chǎn)生密鑰對(duì)
System.out.println("生成張三的公鑰對(duì)");
// 張三生成公鑰(publicKeyZhang)并發(fā)送給李四,這里發(fā)送的是公鑰的數(shù)組字節(jié)
byte[] publicKeyZhangEncode = keyPairZhang.getPublic().getEncoded();
//通過網(wǎng)絡(luò)或磁盤等方式,把公鑰編碼傳送給李四
//李四接收到張三編碼后的公鑰,將其解碼
KeyFactory keyFacoryLi = KeyFactory.getInstance(algorithm); //得到KeyFactory對(duì)象
X509EncodedKeySpec x509KeySpec =
new X509EncodedKeySpec(publicKeyZhangEncode); //公鑰采用X.509編碼
PublicKey publicKeyZhang = keyFacoryLi.generatePublic(x509KeySpec); //將公鑰的KeySpec對(duì)象轉(zhuǎn)換為公鑰
System.out.println("李四成功解碼,得到張三的公鑰");
//李四用張三的公鑰加密信息,并發(fā)送給李四
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); //得到Cipher對(duì)象
cipher.init(Cipher.ENCRYPT_MODE, publicKeyZhang); //用張三的公鑰初始化Cipher對(duì)象
byte[] cipherMessage = cipher.doFinal(message.getBytes()); //得到加密信息
System.out.println("加密后信息:" + new String(cipherMessage));
System.out.println("加密完成,發(fā)送給李四...");
//張三用自己的私鑰解密從李四處收到的信息
cipher.init(Cipher.DECRYPT_MODE, keyPairZhang.getPrivate()); //張三用其私鑰初始化Cipher對(duì)象
byte[] originalMessage = cipher.doFinal(cipherMessage); //得到解密后信息
System.out.println("張三收到信息,解密后為:" + new String(originalMessage));
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
//base64字符串轉(zhuǎn)化成圖片
public static boolean GenerateImage(String imgStr)
{ //對(duì)字節(jié)數(shù)組字符串進(jìn)行Base64解碼并生成圖片
if (imgStr == null) //圖像數(shù)據(jù)為空
return false;
BASE64Decoder decoder = new BASE64Decoder();
try
{
//Base64解碼
byte[] b = decoder.decodeBuffer(imgStr);
for(int i=0;ib.length;++i)
{
if(b[i]0)
{//調(diào)整異常數(shù)據(jù)
b[i]+=256;
}
}
//生成jpeg圖片
String imgFilePath = "d://222.jpg";//新生成的圖片
OutputStream out = new FileOutputStream(imgFilePath);
out.write(b);
out.flush();
out.close();
return true;
}
catch (Exception e)
{
return false;
}
}
希望可以幫到你。
private String getPictureString() {
String upload = "";
try {
FileInputStream in = new FileInputStream(fileName);
byte[] ba = new byte[in.available()];
in.read(ba);
in.close();
upload = new String(android.util.Base64.encode(ba,
android.util.Base64.DEFAULT));
} catch (FileNotFoundException e) {
LogUtil.e(e.getMessage(), e);
} catch (IOException e) {
LogUtil.e(e.getMessage(), e);
}
return upload;
}
這個(gè)是加密
解密就是
encode換成decode
upload?=?new?String(android.util.Base64.decode(ba,
android.util.Base64.DEFAULT));