SSL安全性与两个节点上的数据是否加密有关。由于SSL执行加密,在互联网上的连接可以实现安全传输。
package eds;
import java.math.BigInteger;
import java.security.InvalidKeyException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.Base64;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
public class EncDecSymmetric {
Symmetric encryption algorithms supported - AES,
RC4,
DES protected static String DEFAULT_ENCRYPTION_ALGORITHM = "AES";
protected static int DEFAULT_ENCRYPTION_KEY_LENGTH = 256;
protected SecretKey mSecretKey;
protected String mEncryptionAlgorithm,
mKeyEncryptionAlgorithm,
mTransformation;
protected int mEncryptionKeyLength,
mKeyEncryptionKeyLength;
protected PublicKey mPublicKey;
protected PrivateKey mPrivateKey;
EncDecSymmetric() {
mSecretKey = null;
mEncryptionAlgorithm = EncDecSymmetric.DEFAULT_ENCRYPTION_ALGORITHM;
mEncryptionKeyLength = EncDecSymmetric.DEFAULT_ENCRYPTION_KEY_LENGTH;
}
public static BigInteger keyToNumber(byte byteArray) {
return new BigInteger(1, byteArray);
}
public SecretKey getSecretKey() {
return mSecretKey;
}
public byte getSecretKeyAsByteArray() {
return mSecretKey.getEncoded();
}
public String getEncodedPublicKey() {
String encodedKey = Base64.getEncoder().encodeToString(mPublicKey.getEncoded());
return encodedKey;
}
get base64 encoded version of the key public String getEncodedSecretKey() {
String encodedKey = Base64.getEncoder().encodeToString(mSecretKey.getEncoded());
return encodedKey;
}
public void generateSymmetricKey() {
KeyGenerator generator;
try {
generator = KeyGenerator.getInstance(mEncryptionAlgorithm);
generator.init(mEncryptionKeyLength);
mSecretKey = generator.generateKey();
} catch(NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
public byte encryptText(String textToEncrypt) {
byte byteCipherText = null;
try {
Cipher encCipher = Cipher.getInstance(mEncryptionAlgorithm);
encCipher.init(Cipher.ENCRYPT_MODE, mSecretKey);
byteCipherText = encCipher.doFinal(textToEncrypt.getBytes());
} catch(NoSuchAlgorithmException e) {
e.printStackTrace();
} catch(NoSuchPaddingException e) {
e.printStackTrace();
} catch(InvalidKeyException e) {
e.printStackTrace();
} catch(IllegalBlockSizeException e) {
e.printStackTrace();
} catch(BadPaddingException e) {
e.printStackTrace();
}
return byteCipherText;
}
public String decryptText(byte decryptedKey, byte encryptedText) {
String decryptedPlainText = null;
try {
SecretKey originalKey = new SecretKeySpec(decryptedKey, 0, decryptedKey.length, mEncryptionAlgorithm);
Cipher aesCipher2 = Cipher.getInstance(mEncryptionAlgorithm);
aesCipher2.init(Cipher.DECRYPT_MODE, originalKey);
byte bytePlainText = aesCipher2.doFinal(encryptedText);
decryptedPlainText = new String(bytePlainText);
} catch(NoSuchAlgorithmException e) {
e.printStackTrace();
} catch(NoSuchPaddingException e) {
e.printStackTrace();
} catch(InvalidKeyException e) {
e.printStackTrace();
} catch(IllegalBlockSizeException e) {
e.printStackTrace();
} catch(BadPaddingException e) {
e.printStackTrace();
}
return decryptedPlainText;
}
}
package eds;
import javax.crypto.SecretKey;
public class Main {
public static void main(String args) {
EncDecSymmetric sed = new EncDecSymmetric();
sed.generateSymmetricKey();
byte secretKeyByteArray = sed.getSecretKeyAsByteArray();
System.out.println("secret key: '" + EncDecSymmetric.keyToNumber(secretKeyByteArray).toString() + "'");
String plainText = "Hello World, Symmetric Encryption style";
System.out.println("plainText: '" + plainText + "'");
byte encryptedText = sed.encryptText(plainText);
System.out.println("encrypted text: '" + EncDecSymmetric.keyToNumber(encryptedText).toString() + "'");
String decryptedText = sed.decryptText(secretKeyByteArray, encryptedText);
System.out.println("decrypted text: '" + decryptedText + "'");
}
}
package edpkpk;
import java.math.BigInteger;
import java.security.InvalidKeyException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.Base64;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
public class EncDecPublicKeyPrivateKey
{
// key encryption algorithms supported - RSA, Diffie-Hellman, DSA
// key pair generator - RSA: keyword - RSA, key size: 1024, 2048
// key pair generator - Diffie-Hellman: keyword i DiffieHellman, key size - 1024
// key pair generator - DSA: keyword - DSA, key size: 1024
// NOTE: using asymmetric algorithms other than RSA needs to be worked out
protected static String DEFAULT_ENCRYPTION_ALGORITHM = "RSA";
protected static int DEFAULT_ENCRYPTION_KEY_LENGTH = 1024;
protected static String DEFAULT_TRANSFORMATION = "RSA/ECB/PKCS1Padding";
protected String mEncryptionAlgorithm, mTransformation;
protected int mEncryptionKeyLength;
protected PublicKey mPublicKey;
protected PrivateKey mPrivateKey;
EncDecPublicKeyPrivateKey()
{
mEncryptionAlgorithm = EncDecPublicKeyPrivateKey.DEFAULT_ENCRYPTION_ALGORITHM;
mEncryptionKeyLength = EncDecPublicKeyPrivateKey.DEFAULT_ENCRYPTION_KEY_LENGTH;
mTransformation = EncDecPublicKeyPrivateKey.DEFAULT_TRANSFORMATION;
mPublicKey = null;
mPrivateKey = null;
}
public static BigInteger keyToNumber(byte byteArray)
{
return new BigInteger(1, byteArray);
}
public String getEncryptionAlgorithm()
{
return mEncryptionAlgorithm;
}
public int getEncryptionKeyLength()
{
return mEncryptionKeyLength;
}
public String getTransformation()
{
return mTransformation;
}
public PublicKey getPublicKey()
{
return mPublicKey;
}
public byte getPublicKeyAsByteArray()
{
return mPublicKey.getEncoded();
}
public String getEncodedPublicKey()
{
String encodedKey = Base64.getEncoder().encodeToString(mPublicKey.getEncoded());
return encodedKey;
}
public PrivateKey getPrivateKey()
{
return mPrivateKey;
}
public byte getPrivateKeyAsByteArray()
{
return mPrivateKey.getEncoded();
}
public String getEncodedPrivateKey()
{
String encodedKey = Base64.getEncoder().encodeToString(mPrivateKey.getEncoded());
return encodedKey;
}
public byte encryptText(String text)
{
byte encryptedText = null;
try {
KeyPairGenerator kpg = KeyPairGenerator.getInstance(mEncryptionAlgorithm);
kpg.initialize(mEncryptionKeyLength);
KeyPair keyPair = kpg.generateKeyPair();
mPublicKey = keyPair.getPublic();
mPrivateKey = keyPair.getPrivate();
Cipher cipher = Cipher.getInstance(mTransformation);
cipher.init(Cipher.PUBLIC_KEY, mPublicKey);
encryptedText = cipher.doFinal(text.getBytes());
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return encryptedText;
}
public byte decryptText(byte encryptedText)
{
byte decryptedText = null;
try {
Cipher cipher = Cipher.getInstance(mTransformation);
cipher.init(Cipher.PRIVATE_KEY, mPrivateKey);
decryptedText = cipher.doFinal(encryptedText);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return decryptedText;
}
}
package edpkpk;PKU方法加密安全可靠,但是也有局限性,第一就是加密速度慢,第二必须使用块的形式加密,每个块的长度要小于密钥的长度。例如,使用密钥长度为1024的RSA算法,那么可加密的块最大长度为117个字符(块的长度=(密钥长度/64)-11)
public class Main
{
public static void encryptDecrypt(String plainText)
{
EncDecPublicKeyPrivateKey edpkpk = new EncDecPublicKeyPrivateKey();
//byte secretKeyByteArray = sed.getSecretKeyAsByteArray();
//System.out.println("secret key: '" + EncryptDecryptPublicKeyPrivateKey.keyToNumber(secretKeyByteArray).toString() + "'" );
System.out.println("plainText: '" + plainText + "'");
System.out.println("plainText size: '" + plainText.length() + "'");
System.out.println("encryption key length: '" + edpkpk.getEncryptionKeyLength() + "'");
System.out.println("encryption algorithm: '" + edpkpk.getEncryptionAlgorithm() + "'");
System.out.println("encryption transform: '" + edpkpk.getTransformation() + "'");
byte encryptedText = edpkpk.encryptText(plainText);
System.out.println("encrypted text: '" + EncDecPublicKeyPrivateKey.keyToNumber(encryptedText).toString() + "'" );
System.out.println("encrypted text length: '" + EncDecPublicKeyPrivateKey.keyToNumber(encryptedText).toString().length() + "'" );
System.out.println("public key: '" + EncDecPublicKeyPrivateKey.keyToNumber(edpkpk.getPublicKeyAsByteArray()).toString() + "'" );
System.out.println("public key length: '" + EncDecPublicKeyPrivateKey.keyToNumber(edpkpk.getPublicKeyAsByteArray()).toString().length() + "'" );
System.out.println("private key: '" + EncDecPublicKeyPrivateKey.keyToNumber(edpkpk.getPrivateKeyAsByteArray()).toString() + "'" );
System.out.println("private key length: '" + EncDecPublicKeyPrivateKey.keyToNumber(edpkpk.getPrivateKeyAsByteArray()).toString().length() + "'" );
String decryptedText = new String(edpkpk.decryptText(encryptedText));
System.out.println("decrypted text: '" + decryptedText + "'" );
System.out.println("decrypted text length: '" + decryptedText.length() + "'");
}
public static void main(String args)
{
String plainText1 = "Hello World, Public Key / Private Key style";
Main.encryptDecrypt(plainText1);
//System.out.println("----------------------------------------------------------------");
//String plainText2 = "Hello World, Public Key / Private Key style with a very loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooog piece of text";
//Main.encryptDecrypt(plainText2);
}
}
作者:恒一
说明:21CTO社区原创稿件,未经许可请勿转载。
本文为 @ 21CTO 创作并授权 21CTO 发布,未经许可,请勿转载。
内容授权事宜请您联系 webmaster@21cto.com或关注 21CTO 公众号。
该文观点仅代表作者本人,21CTO 平台仅提供信息存储空间服务。