- 940 名前:デフォルトの名無しさん mailto:sage [2007/05/11(金) 04:15:29 ]
- >>935
public class HogeCode { private static java.util.Random r = new java.util.Random(); public static byte[] generateKey() { byte b; do { b = (byte)r.nextInt(); } while (b == -128); return new byte[]{b, (byte)(-b)}; } public static byte code(byte text, byte key) { return (byte)(text + key); } public static void main(String[] args) { byte[] key = HogeCode.generateKey(); System.out.printf("公開鍵: %d, 秘密鍵: %d%n", key[0], key[1]); byte[] 元の文 = "Hogeほげ".getBytes(); byte[] 暗号文 = new byte[元の文.length]; byte[] 復号文 = new byte[元の文.length]; System.out.print("元の文: "); for (byte b : 元の文) System.out.print(b + ","); System.out.println("\"" + new String(元の文) + "\""); for (int i = 0; i < 元の文.length; i++) 暗号文[i] = HogeCode.code(元の文[i], key[0]); // 公開鍵で暗号化 System.out.print("暗号文: "); for (byte b : 暗号文) System.out.print(b + ","); System.out.println("\"" + new String(暗号文) + "\""); for (int i = 0; i < 暗号文.length; i++) 復号文[i] = HogeCode.code(暗号文[i], key[1]); // 秘密鍵で復号化 System.out.print("復号文: "); for (byte b : 復号文) System.out.print(b + ","); System.out.println("\"" + new String(復号文) + "\""); for (int i = 0; i < 暗号文.length; i++) 復号文[i] = HogeCode.code(暗号文[i], key[0]); // 公開鍵で復号化 System.out.print("公開鍵で復号: "); for (byte b : 復号文) System.out.print(b + ","); System.out.println("\"" + new String(復号文) + "\""); } }
|

|