Java8 ¤Ç½ñ¤­¤Ê¤ª¤·¤¿ ¢ª JavaSE RSA°Å¹æ Java8

RSA°Å¹æ³µÎ¬

RSA°Å¹æ¤Î´ðËÜŪ¤Ê¹Í¤¨Êý

  1. ʿʸ x ¤ò n¾è ¤·¤¿¤â¤Î¤ò Æó¤Ä¤ÎÁÇ¿ô¤ÎÁÈ(p,q)¤ÎÀÑ(p¡ßq)¤Ç³ä¤Ã¤¿Í¾¤ê¤ò°ìÍ÷ɽ¤Ë¤·¤Æ°Å¹æɽ¤òºî¤ë
    ¤³¤³¤Ç¤Ï (p,q)=(3,5) ¤òÎã¤Ë¹Í¤¨¤ë¡£
    matrix.png
     
  2. ¤³¤Îɽ¤ò¸«¤ë¤ÈÄê´üŪ¤Ë¸µ¤Îʿʸ¤¬¸½¤ì¤Æ¤¤¤ë¤³¤È¤¬Ê¬¤«¤ë(¥Ô¥ó¥¯¤ÎÎó)
    • n ¡ß LCM( p-1 , q-1 ) + 1 ¹Ô¤¬Ê¿Ê¸¤ÈƱ¤¸¤Ë¤Ê¤Ã¤Æ¤¤¤ë
    • LCM (Least Common Multiple¡£ºÇ¾®¸øÇÜ¿ô)¡£LCM(3-1,5-1)=4
  3. °Å¹æ¤Ø¤Î±þÍÑ
    • ʿʸ¤òE¾è¤·¤Æ°Å¹æ²½¤·¡¢°Å¹æʸ¤òD¾è¤·¤¿¤é¥Ô¥ó¥¯¤ÎÎó¤Ë¾è¤ë¤è¤¦¤Ê(E,D)¤ÎÁȤò¸«¤Ä¤±¤ë
    • ¸°¥»¥Ã¥È = (E,D)
    • ¤³¤Î¸°¥»¥Ã¥È¤ÏµÕ¤Ë»È¤¦¤³¤È¤â¤Ç¤­¤ë
      • ʿʸ¤òD¾è¤·¤Æ°Å¹æ²½¤·¡¢°Å¹æʸ¤òE¾è¤·¤Æ¤â¥Ô¥ó¥¯¤ÎÎó¤Ë¾è¤ë

¸ø³«¸°¤ÈÈëÌ©¸°

  1. ¸°¥»¥Ã¥È¤Îµá¤áÊý
    rsaGetKey.png
     
  2. ¸°¥»¥Ã¥È¤ÎÍøÍÑÎã
    • (p,q)=(3,5) ¤Î¤È¤­¡¢(E,D)=(3,7)¤ÏÍ­¸ú¤Ê¸°¥»¥Ã¥È¡£E=3 ¤òÈëÌ©¸°¡¢D=7 ¤ò¸ø³«¸°¤È¸Æ¤Ö¤È¤¹¤ë¤È
    1. ÈëÌ©¸°(3) ¤Ç°Å¹æ²½¤·¤Æ¡¢¸ø³«¸°(7) ¤ÇÉü¹æ²½
    2. ¸ø³«¸°(7) ¤Ç°Å¹æ²½¤·¤Æ¡¢ÈëÌ©¸°(3) ¤ÇÉü¹æ²½
      decrypt.png
       
    3. ³Î¤«¤Ë¡¢3 ¤òÈëÌ©¸°(3)¤Ç°Å¹æ²½¤¹¤ë¤È 12 ¤Ë¤Ê¤ê¡¢12 ¤ò¸ø³«¸°(7)¤ÇÉü¹æ²½¤¹¤ë¤È 3 ¤ËÌá¤Ã¤Æ¤¤¤ë
      [source] [reload]

RSA°Å¹æ¤Ç°Å¹æ²½¤Ç¤­¤ëʿʸ¤ÎŤµ

  1. RSA°Å¹æ¤Ï¡¢Ê¿Ê¸¤ò n¾è ¤·¤Æ m ¤Ç³ä¤Ã¤¿Í¾¤ê¤òÍѤ¤¤Æ°Å¹æ²½¤¹¤ë¤Î¤Ç¡¢m¤è¤êÂ礭¤¤Ãͤò°Å¹æ²½¤¹¤ë¤³¤È¤Ï¤Ç¤­¤Ê¤¤
    • RSA°Å¹æ¤Ç¤Ï¡¢°Å¹æ²½¤·¤¿¤¤Ê¸»úÎó¤ò°ì¤Ä¤ÎÃͤȤ·¤Æ°·¤¦¡£
      ABCD = 0x41424344 = 1094861636
    • ¸°Ä¹¤¬ 64bit ¤Î¾ì¹ç¤Ë¤Ï¡¢¤ª¤ª¤à¤Í
      0xFFFFFFFF = 4294967295
      ¤Þ¤Ç¤ÎÃͤò°Å¹æ²½¤¹¤ë¤³¤È¤¬¤Ç¤­¤ë¡£(Àµ³Î¤Ë¤Ï 0xFFFFFFFF °Ê²¼¤Ç°ìÈÖÂ礭¤¤ÁÇ¿ô¤ÎÀѤޤÇ)
  2. RSA-???? ¤Î¸°Ä¹
    Ä̾ï 11byte ¤ò Padding ¤Ë»È¤¦¤Î¤Ç¡¢????¤Ç¼¨¤µ¤ì¤¿ bit Ťµ¤«¤é¡¢88bit(11byte)¤ò°ú¤¤¤¿¤â¤Î¤¬¸°Ä¹
    ¸°Ä¹(Padding´Þ¤à)ʿʸ¤ÎºÇÂçĹ(=¼Â¼ÁŪ¤Ê¸°Ä¹)
    RSA-512512bit(64byte)53byte
    RSA-10241024bit(128byte)117byte
    RSA-20482048bit(256byte)245byte
  3. Ĺʸ¤ò°Å¹æ²½¤¹¤ë¤Ë¤Ï
    • Ĺʸ¤ò n byte Ëè¤Ë¶èÀڤäưŹ沽¤¹¤ì¤Ð°Å¹æ²½¤Ç¤­¤Ê¤¤¤³¤È¤â¤Ê¤¤¤¬¡¢ÉáÄ̤Ϥ½¤¦¤¤¤¦¤³¤È¤Ï¤·¤Ê¤¤¡£
    • Ä̾ï¤Ï¡¢¥³¥ó¥Æ¥ó¥ÄËÜÂΤ϶¦Ä̸°¤Ç°Å¹æ²½¤·¡¢¶¦Ä̸°¤òRSA¤Ç°Å¹æ²½¤·¤ÆÁ÷¤ë¡£
      ================(Á÷¿®Â¦)===========|     |===========(¼õ¿®Â¦)==================
      [Ĺʸ]--(°Å¹æ²½)------------>[****]¡Ä¡Ä¡Ä>[****]-------------(Éü¹æ²½)-->[Ĺʸ]
                 ¢¬­¡                    |     |                      ¢¬­¤
              [¶¦Ä̸°]--(°Å¹æ²½)-->[????]¡Ä¡Ä¡Ä>[????]--(Éü¹æ²½)-->[¶¦Ä̸°]
            (Ä̾ï¤ÏÍð¿ô)   ¢¬­¢          |     |           ¢¬­£
                        [¸ø³«¸°]         |     |        [ÈëÌ©¸°]
      ===================================|     |======================================
      • ­¡Á÷¿®Â¦¡§Íð¿ô¤ò¶¦Ä̸°¤È¤·¤Æ¡¢Ä¹Ê¸¤ò°Å¹æ²½
      • ­¢Á÷¿®Â¦¡§¶¦Ä̸°¤ò¸ø³«¸°¤Ç°Å¹æ²½
      • ­£¼õ¿®Â¦¡§Á÷¤é¤ì¤Æ¤­¤¿¶¦Ä̸°[????]¤òÉü¹æ²½
      • ­¤¼õ¿®Â¦¡§Éü¹æ²½¤·¤¿¶¦Ä̸°¤Ç¡¢Á÷¤é¤ì¤Æ¤­¤¿Ä¹Ê¸[****]¤òÉü¹æ²½

ÈëÌ©¸°¤ÎÊݸÊýË¡

  1. PKCS(Public Key Cryptography Standard)
  2. DER/PEM
    • ¸°¤ÎÊݸÊýË¡
      DERDistinguished Encoding Rules¥Ð¥¤¥Ê¥ê·Á¼°
      PEMPrivacy Enhanced MailDER¤òBase64¤Ç¥Æ¥­¥¹¥È²½¤·¤¿¤â¤Î
  3. Îã
    • PKCS#8 ¤ò DER ¤ÇÊݸ
    • PKCS#12 ¤ò PEM ¤ÇÊݸ

¸ø³«¸°¤ÎÊݸÊýË¡

  1. X.509¤ÎÅоì¿Íʪ
    ÍøÍѼԸø³«¸°¤ÎÍøÍѼÔ
    ¥µ¡¼¥Ó¥¹Ä󶡼Ըø³«¸°¤ÎÄ󶡼Ô
    CA(Certification Authority)¿®Íê¤ÎÃÖ¤±¤ëµ¡´Ø
    • ÍøÍѼԤϡ¢¥µ¡¼¥Ó¥¹Ä󶡼ԤÎÄ󶡤¹¤ë¸ø³«¸°¤ò»È¤¤¤¿¤¤¤¬¡¢¥µ¡¼¥Ó¥¹Ä󶡼Ԥò¿®Íꤷ¤ÆÎɤ¤¤«Ê¬¤«¤é¤Ê¤¤
    • ÍøÍѼԤϡ¢CA¤ò¿®Íꤷ¤Æ¤¤¤ë
  2. X.509 ¤Ï¡¢¸ø³«¸° + CA¤Î¤ªËÏÉÕ¤­
  3. X.509¤Î¹½Â¤
    ¥Ñ¥é¥á¡¼¥¿Îã
    ­¡CA ½ð̾½ñ¥Ð¡¼¥¸¥ç¥óV1
    ­¢CA ¾ÚÌÀ½ñ¥·¥ê¥¢¥ëÈÖ¹æÀ°¿ô
    ­£CA ½ð̾¥¢¥ë¥´¥ê¥º¥àSHA1 RSA
    ­¤CA ̾Foo Co.LTD
    ­¥Í­¸ú´ü¸Â2031/12/31
    ­¦¥µ¡¼¥Ó¥¹Ä󶡼ÔBar Co.LTD
    ­§¸ø³«¸°¥¢¥ë¥´¥ê¥º¥àRSA
    ­¨¸ø³«¸°
    ­©½ð̾¥¢¥ë¥´¥ê¥º¥à­£¤ÈƱ¤¸(SHA1¤Ê¤É)
    ­ª½ð̾encrypt(CAÈëÌ©¸°, digest(­©,­¡­¢­£­¤­¥­¦­§­¨))
  4. ¾ÚÌÀ½ñ¤Î³ÎǧÊýË¡
    • ­ª¤Ï¡¢­¡¡Á­¨¤ò­©¤Ç¥À¥¤¥¸¥§¥¹¥È²½¤·¤¿¤â¤Î¤ò CA ¤ÎÈëÌ©¸°¤Ç°Å¹æ²½¤·¤¿¤â¤Î¤¬µ­½Ò¤µ¤ì¤Æ¤¤¤ë
    • ÍøÍѼÔ¦¤Ç¤Ï¡¢­ª¤ò CA ¤Î¸ø³«¸°¤ÇÉü¹æ²½¤·¤¿¤â¤Î¤È¡¢­¡¡Á­¨¤ò­©¤Ç¥À¥¤¥¸¥§¥¹¥È²½¤·¤¿¤â¤Î¤¬°ìÃפ¹¤ì¤Ð¡¢¤³¤Î¾ÚÌÀ½ñ¤Ï¡¢³Î¤«¤Ë CA ¤¬¤ªËÏÉÕ¤­¤òÍ¿¤¨¤¿¤â¤Î¤À¤Èʬ¤«¤ë
      decrypt(CA¸ø³«¸°,­ª) =? digest(­©,­¡­¢­£­¤­¥­¦­§­¨)

Java SE ¤Ç¤Î RSA°Å¹æ

Java SE 5.0 (=Java2 1.5) ¤«¤é¡¢RSA°Å¹æ¤Î¼ÂÁõ*1¤¬É¸½à¤ÇÁȤ߹þ¤Þ¤ì¤ë¤è¤¦¤Ë¤Ê¤ê¤Þ¤·¤¿¡£
¤Û¤«¤Î°Å¹æ¤ÈƱÍͤˡ¢JCA¤Î¼ê½ç¤Ç°Å¹æ²½¡¦Éü¹æ²½¤·¤Æ¤¤¤ë¤Î¤Ç¡¢main() ¤«¤é½ç¤Ë¸«¤Æ¤¤¤±¤Ðʬ¤«¤ë¤È»×¤¤¤Þ¤¹¡£
Java 1.4°ÊÁ°¤Ç RSA°Å¹æ ¤ò°·¤¦¤¿¤á¤Ë¤Ï¡¢RSA°Å¹æ¤Î¼ÂÁõ¤òÊÌÅÓ½àÈ÷¤¹¤ëɬÍפ¬¤¢¤ê¤Þ¤¹( http://www.bouncycastle.org/ ¤òÍøÍѤ¹¤ë¤³¤È¤¬¤Ç¤­¤Þ¤¹)¡£

package com.snail.example;

import sun.misc.HexDumpEncoder;

import java.security.InvalidKeyException;
import java.security.Key;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.interfaces.RSAKey;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;


/**
 * RSA Examination.
 *
 * @author kagyuu
 * @version $Revision$
 */
public class RSAExam {
  /** sources of the example document */
  private static String[] WISDOMS = new String[] {
      "Insanity: doing the same thing over and over again and expecting different results.",
      "Albert Einstein",
      "Happiness is nothing more than good health and a bad memory.",
      "Albert Schweitzer",
      "Our problems are man-made, therefore they may be solved by man.", "J.F.K",
      "Obstacles are those frightful things you see when you take your eyes off your goal. ",
      "Henry Ford"
    };

  /** Example plain document */
  private static String EXAMPLE = WISDOMS[0] + WISDOMS[1];

  /** KEY LENGTH */
  private static int KEY_LENGTH = 1024;

  /**
   * Main Routine.
   *
   * @param args Commandline Argument
   */
  public static void main(String[] args) {
    // Create Key Pair
    KeyPair keyPair = createKeyPair();

    System.out.println("Private Key: "
      + ((RSAKey) keyPair.getPrivate()).getModulus().bitLength() + "bit.");
    System.out.println("Public  Key: "
      + ((RSAKey) keyPair.getPublic()).getModulus().bitLength() + "bit.");

    // Raw data
    System.out.println("\nRAW DATA");

    byte[] rawText = EXAMPLE.getBytes();
    hexDump(rawText);

    try {
      // Encrypt data
      byte[] encryptedText = encrypt(keyPair.getPublic(), rawText);
      System.out.println("\nENCRYPTED DATA");
      hexDump(encryptedText);

      // Decrypt data
      byte[] decryptedText = decrypt(keyPair.getPrivate(), encryptedText);
      System.out.println("\nDECRYPTED DATA");
      hexDump(decryptedText);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  /**
   * Create Key Pair.
   *
   * @return key pair
   */
  private static KeyPair createKeyPair() {
    KeyPair keyPair = null;

    try {
      KeyPairGenerator keygen = KeyPairGenerator.getInstance("RSA");
      keygen.initialize(KEY_LENGTH);

      keyPair = keygen.generateKeyPair();

      System.out.print("PUBLIC KEY (");
      System.out.print(keyPair.getPublic().getAlgorithm() + " ");
      System.out.println(keyPair.getPublic().getFormat() + ")");
      hexDump(keyPair.getPublic().getEncoded());

      System.out.print("\nPRIVATE KEY (");
      System.out.print(keyPair.getPrivate().getAlgorithm() + " ");
      System.out.println(keyPair.getPrivate().getFormat() + ")");
      hexDump(keyPair.getPrivate().getEncoded());
    } catch (NoSuchAlgorithmException e) {
      // should not happen
      e.printStackTrace();
      System.exit(-1);
    }

    return keyPair;
  }

  /**
   * Decrypt data.
   *
   * @param key decrypt key
   * @param data encrypted data
   *
   * @return decrypted data
   *
   * @throws InvalidKeyException key is wrong
   * @throws IllegalBlockSizeException data is wrong ( too long )
   */
  private static byte[] decrypt(Key key, byte[] data)
    throws InvalidKeyException, IllegalBlockSizeException {
    byte[] decrypted = null;

    try {
      Cipher cipher = Cipher.getInstance("RSA");
      cipher.init(Cipher.DECRYPT_MODE, key);

      decrypted = cipher.doFinal(data);
    } catch (NoSuchAlgorithmException e) {
      // should not happen
      e.printStackTrace();
      System.exit(-1);
    } catch (NoSuchPaddingException e) {
      // should not happen
      e.printStackTrace();
      System.exit(-1);
    } catch (BadPaddingException e) {
      // should not happen
      e.printStackTrace();
      System.exit(-1);
    }

    return decrypted;
  }

  /**
   * Encrypt data.
   *
   * @param key encrypt key
   * @param data data
   *
   * @return encrypted data
   *
   * @throws InvalidKeyException key is wrong
   * @throws IllegalBlockSizeException data is wrong ( too long )
   */
  private static byte[] encrypt(Key key, byte[] data)
    throws InvalidKeyException, IllegalBlockSizeException {
    byte[] encrypted = null;

    try {
      Cipher cipher = Cipher.getInstance("RSA");
      cipher.init(Cipher.ENCRYPT_MODE, key);

      encrypted = cipher.doFinal(data);
    } catch (NoSuchAlgorithmException e) {
      // should not happen
      e.printStackTrace();
      System.exit(-1);
    } catch (NoSuchPaddingException e) {
      // should not happen
      e.printStackTrace();
      System.exit(-1);
    } catch (BadPaddingException e) {
      // should not happen
      e.printStackTrace();
      System.exit(-1);
    }

    return encrypted;
  }

  /**
   * Dump Byte Array.
   *
   * @param dump byte array.
   */
  private static void hexDump(byte[] dump) {
    HexDumpEncoder hexDump = new HexDumpEncoder();
    System.out.println(hexDump.encode(dump));
  }
}

¼Â¹Ô·ë²Ì

PUBLIC KEY (RSA X.509)
0000: 30 81 9F 30 0D 06 09 2A   86 48 86 F7 0D 01 01 01  0..0...*.H......
0010: 05 00 03 81 8D 00 30 81   89 02 81 81 00 B3 A7 70  ......0........p
0020: 33 E6 77 02 13 5B AE E5   C6 97 A0 95 8B 4F B9 11  3.w..[.......O..
0030: FE 54 22 97 9F 94 AD ED   CB 7D 85 C5 40 88 46 FF  .T".........@.F.
0040: 59 B8 AF 69 5F AC 06 67   F9 F0 6D 4D EE 2A 25 71  Y..i_..g..mM.*%q
0050: 90 96 B2 50 1E 23 71 22   56 C9 7B 0D 04 29 44 AD  ...P.#q"V....)D.
0060: 62 DF ED 08 D6 64 24 C6   E0 B0 ED D8 AA 8C DA A0  b....d$.........
0070: C0 23 83 20 4E A4 8F 33   AB EF 0E 9B 2F CA AA B2  .#. N..3..../...
0080: 7C 96 13 10 F2 16 5B D7   D2 48 42 8D 0F 7A 5B AC  ......[..HB..z[.
0090: B9 71 49 55 81 03 B4 9B   82 68 95 83 A3 02 03 01  .qIU.....h......
00A0: 00 01 

PRIVATE KEY (RSA PKCS#8)
0000: 30 82 02 77 02 01 00 30   0D 06 09 2A 86 48 86 F7  0..w...0...*.H..
0010: 0D 01 01 01 05 00 04 82   02 61 30 82 02 5D 02 01  .........a0..]..
0020: 00 02 81 81 00 B3 A7 70   33 E6 77 02 13 5B AE E5  .......p3.w..[..
0030: C6 97 A0 95 8B 4F B9 11   FE 54 22 97 9F 94 AD ED  .....O...T".....
0040: CB 7D 85 C5 40 88 46 FF   59 B8 AF 69 5F AC 06 67  ....@.F.Y..i_..g
0050: F9 F0 6D 4D EE 2A 25 71   90 96 B2 50 1E 23 71 22  ..mM.*%q...P.#q"
0060: 56 C9 7B 0D 04 29 44 AD   62 DF ED 08 D6 64 24 C6  V....)D.b....d$.
0070: E0 B0 ED D8 AA 8C DA A0   C0 23 83 20 4E A4 8F 33  .........#. N..3
0080: AB EF 0E 9B 2F CA AA B2   7C 96 13 10 F2 16 5B D7  ..../.........[.
0090: D2 48 42 8D 0F 7A 5B AC   B9 71 49 55 81 03 B4 9B  .HB..z[..qIU....
00A0: 82 68 95 83 A3 02 03 01   00 01 02 81 80 53 23 C4  .h...........S#.
00B0: B8 30 0A 99 2A 4E 3E A5   55 7F 6C 30 68 B5 2F 47  .0..*N>.U.l0h./G
00C0: 95 74 68 41 D4 F7 20 7A   B0 B6 59 46 CC B3 84 6F  .thA.. z..YF...o
00D0: BD 9C CB 88 AF 5E D3 E4   20 75 D4 8F C1 0D 3E 20  .....^.. u....> 
00E0: CD 32 00 39 35 89 5F 5C   ED DE F6 14 32 37 2D B6  .2.95._\....27-.
00F0: 5D EE 44 7A 3E E4 B3 8E   55 0A BB 1E 3B 69 E1 E7  ].Dz>...U...;i..
0100: FF 37 CA 0F 54 0A 3B A8   A0 48 8B 40 0E 78 DC 61  .7..T.;..H.@.x.a
0110: 92 62 88 AC 68 C9 7B 8D   8F 0E F7 24 FA B2 C6 C3  .b..h......$....
0120: 37 37 72 EC 6A EF 5A AD   B1 F3 B5 0D 51 02 41 00  77r.j.Z.....Q.A.
0130: DE 0F 9F 50 A8 BA 84 00   72 89 33 CF 93 29 97 A5  ...P....r.3..)..
0140: 4D 23 4C 54 68 EF DA 77   50 E9 F1 DB C7 0F 51 DB  M#LTh..wP.....Q.
0150: BE 2A 95 BE 81 09 25 DF   C1 CD 26 D2 F1 4F 38 05  .*....%...&..O8.
0160: 0E 1B C6 1C D5 2A F1 CE   25 92 40 4E 31 DB CD 0B  .....*..%.@N1...
0170: 02 41 00 CF 1C 98 F3 39   65 9D 6D D1 A5 AD 83 E2  .A.....9e.m.....
0180: 84 FF CB 51 A6 6A 5D A2   70 D7 89 4D D2 EE 66 E6  ...Q.j].p..M..f.
0190: 00 B7 5F 78 4A 4E A3 4A   50 FD 6F 50 8F E1 95 F3  .._xJN.JP.oP....
01A0: C7 3A CA 8F E4 1D B2 45   8C 8D 0F 45 4C EE 5A DE  .:.....E...EL.Z.
01B0: 7C 52 C9 02 40 42 E1 DB   AF 34 92 B0 CD 5D B1 FB  .R..@B...4...]..
01C0: 5B EA CC 1D 33 99 6F 27   3F 49 37 EC CA 01 21 E5  [...3.o'?I7...!.
01D0: 49 B7 C3 E2 78 D7 E7 05   A1 12 46 4A 15 01 CF 23  I...x.....FJ...#
01E0: 81 67 52 DF E1 8D E5 49   9F 47 89 1E B9 63 51 90  .gR....I.G...cQ.
01F0: 72 BC CB A2 0F 02 41 00   C4 4F 5F EE 52 FE 2D 1D  r.....A..O_.R.-.
0200: 55 B5 0B 65 D1 C4 AC CC   64 05 30 16 B6 8C EC CF  U..e....d.0.....
0210: 79 53 B0 A3 19 E9 9D E6   41 17 75 B8 33 0D D7 5A  yS......A.u.3..Z
0220: B6 6A DA B1 0E 93 4D F7   88 31 F9 AF EB 5A 50 AE  .j....M..1...ZP.
0230: 23 CA CA B6 98 E2 10 79   02 41 00 A1 15 83 7A 5B  #......y.A....z[
0240: D9 A7 66 49 E0 72 C3 CC   82 8C EC CD D4 E9 0C 91  ..fI.r..........
0250: DE F1 AC 50 39 DB FC FC   65 F8 96 86 20 43 22 9F  ...P9...e... C".
0260: 6C 1B 49 08 61 25 5E D8   EE F6 C9 45 8D D8 7C 84  l.I.a%^....E....
0270: 01 5C 8A D5 F1 1B 64 42   75 C8 7C 
Private Key: 1024bit.
Public  Key: 1024bit.

RAW DATA
0000: 49 6E 73 61 6E 69 74 79   3A 20 64 6F 69 6E 67 20  Insanity: doing 
0010: 74 68 65 20 73 61 6D 65   20 74 68 69 6E 67 20 6F  the same thing o
0020: 76 65 72 20 61 6E 64 20   6F 76 65 72 20 61 67 61  ver and over aga
0030: 69 6E 20 61 6E 64 20 65   78 70 65 63 74 69 6E 67  in and expecting
0040: 20 64 69 66 66 65 72 65   6E 74 20 72 65 73 75 6C   different resul
0050: 74 73 2E 41 6C 62 65 72   74 20 45 69 6E 73 74 65  ts.Albert Einste
0060: 69 6E 

ENCRYPTED DATA
0000: 26 30 89 62 52 AC 90 6F   1B 7C 87 06 B2 72 AE 85  &0.bR..o.....r..
0010: DC 40 D0 40 5F B5 47 54   59 D2 6E 40 C0 B3 5D B9  .@.@_.GTY.n@..].
0020: FE E1 F2 20 53 A9 36 C6   5F 7A FA 31 78 5D 29 79  ... S.6._z.1x])y
0030: 93 5F 1D BB 23 10 FA F0   53 B2 38 A4 C2 6D 6A D0  ._..#...S.8..mj.
0040: C6 C8 D6 F0 08 24 EB 52   2B 43 23 18 C3 17 F8 A8  .....$.R+C#.....
0050: 3B 2E F7 D1 90 A4 11 0B   A5 43 49 BA 6E 9C 94 AA  ;........CI.n...
0060: B0 3C EC B4 95 BB 67 9C   48 DB D1 83 CC 83 56 41  .<....g.H.....VA
0070: AF B5 65 31 2A 07 79 AB   1F 7E 03 3B C7 A9 95 2A  ..e1*.y....;...*

DECRYPTED DATA
0000: 49 6E 73 61 6E 69 74 79   3A 20 64 6F 69 6E 67 20  Insanity: doing 
0010: 74 68 65 20 73 61 6D 65   20 74 68 69 6E 67 20 6F  the same thing o
0020: 76 65 72 20 61 6E 64 20   6F 76 65 72 20 61 67 61  ver and over aga
0030: 69 6E 20 61 6E 64 20 65   78 70 65 63 74 69 6E 67  in and expecting
0040: 20 64 69 66 66 65 72 65   6E 74 20 72 65 73 75 6C   different resul
0050: 74 73 2E 41 6C 62 65 72   74 20 45 69 6E 73 74 65  ts.Albert Einste
0060: 69 6E 

ʿʸ¤¬¸°Ä¹¤è¤ê¤âŤ¤¤È¤­

¸°Ä¹¤¬ 1024bit ¤Î¤È¤­¤Ë¤Ï¡¢117byte ¤Þ¤Ç¤Îʿʸ¤·¤«°Å¹æ²½¤Ç¤­¤Þ¤»¤ó¡£(cf.RSA-???? ¤Î¸°Ä¹)

¡Ä
Private Key: 1024bit.
Public  Key: 1024bit.

RAW DATA
0000: 49 6E 73 61 6E 69 74 79   3A 20 64 6F 69 6E 67 20  Insanity: doing 
0010: 74 68 65 20 73 61 6D 65   20 74 68 69 6E 67 20 6F  the same thing o
0020: 76 65 72 20 61 6E 64 20   6F 76 65 72 20 61 67 61  ver and over aga
0030: 69 6E 20 61 6E 64 20 65   78 70 65 63 74 69 6E 67  in and expecting
0040: 20 64 69 66 66 65 72 65   6E 74 20 72 65 73 75 6C   different resul
0050: 74 73 2E 41 6C 62 65 72   74 20 45 69 6E 73 74 65  ts.Albert Einste
0060: 69 6E 48 61 70 70 69 6E   65 73 73 20 69 73 20 6E  inHappiness is n
0070: 6F 74 68 69 6E 67 20 6D   6F 72 65 20 74 68 61 6E  othing more than
0080: 20 67 6F 6F 64 20 68 65   61 6C 74 68 20 61 6E 64   good health and
0090: 20 61 20 62 61 64 20 6D   65 6D 6F 72 79 2E 41 6C   a bad memory.Al
00A0: 62 65 72 74 20 53 63 68   77 65 69 74 7A 65 72

javax.crypto.IllegalBlockSizeException: Data must not be longer than 117 bytes
	at com.sun.crypto.provider.RSACipher.a(DashoA13*..)
	at com.sun.crypto.provider.RSACipher.engineDoFinal(DashoA13*..)
	at javax.crypto.Cipher.doFinal(DashoA13*..)
	at com.snail.example.RSAExam.encrypt(RSAExam.java:165)
	at com.snail.example.RSAExam.main(RSAExam.java:64)

Open SSL ¤Çºî¤Ã¤¿¸°¥Ú¥¢¤ò»È¤¦

ÈëÌ©¸°(PKCS#8)¤È¸ø³«¸°(X.509)¤òºî¤ë

  1. ¿½ÀÁ½ñ¤ÎºîÀ®
    kagyuu@grape:~> /usr/share/ssl/misc/CA.pl -newreq-nodes
    Generating a 1024 bit RSA private key
    ..++++++
    ....++++++
    writing new private key to 'newreq.pem'
    -----
    You are about to be asked to enter information that will be incorporated
    into your certificate request.
    What you are about to enter is what is called a Distinguished Name or a DN.
    There are quite a few fields but you can leave some blank
    For some fields there will be a default value,
    If you enter '.', the field will be left blank.
    -----
    Country Name (2 letter code) [AU]:JP
    State or Province Name (full name) [Some-State]:Musashi
    Locality Name (eg, city) []:Edo
    Organization Name (eg, company) [Internet Widgits Pty Ltd]:ISEYA LTD
    Organizational Unit Name (eg, section) []:Web Admin
    Common Name (eg, YOUR name) []:Saemon
    Email Address []:saemon@ise.co.jp
    
    Please enter the following 'extra' attributes
    to be sent with your certificate request
    A challenge password []:
    An optional company name []:
    Request (and private key) is in newreq.pem
  2. ¿½ÀÁ½ñ¤«¤éÈëÌ©¸°¤ò¼è¤ê½Ð¤¹
    kagyuu@grape:~> openssl rsa -in newreq.pem -out key.prv
    writing RSA key
    kagyuu@grape:~> openssl pkcs8 -topk8 -in key.prv -out key.pkcs8 -nocrypt
    kagyuu@grape:~> cat key.pkcs8
    -----BEGIN PRIVATE KEY-----
    MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBANlpV2MggGPPSBMU
    Ht0myHcLCS1PfJ+Iqrn8+6a/uq1//X8Z/a+/64RoOPAowL2z3XTp1ylj9KGh9uIv
    QIZzcRtHF8tVubY3np9mN5htWzHlDStF7YTb0ypJlpKD9Oon9LhlXxN1hMe9HTuA
    AlH67SAUJYk6n03cRzE7ZO2VqjQrAgMBAAECgYABOxJukjmH1x2OVhfSoqyR1/Mm
    WYPYF1r4EJGQIRe6cbYKqSgBfbGknz5rRZ/JsyH2zngUWOeCnrsaDrBr0m16emCZ
    8M+MEd6XxdtXH0WMKstMA9ZQLWkLKkugljOT73Ud1CVTO7eo2HOixU3m5qei0gte
    NtgOrt3M+Zw2KbYxuQJBAPoKg76m0hAW3FzUD5+MBZS54BJpFADpvlY9Gb9c8oY7
    dfruo5isqlo+JCQoft1v9iX0RZV4z8c9yX6Gb/jA7T0CQQDel8FwySQKF4jDnjOC
    Il0R5xdbT/0fEm0YwQ6fN/WJiXM6B/s4AHAc2Lo/GHm3rzUw/ra7u91bxpT++6Db
    dQ2HAkEAsN81OGzbFWSGdufPMWYkIIOnnH5WFxtBd9F64CIIsKlpGJssgHhLfNCO
    UkGPFGb5sPltK4GtQNxVJzpEBKu8ZQJAClGCmBXa49TfdCH7RUcOhWvu+z6j/zK9
    d+5LHTpc2XlTrBb+oEP07oFlcNNToR4wGwtTsTQuOLjl40QK2uF8NQJAN++aX5bf
    xhPw7fu7KbSc6pvoj8NS7hE7WD911tkBnR2ooGH8of3N3rq0qEzB6t7d1HOfNXmN
    gR95xd2jgU9NJw==
    -----END PRIVATE KEY-----
    kagyuu@grape:~>
    "-nocrypt"¤Ë¤·¤Ê¤¤¤È¡¢Java(PKCS8EncodedKeySpec?)¤«¤éÆɤá¤Ê¤¤¤è¤¦¤À
  3. ¿½ÀÁ½ñ¤«¤é¸ø³«¸°¤ò¼è¤ê½Ð¤¹
    kagyuu@grape:~> openssl rsa -in newreq.pem -pubout -out key.pub.x509
    writing RSA key
    kagyuu@grape:~> cat key.pub.x509
    -----BEGIN PUBLIC KEY-----
    MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDZaVdjIIBjz0gTFB7dJsh3Cwkt
    T3yfiKq5/Pumv7qtf/1/Gf2vv+uEaDjwKMC9s9106dcpY/ShofbiL0CGc3EbRxfL
    Vbm2N56fZjeYbVsx5Q0rRe2E29MqSZaSg/TqJ/S4ZV8TdYTHvR07gAJR+u0gFCWJ
    Op9N3EcxO2Ttlao0KwIDAQAB
    -----END PUBLIC KEY-----

Java SE¤Ç¡¢¸°¥»¥Ã¥È¤òÆɤ߼è¤ë

¼Â¹Ô·ë²Ì

»²¹Íʸ¸¥

  1. ¥µ¥ë¤Ë¤â¤ï¤«¤ëRSA°Å¹æ, http://www.maitou.gr.jp/rsa/
  2. PHP¥Þ¥Ë¥å¥¢¥ë, http://jp2.php.net/manual/ja/function.openssl-public-encrypt.php#55901

Java#JavaSE


*1 Java2 1.4¤Þ¤Ç¤Ï¡¢RSA°Å¹æ¥¯¥é¥¹¤Î¥¤¥ó¥¿¥Õ¥§¡¼¥¹¤Î¤ßÄó¶¡

źÉÕ¥Õ¥¡¥¤¥ë: filedecrypt.png 3318·ï [¾ÜºÙ] filersaEnDeCrypt.png 2755·ï [¾ÜºÙ] filersaGetKey.png 4022·ï [¾ÜºÙ] filematrix.png 4255·ï [¾ÜºÙ] filersa.html 27177·ï [¾ÜºÙ]

¥È¥Ã¥×   ÊÔ½¸ Åà·ë º¹Ê¬ ¥Ð¥Ã¥¯¥¢¥Ã¥× źÉÕ Ê£À½ ̾Á°Êѹ¹ ¥ê¥í¡¼¥É   ¿·µ¬ °ìÍ÷ ñ¸ì¸¡º÷ ºÇ½ª¹¹¿·   ¥Ø¥ë¥×   ºÇ½ª¹¹¿·¤ÎRSS   sitemap
Last-modified: 2014-06-16 (·î) 21:55:44 (3846d)
Short-URL: http://at-sushi.com/pukiwiki/index.php?cmd=s&k=cbc9346a13
ISBN10
ISBN13
9784061426061