Total members 11893 |It is currently Wed Nov 13, 2024 12:50 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





Can anyone tell me why I'm getting the folllowing errors when i use this code. The code was taken directly from a Java encryption textbook so I assume it should run with zero problems.

/////////////////////////////////////////////////////////////

C:\Users\BATEMAN>cd "C:\Users\BATEMAN\Documents\java"

C:\Users\BATEMAN\Documents\java>javac InterNetworkSecurityAssignmentOne.java
InterNetworkSecurityAssignmentOne.java:52: warning: sun.misc.BASE64Encoder is Sun proprietary API and may be removed in a future release
BASE64Encoder encoder = new BASE64Encoder();
^
InterNetworkSecurityAssignmentOne.java:52: warning: sun.misc.BASE64Encoder is Sun proprietary API and may be removed in a future release
BASE64Encoder encoder = new BASE64Encoder();
^
InterNetworkSecurityAssignmentOne.java:58: warning: sun.misc.BASE64Decoder is Sun proprietary API and may be removed in a future release
BASE64Decoder decoder = new BASE64Decoder();
^
InterNetworkSecurityAssignmentOne.java:58: warning: sun.misc.BASE64Decoder is Sun proprietary API and may be removed in a future release
BASE64Decoder decoder = new BASE64Decoder();
^
4 warnings

C:\Users\BATEMAN\Documents\java>java InterNetworkSecurityAssignmentOne
Exception in thread "main" javax.crypto.BadPaddingException: Given final block not properly padded
at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..)
at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..)
at com.sun.crypto.provider.DESCipher.engineDoFinal(DashoA13*..)
at javax.crypto.Cipher.doFinal(DashoA13*..)
at InterNetworkSecurityAssignmentOne.main(InterNetworkSecurityAssignmentOne.java:60)

/////////////////////////////////////////////////////////

import java.io.*;
//The Java Security API
import java.security.*;
//The Java Security API
import javax.crypto.*;
import sun.misc.*;
//The Java Security API is a set of packages that are used for writing secure programs in Java.

public class InterNetworkSecurityAssignmentOne {
public static void main(String[] args) throws Exception {
// Check command-line arguments. -e or -d, & a string.
if (args.length < 2) {
// -e will encrypt data, -d decrypts it.
System.out.println("Usage: InterNetworkSecurityAssignmentOne -e|-d text");
//
return;
}
// Get or create key.
Key key;
try {
ObjectInputStream in = new ObjectInputStream(
new FileInputStream("AssignmentOneKey.ser"));
key = (Key)in.readObject();
in.close();
}
catch (FileNotFoundException fnfe) {
//Symmetric ciphers use a single key instead of a key pair. The JCE has a class called
//javax.crypto.KeyGenerator that can randomly generate a single key.
//Here I have created it using a getInstance().
//Next I asked for a key for the DES cipher.
KeyGenerator generator = KeyGenerator.getInstance("DES");
generator.init(new SecureRandom());// pseudo-random number generator PRNG?
key = generator.generateKey();
ObjectOutputStream out = new ObjectOutputStream(
new FileOutputStream("AssignmentOneKey.ser"));
out.writeObject(key);
out.close();
}
// Get a cipher object.
Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
// Encrypt or decrypt the input string.
if (args[0].indexOf("e") != -1) {
cipher.init(Cipher.ENCRYPT_MODE, key);
String amalgam = args[1];
for (int i = 2; i < args.length; i++)
amalgam += " " + args[i];
byte[] stringBytes = amalgam.getBytes("UTF8");
byte[] raw = cipher.doFinal(stringBytes);
BASE64Encoder encoder = new BASE64Encoder();
String base64 = encoder.encode(raw);
System.out.println(base64);
}
else if (args[0].indexOf("d") != -1) {
cipher.init(Cipher.DECRYPT_MODE, key);
BASE64Decoder decoder = new BASE64Decoder();
byte[] raw = decoder.decodeBuffer(args[1]);
byte[] stringBytes = cipher.doFinal(raw);
String result = new String(stringBytes, "UTF8");
System.out.println(result);
}
}
}




Author:
Newbie
User avatar Posts: 6
Have thanks: 0 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : BASE64Encoder/ BASE64Decoder & BadPaddingException errors.
 errors in commission program     -  
 C++ DES Encryption Package - Errors?     -  
 C program that displays shapes. Keep getting errors.     -  









Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com