一般加密原理是,由用户共有的公共密钥和传输数据的用户直接有的私有密钥组成。每次加密的时候,都是由一定算法随机生成一对密钥,用密钥加密所需加密的文件,用公钥加密密钥,然后将这些一起传输给目标用户,目标用户将数据处理后,又按照此密钥加密,然后传输回去。
这样的优点别人很难破解,网络上的数据无法定位,而且产生的密钥是随机的。
接下来这个例子就是java核心技术卷2高级特性安全性一章的最后一个例子。
import
java.io.
*
;
import
java.security.
*
;
import
javax.crypto.
*
;
import
javax.crypto.interfaces.
*
;
import
javax.crypto.spec.
*
;
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
public
class
RSATest
...
{
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
private static final int KEYSIZE = 512 ;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/** */ /**
* @param args
*/
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public static void main(String[] args) ... {
// TODO Auto-generated method stub
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
try ... {
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if (args[ 0 ].equals( " -genkey " )) ... {
KeyPairGenerator keyPair = KeyPairGenerator.getInstance( " RSA " );
SecureRandom random = new SecureRandom();
keyPair.initialize(KEYSIZE, random);
KeyPair keyP = keyPair.generateKeyPair();
ObjectOutputStream oos = new ObjectOutputStream( new FileOutputStream(args[ 1 ]));
oos.writeObject(keyP.getPublic());
oos.close();
oos = new ObjectOutputStream( new FileOutputStream(args[ 2 ]));
oos.writeObject(keyP.getPrivate());
oos.close();
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
} else if (args[ 0 ].equals( " -encrypt " )) ... {
KeyGenerator keygen = KeyGenerator.getInstance( " AES " );
SecureRandom random = new SecureRandom();
keygen.init(random);
SecretKey key = keygen.generateKey();
ObjectInputStream kin = new ObjectInputStream( new FileInputStream(args[ 3 ]));
Key publicKey = (Key)kin.readObject();
kin.close();
Cipher cipher = Cipher.getInstance( " RSA " );
System.out.println(cipher.getAlgorithm());
cipher.init(Cipher.WRAP_MODE,publicKey);
byte [] wrappedKey = cipher.wrap(key);
DataOutputStream out = new DataOutputStream( new FileOutputStream(args[ 2 ]));
out.writeInt(wrappedKey.length);
out.write(wrappedKey);
InputStream in = new FileInputStream(args[ 1 ]);
cipher = Cipher.getInstance( " AES " );
cipher.init(Cipher.ENCRYPT_MODE, key);
crypt(in,out,cipher);
in.close();
out.close();
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
} else ... {
DataInputStream in = new DataInputStream( new FileInputStream(args[ 1 ]));
int length = in.readInt();
byte []wrappedKey = new byte [length];
in.read(wrappedKey, 0 ,length);
ObjectInputStream keyIn = new ObjectInputStream( new FileInputStream(args[ 3 ]));
Key privateKey = (Key) keyIn.readObject();
keyIn.close();
Cipher cipher = Cipher.getInstance( " RSA " );
cipher.init(Cipher.UNWRAP_MODE, privateKey);
Key key = cipher.unwrap(wrappedKey, " AES " , Cipher.SECRET_KEY);
OutputStream out = new FileOutputStream(args[ 2 ]);
cipher = Cipher.getInstance( " AES " );
cipher.init(Cipher.ENCRYPT_MODE, key);
crypt(in,out,cipher);
in.close();
out.close();
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
} catch (Exception e) ... {
e.printStackTrace();
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
private static void crypt(InputStream in, OutputStream out, Cipher cipher) throws IOException,GeneralSecurityException ... {
// TODO Auto-generated method stub
int blockSize = cipher.getBlockSize();
int outputSize = cipher.getOutputSize(blockSize);
byte [] inBytes = new byte [blockSize];
byte [] outputBytes = new byte [outputSize];
int inLength = 0 ;
boolean more = true ;
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
while (more) ... {
inLength = in.read(inBytes);
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if (inLength == blockSize) ... {
int outLength = cipher.update(inBytes, 0 ,blockSize,outputBytes);
out.write(outputBytes, 0 , outLength);
} else
more = false ;
}
if (inLength > 0 )
outputBytes = cipher.doFinal(inBytes, 0 , inLength);
else
outputBytes = cipher.doFinal();
out.write(outputBytes);
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
}
运行的时候,先在eclipse里面运行的参数里加上-genkey public.key private.key
生成两个文件,就是用于加密的公钥和私钥。
然后运行的参数里面加上-encrypt textFile encryptedFile public.key对第二个参数对应的文件加密,第三个参数对应的就是生成的加密过的文件。
最后运行-decrypt encryptedFile decryptedFile private.key对文件进行解密。