Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1102463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:11:25+00:00 2026-05-17T01:11:25+00:00

I had some time and decided to implement a one time pad just for

  • 0

I had some time and decided to implement a one time pad just for fun and self education. Now I end up with a weird behavior of the data. Its driving me crazy ^^. Would you please help me? Thanks in advance.

There is an encrypt method which takes as arguements:

  • an InputStream for the plain text
  • an OutputStreams for the cipher text
  • and an OutputStreams for the key.

There is an decrypt method which takes as arguements:

  • an InputStream for the cipher text
  • an InputStream for the key
  • an OutputStreams for the plain text.

There is a main method to test and debug the code. Here is the class:

import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.security.SecureRandom;

 public class MyPad {

  public static void encrypt(InputStream plainTextInputStream, OutputStream cipherTextOutputStream, OutputStream keyOutputStream) {
   int plainTextByte;
   SecureRandom random = new SecureRandom(); 
   System.out.println("plain\tkey\tcipher");
   try {
    while((plainTextByte = plainTextInputStream.read()) != -1){
     int keyByte = random.nextInt(256);
     int cipherTextByte = (plainTextByte + keyByte) % 256;
     System.out.println(plainTextByte + "\t" + keyByte + "\t" + cipherTextByte);
     cipherTextOutputStream.write(cipherTextByte);
     keyOutputStream.write(keyByte);
    }
    plainTextInputStream.close();
    cipherTextOutputStream.close();
    keyOutputStream.close();
   } catch (IOException e) {
    e.printStackTrace();
   }
  }

  public static void decrypt(InputStream cipherTextInputStream, InputStream keyInputStream, OutputStream plainTextOutputStream) {
   int cipherTextByte;
   System.out.println("plain\tkey\tcipher");
   try {
    while((cipherTextByte = cipherTextInputStream.read()) != -1){
     int keyByte = keyInputStream.read();
     int plainTextByte = Math.abs(cipherTextByte - keyByte) % 256;
     System.out.println(plainTextByte + "\t" + keyByte + "\t" + cipherTextByte);
     plainTextOutputStream.write(plainTextByte);
    }
    cipherTextInputStream.close();
    keyInputStream.close();
    plainTextOutputStream.close();
   } catch (IOException e) {
    e.printStackTrace();
   }
  }

  public static void main(String[] args) {
   String plainText = "This is my plain text.";
   InputStream plainTextInputStream = new ByteArrayInputStream(plainText.getBytes());
   ByteArrayOutputStream cipherTextOutputStream = new ByteArrayOutputStream();
   ByteArrayOutputStream keyOutputStream = new ByteArrayOutputStream();

   System.out.println("------------------------------------ encrypting");
   encrypt(plainTextInputStream, cipherTextOutputStream, keyOutputStream);

   String cipherText = cipherTextOutputStream.toString();
   String key = keyOutputStream.toString();
   System.out.println("plaintext:\t" + plainText);
   System.out.println("ciphertext:\t" + cipherText);
   System.out.println("key:\t" + key);
   InputStream cipherTextInputStream = new ByteArrayInputStream(cipherText.getBytes());
   InputStream keyInputStream = new ByteArrayInputStream(key.getBytes());
   ByteArrayOutputStream plainTextOutputStream = new ByteArrayOutputStream();

   System.out.println("------------------------------------ decrypting");
   decrypt(cipherTextInputStream, keyInputStream, plainTextOutputStream);

   plainText = plainTextOutputStream.toString();
   System.out.println("plaintext:\t" + plainText);
  }

 }

Now here is the problem I have. I encrypt a plain text and decrypt it immediatley, but the encrypted plain text is not the same as the original. I made some ouput for debugging and it seems like, the data I wrote during encryption is not the same data I read during decryption. Look at the output yourself:

 ------------------------------------ encrypting
 plain key cipher
 84 25 109
 104 239 87
 105 86 191
 115 74 189
 32 100 132
 105 17 122
 115 211 70
 32 147 179
 109 104 213
 121 118 239
 32 139 171
 112 244 100
 108 196 48
 97 181 22
 105 226 75
 110 94 204
 32 156 188
 116 92 208
 101 91 192
 120 165 29
 116 177 37
 46 49 95
 plaintext: This is my plain text.
 ciphertext: mW���zF���d0K̼��%_
 key: �VJdӓhv��ĵ�^�\[��1
 ------------------------------------ decrypting
 plain key cipher
 84 25 109
 152 239 87
 48 191 239
 2 189 191
 103 86 189
 165 74 239
 91 100 191
 172 17 189
 28 211 239
 44 147 191
 85 104 189
 4 118 122
 169 239 70
 48 191 239
 2 189 191
 50 239 189
 48 191 239
 2 189 191
 7 196 189
 58 181 239
 48 239 191
 2 191 189
 89 189 100
 46 94 48
 217 239 22
 116 191 75
 15 189 204
 96 92 188
 148 91 239
 48 239 191
 2 191 189
 50 189 239
 48 239 191
 2 191 189
 160 189 29
 12 49 37
 96 -1 95
 plaintext: T�0g�[�,U�020:0Y.�t`�020�`

This looks really weird to me. Any suggestions where that difference is coming from?

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T01:11:26+00:00Added an answer on May 17, 2026 at 1:11 am

    There are two problems:

    1. You use String#getBytes to get the bytes to feed back in. This means that they went through a round of string decoding and encoding. Notice that the sequence of key bytes is not the same on encryption and decryption. You should instead use ByteArrayOutputStream#toByteArray
    2. Math.abs(cipherTextByte - keyByte) % 256 is wrong. (0 – 1) (mod 256) = 255 not 1. You should instead use (256 + cipherTextByte - keyByte) % 256
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.