睫晉姬

          java RSA 加解密

            import java.security.Key;

            import java.security.KeyFactory;

            import java.security.KeyPair;

            import java.security.KeyPairGenerator;

            import java.security.PrivateKey;

            import java.security.PublicKey;

            import java.security.interfaces.RSAPrivateKey;

            import java.security.interfaces.RSAPublicKey;

            import java.security.spec.PKCS8EncodedKeySpec;

            import java.security.spec.X509EncodedKeySpec;

            import javax.crypto.Cipher;

            import sun.misc.BASE64Decoder;

            import sun.misc.BASE64Encoder;

            public class RSACoder {

            /**

            * 得到公鑰

            * @param key 密鑰字符串(經(jīng)過(guò)base64編碼)

            * @throws Exception

            */

            public static PublicKey getPublicKey(String key) throws Exception {

            byte[] keyBytes;

            keyBytes = (new BASE64Decoder()).decodeBuffer(key);

            X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);

            KeyFactory keyFactory = KeyFactory.getInstance("RSA");

            PublicKey publicKey = keyFactory.generatePublic(keySpec);

            return publicKey;

            }

            /**

            * 得到私鑰

            * @param key 密鑰字符串(經(jīng)過(guò)base64編碼)

            * @throws Exception

            */

            public static PrivateKey getPrivateKey(String key) throws Exception {

            byte[] keyBytes;

            keyBytes = (new BASE64Decoder()).decodeBuffer(key);

            PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);

            KeyFactory keyFactory = KeyFactory.getInstance("RSA");

            PrivateKey privateKey = keyFactory.generatePrivate(keySpec);

            return privateKey;

            }
            /**

            * 得到密鑰字符串(經(jīng)過(guò)base64編碼)

            * @return

            */

            public static String getKeyString(Key key) throws Exception {

            byte[] keyBytes = key.getEncoded();

            String s = (new BASE64Encoder()).encode(keyBytes);

            return s;

            }

            public static void main(String[] args) throws Exception {

            KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");

            //密鑰位數(shù)

            keyPairGen.initialize(1024);

            //密鑰對(duì)

            KeyPair keyPair = keyPairGen.generateKeyPair();

            // 公鑰

            PublicKey publicKey = (RSAPublicKey) keyPair.getPublic();

            // 私鑰

            PrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();

            String publicKeyString = getKeyString(publicKey);

            System.out.println("public:\n" + publicKeyString);

            String privateKeyString = getKeyString(privateKey);

            System.out.println("private:\n" + privateKeyString);

            //加解密類(lèi)

            Cipher cipher = Cipher.getInstance("RSA");//Cipher.getInstance("RSA/ECB/PKCS1Padding");

            //明文

            byte[] plainText = "我們都很好!郵件:@sina.com".getBytes();

            //加密

            cipher.init(Cipher.ENCRYPT_MODE, publicKey);

            byte[] enBytes = cipher.doFinal(plainText);

            //通過(guò)密鑰字符串得到密鑰

            publicKey = getPublicKey(publicKeyString);

            privateKey = getPrivateKey(privateKeyString);

            //解密

            cipher.init(Cipher.DECRYPT_MODE, privateKey);

            byte[]deBytes = cipher.doFinal(enBytes);

            publicKeyString = getKeyString(publicKey);

            System.out.println("public:\n" +publicKeyString);

            privateKeyString = getKeyString(privateKey);

            System.out.println("private:\n" + privateKeyString);

            String s = new String(deBytes);

            System.out.println(s);

            }

            }

          posted on 2009-12-05 17:18 睫晉姬 閱讀(330) 評(píng)論(0)  編輯  收藏


          只有注冊(cè)用戶(hù)登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 建湖县| 昭通市| 四会市| 阿拉善左旗| 清水河县| 北票市| 江北区| 平阴县| 镇原县| 巩义市| 襄垣县| 北碚区| 疏勒县| 漳浦县| 泉州市| 濮阳县| 莲花县| 崇仁县| 昌吉市| 罗江县| 四会市| 连州市| 郴州市| 尉氏县| 宣化县| 大厂| 宜州市| 汝城县| 双辽市| 剑川县| 宁国市| 志丹县| 西乡县| 平湖市| 海口市| 灵丘县| 南宁市| 峨眉山市| 密山市| 泉州市| 汉川市|