?? asymmetric.java
字號:
/*
Name: Asymmetric.java
Licensing: LGPL
API: Sun (http://java.sun.com) JCE 1.2.2 API (cleanroom implementation by Bouncy Castle)
Provider: Bouncy Castle (http://www.bouncycastle.org)
Disclaimer:
COVERED CODE IS PROVIDED UNDER THIS LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTY OF ANY KIND,
EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES THAT THE COVERED CODE
IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE OR NON-INFRINGING. THE ENTIRE
RISK AS TO THE QUALITY AND PERFORMANCE OF THE COVERED CODE IS WITH YOU. SHOULD ANY COVERED CODE
PROVE DEFECTIVE IN ANY RESPECT, YOU (NOT THE INITIAL DEVELOPER OR ANY OTHER CONTRIBUTOR)
ASSUME THE COST OF ANY NECESSARY SERVICING, REPAIR OR CORRECTION. THIS DISCLAIMER OF WARRANTY
CONSTITUTES AN ESSENTIAL PART OF THIS LICENSE. NO USE OF ANY COVERED CODE IS AUTHORIZED
HEREUNDER EXCEPT UNDER THIS DISCLAIMER.
(C) Copyright 2003 Gert Van Ham
*/
package net.sourceforge.jcetaglib.lib;
import net.sourceforge.jcetaglib.exceptions.CryptoException;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.util.encoders.Base64;
import javax.crypto.Cipher;
import java.io.*;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Security;
/**
* Asymmetric encryption & decryption routines with BouncyCastle JCE provider
*
* @author Gert Van Ham
* @author hamgert@users.sourceforge.net
* @author http://jcetaglib.sourceforge.net
* @version $Id: Asymmetric.java,v 1.3 2004/04/15 07:28:24 hamgert Exp $
*/
public class Asymmetric {
// buffersizes in bytes
private static int BUFFERSIZE_TEXT = 64;
/**
* Encrypts text with a public RSA key (from a X.509 certificate)
*
* @param text the text to encrypt
* @param encryptKey the public encryption key
* @return the encrypted text in BASE64 format
* @throws CryptoException for encryption errors
*/
public static StringBuffer encrypt(StringBuffer text
, PublicKey encryptKey)
throws CryptoException {
ByteArrayOutputStream bao = null;
DataOutputStream dao = null;
try {
bao = new ByteArrayOutputStream();
dao = new DataOutputStream(bao);
// Encrypt
encrypt(new ByteArrayInputStream(text.toString().getBytes()), dao, encryptKey, BUFFERSIZE_TEXT);
return new StringBuffer(new String(Base64.encode(bao.toByteArray())));
} catch (IOException ioe) {
ioe.printStackTrace();
throw new CryptoException(ioe.getMessage());
} finally {
if (dao != null) {
// close outputstream
try {
dao.close();
} catch (IOException e) {
;
}
}
}
}
/**
* Encrypts an inputstream with a public RSA key (from a X.509 certificate)
* This result can only be decrypted with the corresponding private key
*
* @param is the inputstream to encrypt
* @param daos returns ciphered outputstream
* @param encryptKey the public encryption key
* @throws IOException I/O errors
* @throws CryptoException for all encryption errors
**/
public static void encrypt(InputStream is
, DataOutputStream daos
, PublicKey encryptKey
, int bufferlength)
throws CryptoException, IOException {
Cipher cipher = null;
try {
Security.addProvider(new BouncyCastleProvider());
// create a cipher object: ("algorithm/mode/padding", provider)
cipher = Cipher.getInstance("RSA/ECB/OAEPPadding", "BC");
cipher.init(Cipher.ENCRYPT_MODE, encryptKey);
byte[] buffer = new byte[bufferlength];
int length = 0;
while ((length = is.read(buffer)) != -1) {
cipher.update(buffer, 0, length);
}
byte[] result = cipher.doFinal();
daos.write(result);
} catch (IOException ioe) {
ioe.printStackTrace();
throw new IOException(ioe.getMessage());
} catch (Exception ex) {
ex.printStackTrace();
throw new CryptoException(ex.getMessage());
}
}
/**
* decrypts text with a private RSA key (from a X.509 certificate)
*
* @param text the text to decrypt
* @param decryptKey the private key
* @return the encrypted text in BASE64 format
* @throws CryptoException for encryption errors
*/
public static StringBuffer decrypt(StringBuffer text
, PrivateKey decryptKey)
throws CryptoException {
ByteArrayOutputStream bao = null;
DataOutputStream dao = null;
try {
bao = new ByteArrayOutputStream();
dao = new DataOutputStream(bao);
// Decrypt
decrypt(new ByteArrayInputStream(Base64.decode(text.toString())), dao, decryptKey, BUFFERSIZE_TEXT);
return new StringBuffer(new String(bao.toByteArray()));
} catch (IOException ioe) {
ioe.printStackTrace();
throw new CryptoException(ioe.getMessage());
} finally {
if (dao != null) {
// close outputstream
try {
dao.close();
} catch (IOException e) {
;
}
}
}
}
/**
* Decrypts an inputstream, encrypted with an RSA public key (from X.509), with the
* corresponding private key
*
* @param is the inputstream to decrypt
* @param daos returns deciphered outputstream
* @param decryptKey the private key
* @throws IOException I/O errors
* @throws CryptoException for all encryption errors
**/
public static void decrypt(InputStream is
, DataOutputStream daos
, PrivateKey decryptKey
, int bufferlength)
throws CryptoException, IOException {
Cipher cipher = null;
try {
Security.addProvider(new BouncyCastleProvider());
// create a cipher object: ("algorithm/mode/padding", provider)
cipher = Cipher.getInstance("RSA/ECB/OAEPPadding", "BC");
cipher.init(Cipher.DECRYPT_MODE, decryptKey);
byte[] buffer = new byte[bufferlength];
int length = 0;
while ((length = is.read(buffer)) != -1) {
cipher.update(buffer, 0, length);
}
byte[] result = cipher.doFinal();
daos.write(result);
} catch (IOException ioe) {
ioe.printStackTrace();
throw new IOException(ioe.getMessage());
} catch (Exception ex) {
ex.printStackTrace();
throw new CryptoException(ex.getMessage());
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -